INeedAGig Posted March 12, 2012 Share Posted March 12, 2012 Alrighty, I have been working on a simple hit counter and I can't quite get it to work, I am having trouble getting the system to actually display the amount of hits, here is the code for review. This code below is located within the file I call 'count_processor.php' <?php $myfile = "counter/hits.txt"; if(file_exists($myfile)) //Check for file { $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 recent visitors",$visits); //saves our visits message in a variable ($message) that will be used as output print $message; ?> Now, this small snippet of code is located on the page I want to count hits for, which is the index.php file. This calls upon the above code and in turn renders the data onto the page, but all that is showing up on the index.php page is 'recent visitors' which is the text after the numerical value of visitors is displayed, i.e. "137 recent visitors" <?php include 'counter/count_processor.php'; ?> Any ideas? Thanks! Also, you will notice I am using a flat-file system (.txt), no MySQL database. Quote Link to comment https://forums.phpfreaks.com/topic/258775-not-quite-working/ Share on other sites More sharing options...
requinix Posted March 12, 2012 Share Posted March 12, 2012 $var = fopen('$myfile,\'r+\''); Reconsider that. Quote Link to comment https://forums.phpfreaks.com/topic/258775-not-quite-working/#findComment-1326586 Share on other sites More sharing options...
INeedAGig Posted March 12, 2012 Author Share Posted March 12, 2012 What would you recommend? I am a bit stuck atm. Quote Link to comment https://forums.phpfreaks.com/topic/258775-not-quite-working/#findComment-1326587 Share on other sites More sharing options...
xyph Posted March 12, 2012 Share Posted March 12, 2012 I'd suggest using correct syntax. Quote Link to comment https://forums.phpfreaks.com/topic/258775-not-quite-working/#findComment-1326593 Share on other sites More sharing options...
INeedAGig Posted March 12, 2012 Author Share Posted March 12, 2012 Alrighty, I fixed that line of code, which is now: $var = fopen("$myfile", "r+") or exit ("Unable to open file!"); But I am receiving this error on the page: Warning: fopen(counter/hits.txt) [function.fopen]: failed to open stream: Permission denied Quote Link to comment https://forums.phpfreaks.com/topic/258775-not-quite-working/#findComment-1326602 Share on other sites More sharing options...
requinix Posted March 12, 2012 Share Posted March 12, 2012 Use FTP or whatever you have to create that file yourself. Put a 0 in it. Then change its permissions (chmod) to 0666 (=readable and writable for everybody) and try your script again. Quote Link to comment https://forums.phpfreaks.com/topic/258775-not-quite-working/#findComment-1326611 Share on other sites More sharing options...
INeedAGig Posted March 12, 2012 Author Share Posted March 12, 2012 I already have the file created, with the 0, which has now updated itself to '1' but no further, I can't CHMOD, constant '504 command not implemented for that parameter' error in FileZilla....I know that's what is holding it up...just not sure how to get the permissions set now. Quote Link to comment https://forums.phpfreaks.com/topic/258775-not-quite-working/#findComment-1326614 Share on other sites More sharing options...
requinix Posted March 12, 2012 Share Posted March 12, 2012 Then... asking your hosting provider how you can change permissions on files. Or use a database instead of a file. Quote Link to comment https://forums.phpfreaks.com/topic/258775-not-quite-working/#findComment-1326626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.