Jump to content

creating a hit counter


cyberdie
 Share

Recommended Posts

Hi guys just wanted a quick fix as I dont know PHP, but all i was looking to do was load the following. I have a file called db.txt which has 0 numeric value and have sourced this code

<?php$db = "db.txt";  // The database we created$handle = fopen($db, 'r+') ; // Open the db, read and be able to write to it$data = fread($handle, 512) ; // Get current count from the db$count = $data + 1; // Add in a count since it's being readprint $count; // Display current count on the web pagefseek($handle, 0) ; // Point back to the beginning of the filefwrite($handle, $count) ; // Save the countfclose($handle) ; // Close count?>

I can have the counter work if I load counter.php however I want the counter embedded into the index homepage and the number increase on open, like a hit counter should. Please can someone provide the answer.

Link to comment
Share on other sites

You might have to wait awhile for an answer. Since you're new, you should know that it's considered discourteous to ask for help with someone else's script while avoiding the tutorials. The more learning you do before you ask, the faster and more meaningful the answer will be. So to get the learning started, how will you include your script in your index file and why are you using text? How else could you accomplish the same task? Your question gives you a great opportunity to be independent. It's like they say in the fishing business, Give him a fish and he'll eat for a day. Give him a fishing pole and help him understand how to use it and he'll eat for the rest of his like. W3schools gives poles to everyone that's willing to use them.

Edited by niche
Link to comment
Share on other sites

I can't give the answer, but a huge flaw you are going to get is there is no "net' to catch the same user. You can spam the counter by just refreshing the page.

  • Like 1
Link to comment
Share on other sites

you can use include() to include the counter.php page where it needs, better will be encapsulate it in a function aand calling it.

  • Like 1
Link to comment
Share on other sites

Appreciate the comments, although maybe not the patronising one, although that may because I'm not much of a fisherman. Appreciate that the PHP is flawed in terms of refresh of same user, although was just looking for a counter that would act as a guide. Worked it out though as needed to set home page as php and use birbal's include feature. Job Done. (stick of dynamite seems to do just as good a job)

Link to comment
Share on other sites

SQL would be one alternative (in all it's various forms - I use MYSQL), HTML, would not be an option since it doesn't handle variables per se.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...