Jump to content
xisto Community
websaint

Make Your Own Very Simple Counter Using PHP!

Recommended Posts

If you want one more secure counter, use MySQL Database!
You just have to:

CREATE TABLE website_counter(visits int(10))
And put this code in the top of each page that you want to be counted:

$conn = mysql_connect("YOUR HOST","YOUR USER","YOUR PASSWORD") or die('MySQL said:  ' . mysql_error());$db = mysql_select_db($db);$sql = mysql_query("SELECT * FROM website_counter") or die('MySQL said:  ' . mysql_error());while($visits = mysql_fetch_array($sql)){$visits = $dados["visits"];$visits_new = $visits + 1;}$query = "UPDATE TABLE website_counter SET visits = $visits_new"; mysql_query($query, $conn) or die(mysql_error());

And where you want to show the number os visits:
echo ("Visits: $visits_new");

OBS.: I didn't test it. If you got any errors, post here.

Share this post


Link to post
Share on other sites

Hmm, I think the counter (at the beginning of this thread) can't be deloyed on busy site. Because if many pages read and write that file at once, there can be a error when accessing alle.txt

Share this post


Link to post
Share on other sites

@Alexandre : your program has a great advantage, you are independent from any provider, so you can use it on a server other than Xisto, or even on a server on a private network.

OBS.: I didn't test it.

yes, it has to be tested first. For instance, I am curious to see where $dados is defined.

Share this post


Link to post
Share on other sites

For a simple php counter there's no need to use databases, flatfiles will do fine, the example by websaint is suffice as long as

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

Is stored on the server and is not changeable or dependable on userinput.

Anyway, I was thinking that using an OOP approach here might be an overkill. What if you take the following PHP file

vc.php
<?php   $countfile = file("alle.txt");   $count = $countfile[0];   $count= $count + 1;   $fp = fopen("alle.txt","w");   $fw = fwrite($fp,$count);   fclose($fp);   echo $count;}?>

Basically you know that whenever vc.php is called there is a new page visit so which means that you need the variable $count back.

So all you need to do now in your other php files is to have them include("vc.php") in the code.

eg

index.php
<?phpecho("hi");include("vc.php");?>

This would be failsafe for any crosssitescripting attempts since there's no userinput expected. Also, even if the name of vc.php is known so people can call it directly, it doesn't matter since they could only +1 to your counter, which is also possible if they reload your page 20+ times. Also, people still need to know the name of the counter script, which is quite difficult to guess if they don't see it stated somewhere.

Alternatively, you could also add an ip banning system to this script to check if a user has accessed this site before. Which is not really necessary if you want to know how much your page has been loaded.

About the busy website thing, it'd take quite a lot of requests per second to be faster than the opening and writing of one byte in a single file for it to have a deadlock. Also, If one has a busy site that can accomplish such thing, it'd be wiser to use a database approach since you can generate more statistics than just the amount of page visits (which is only interesting with a lot of visitors).

Ps. Anything flash is bad :)

Share this post


Link to post
Share on other sites

@Alexandre : your program has a great advantage, you are independent from any provider, so you can use it on a server other than Xisto, or even on a server on a private network.

 

yes, it has to be tested first. For instance, I am curious to see where $dados is defined.

1064336637[/snapback]

Sorry, is not $dados['visits'], but $visits['visits']

Sorry again for the mistake.

Share this post


Link to post
Share on other sites

Actually... am a complete newbie to PHP.. dont know even a bit about it.. and all that scripting stuff... so was a lot confused in here... Will be learning it soon.. can anyone point out the link to the forum that offers PHP for newbies?

Share this post


Link to post
Share on other sites

I know a very good free script named EliteStats.

You just have to put its folder in your main directory and include a one line of code in your webpage.

It will display How many visitors are online and complete statistics with IP address.

 

Sohail Ahmed

sohail4@msn.com

 


These scripts are so good, personally i was using them before begining to learn PHP, since that time i don't like to put any ready scripts on my site, i prefer to create it myself, that's the joy of PHP and other scripting languages, so i think better to post these useful links to these ready-made scripts on a new topic and to leave us enjoying this scripting battle :o

Personally i prefer the second counter, i like the combination between PHP/MySQL, more secured and better than ordinary text file, but ofcourse the first one is more simpler one.


Edited by XIII (see edit history)

Share this post


Link to post
Share on other sites

OBS.: I didn't test it. If you got any errors, post here.


it gives me this error:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE counter SET visits =' at line 1
i tried a lot but never got a result :o

Share this post


Link to post
Share on other sites

did anyone tried with this?, i tried alot but it keeps telling me there is an error :

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE counter SET visits =' at line 1

hope anyone can tell us why it's not working.

Share this post


Link to post
Share on other sites

did anyone tried with this?, i tried alot but it keeps telling me there is an error :hope anyone can tell us why it's not working.


Can you paste the line/part of the script that's causing this error ? It'll be easier to diagnose that way.

Share this post


Link to post
Share on other sites

Can you paste the line/part of the script that's causing this error ? It'll be easier to diagnose that way.

 


That's the code for the whole script, ofcourse i'm putting the values for connect in my script, the counter table called "counter"

 

<?php$conn = mysql_connect("localhost","db_username","db_pssword") 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 TABLE counter SET visits = '$visits_new' ");mysql_query($query, $conn) or die(mysql_error());echo ("visits : $visits_new");?>

Share this post


Link to post
Share on other sites

 

while($visits = mysql_fetch_array($sql))

{

$visits = $visits["visits"];

$visits_new = $visits + 1;

}

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

mysql_query($query, $conn) or die(mysql_error());

 


One error I can spot rightaway is with the $query statement. Why is the $visits_new variable within single quotes ? The function of the script is to update the table with the new value of $visits_new, right? In that case, it has to be enclosed with DOUBLE QUOTES in order for php to parse it and replace it with it's value.

 

Another factor that's confusing is that why's the statement enclosed by paranthesis ? ( $query = (...)) - those () are totally unnecessary here, although they shouldn't ideally cause a problem...

 

Try modifying that line to:

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

Try this and let me know if it still produces the same error.

Share this post


Link to post
Share on other sites

 

Try modifying that line to:

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

Try this and let me know if it still produces the same error.

 


Now i got the same error, really so strange case to me :lol:

 

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE counter SET visits = "$visits_new"' at line 1

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.