Jump to content
xisto Community
websaint

Make Your Own Very Simple Counter Using PHP!

Recommended Posts

This will not insert any data into the database, it will always say 0 Hits

 

WOW, i found a solution:

 

$conn = mysql_connect("localhost","db_username","db_password") or die('MySQL said:  ' . mysql_error());$db = mysql_select_db("db_name");$sql = mysql_query("SELECT * FROM counter") or die('MySQL said:  ' . mysql_error());while($visits = mysql_fetch_array($sql)){$visits = $visits['visits'];$visits_new = $visits + 1;}$query = "UPDATE counter SET visits = '$visits_new'";mysql_query($query, $conn) or die(mysql_error());echo "$visits_new Visits";

 

i removed TABLE from:

$query = "UPDATE TABLE counter SET visits = '$visits_new'";

and don't forget to set the visits value in the database to zero or any number you want, just don't leave it as default.

Share this post


Link to post
Share on other sites

Good... but you might find others steal your counter if they find it...they will link to your counter script, and store their page cound on your server.

its always good to add some kind of proetection againsed this. a good, but not perfect way is to make sure the referer comes from your own site, and nobody elses.



you misunder stand my meaning.......
i mean someone could replace

<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>

the include file with a coomplete URL to anouther server. and replace "alle.txt" with a different filename.

A solution to your problem just encode your webpage.

Share this post


Link to post
Share on other sites

file based counter may jam - sometimes it wont be able to overwirte the data - when many visitors navigate to your web. The other problem is if you ain't got permissions to open a file on your server...hence host dependent.

Share this post


Link to post
Share on other sites
Does the code hate me or is it wrong?Make Your Own Very Simple Counter Using PHP!

I've integrated my code and it counts double, it started like such: 11 and then when I refresh it adds another 11 so, it goes by 22 and then 33. Really bad :(

Share this post


Link to post
Share on other sites

is this for online counting? for example: "Now 3 online visitors". Or for general counting?



Hi!! I'm going to show you how to make your own counter useing php!! :) It's really easy and you woun't have to keep seaching the net for free and bannerless counters.So here comes the script:

<?php//Simple class for counting visits//Written by WebSaintclass Teller {   function count() {   $countfile = file("alle.txt");   $count = $countfile[0];   $count= $count + 1;   $fp = fopen("alle.txt","w");   $fw = fwrite($fp,$count);   fclose($fp);   echo $count;   }}$obj =& new Teller;$obj->count();?>
Then add this to display the number of visitors:

<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>

That's it. I hope you'll find the script useful! :)

Share this post


Link to post
Share on other sites

is this for online counting? for example: "Now 3 online visitors". Or for general counting?

If you read the example you will notice that it adds +1 to the counter each time somebody enters there.
So, if you put it somewhere on your main page, it will tell you how many people read this page.
If you clear the content of the file each morning, at the end of the day it will tell you how many people today. If you don't clear "alle.txt" it will tell you the total number since creation of the file.

Share this post


Link to post
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

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.