Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 01:06 AM
Member
 
Join Date: May 2007
Posts: 35
HEATSEEKERBUNGE is on a distinguished road
Counter Script help

Alright well besides the fact that im pissed because PHP is the one of the few languages that i dont have down pact, which makes me mad because i have to ask for help like a noob, but can someone tell/hint me in the right direction on how to fix this script? Please?

Note: its a text file based counter just need something simple like this to tell me how many people have visited the page.

Code:
Code:
<?php $myfile = "visits.txt";
//we assign our file name to the variable we'll use to handle it
if(file_exists($myfile))//if the file exists
{
//we run our counter script $var = fopen( $myfile,\'r+\');
//opens in read and write mode our file
$visits = fread($var,filesize($myfile));
//puts the content of the file for its whole lenght
rewind( $var );
//resets the file position indicator to the beginning
$visits++; //increments the actual number of vists by 1
fwrite($var, $visits);
//writes in the variable the actual (incremented) value
fclose($var);//closes our file reference
}
else
{
print "File $myfile doesn't exist...";
Die();
//if the file doesn't exist prompts a warning and kills the script
}
$message = sprintf("%s visitors since 08/20/2005.",$visits);
//saves our visits message in a variable ($message) that will be used as output
print $message;
?>

And heres the error i get:

Code:
Warning: fread(): supplied argument is not a valid stream resource in /home/paaradi/public_html/aceifiedassasins/counter.php on line 7

Warning: rewind(): supplied argument is not a valid stream resource in /home/paaradi/public_html/aceifiedassasins/counter.php on line 9

Warning: fwrite(): supplied argument is not a valid stream resource in /home/paaradi/public_html/aceifiedassasins/counter.php on line 12

Warning: fclose(): supplied argument is not a valid stream resource in /home/paaradi/public_html/aceifiedassasins/counter.php on line 14
visitors since 08/20/2005.
So if anyone can help that would be greatly appreciated.

Reply With Quote
  #2 (permalink)  
Old 07-04-2007, 02:42 PM
Senior Member
 
Join Date: May 2007
Posts: 181
Blog Entries: 1
skyfe is on a distinguished road
Quote:
<?php
$myfile = "visits.txt";
//we assign our file name to the variable we'll use to handle it
if(file_exists($myfile))//if the file exists
{
//we run our counter script
$var = fopen( $myfile,'r+');
//opens in read and write mode our file
$visits = fread($var,filesize($myfile));
//puts the content of the file for its whole lenght
rewind( $var );
//resets the file position indicator to the beginning
$visits++; //increments the actual number of vists by 1
fwrite($var, $visits);
//writes in the variable the actual (incremented) value
fclose($var);//closes our file reference
}
else
{
print "File $myfile doesn't exist...";
Die();
//if the file doesn't exist prompts a warning and kills the script
}
$message = sprintf("%s visitors since 08/20/2005.",$visits);
//saves our visits message in a variable ($message) that will be used as output
print $message;
?>
try to get the content by; file_get_contents.
so it will be

Code:
<?php 
$myfile = "visits.txt";
//we assign our file name to the variable we'll use to handle it
if(file_exists($myfile))//if the file exists
{
//we run our counter script 
$var = fopen( $myfile,\'r+\');
//opens in read and write mode our file
$visits = file_get_contents($myfile);
//puts the content of the file for its whole lenght
rewind( $var );
//resets the file position indicator to the beginning
$visits++; //increments the actual number of vists by 1
fwrite($var, $visits);
//writes in the variable the actual (incremented) value
fclose($var);//closes our file reference
}
else
{
print "File $myfile doesn't exist...";
Die();
//if the file doesn't exist prompts a warning and kills the script
}
$message = sprintf("%s visitors since 08/20/2005.",$visits);
//saves our visits message in a variable ($message) that will be used as output
print $message;
?>
another tip: don't set php scripts after the // too, just start a new line, dont be lazy xD
And, you could even use MySql for this dude, like:

Code:
$get_visits = mysql_query("SELECT * FROM visits");
while($row = mysql_fetch_assoc($get_visits)) {
  echo  "VISITS: ".$row['amount'];
}
$update_visits = mysql_query("UPDATE visits SET amount = amount+1 ");
something like that, where amount contains the visits amount and the table is called 'visits'.

Good luck.
Reply With Quote
  #3 (permalink)  
Old 07-05-2007, 10:01 PM
Member
 
Join Date: May 2007
Posts: 35
HEATSEEKERBUNGE is on a distinguished road
Skyfe i swear you and base have helped me out the most but it actually turns out while i was waiting i followed a different tut on how to make a more complex script which logged ips and which hit each day and which page was being viewed the most, and it works really well and it uses my favorite thing in all of web design, MYSQL DATABASES! But thx for awnsering quickly!


Last edited by HEATSEEKERBUNGE; 07-05-2007 at 10:09 PM.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 03:09 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com