Need Help About Webpage Counter Using PHP / JS

Status
Not open for further replies.

muralikrishnan

Broken In
Hi Friends,

I need webpage counter code using PHP/JavaScript.I don't need the site i need code which is run for my localhost or with in LAN area for that i need the counter program is there any site avilable i tried and i didn't get the source program many sites provide only their site to paste and that site logo with the counting image is shown in the page so i need the general script with out any site support..

Thanks in advance............
 

victor_rambo

हॉर्न ओके प्लीज़
For PHP you will need a server. Here are the steps:
1. Create a text file that shall store the page count.
2. When a page is visited, run a PHP script that shall take the value from the text file, increment by 1, and put it back into the text file(open->read->erase all data->write new data).
3. Display the incremented count on the webpage.

With JS, you can only store it in a cookie on the visitors' browser and the count shall be specific to that user. Also, the cookies can be cleaned(deleted) at any time by the user. So JS will be a poor candidate.
 

toofan

Technomancer
I had just created a small php code for displaying the hit counter. Its working. Please give suggestion about any loopholes in this code. As i am in learning stage.

PHP:
<?php 
// connect to mysql
mysql_connect("localhost", "root", "password") or die("Can not connect: ".mysql_error());
/* connecting to the database. create a database name hitcounter in mysql */
mysql_select_db("hitcounter") or die("Error connecting database: ".mysql_error());

/* first create a table in mysql named hitcounter with two columns id and counter with values 1 and 0 */
// geting the stored value of counter which is 0 from the table
$query = "select counter from hitcounter";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$counter = $row[0];
$hitcounter = $counter +1;

//now submitting the updated counter again to database
$query_submit = "update hitcounter set counter = '$hitcounter'
                where id=1 ";
$result_submit = mysql_query($query_submit);

?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>title</title>
</head>

<body>
<h3> Hit counter</h3>
<input type="text" size="6" value="<?php echo "$hitcounter" ; ?>">
</body>
</html>
 

victor_rambo

हॉर्न ओके प्लीज़
^^

PHP:
<?php
mysql_connect('localhost',"$username","$password");
mysql_select_db('hitcounter');
/*Lets update the table named 'counter' that holds the count in a field name 'count'. The field 'count' is of INT type!!*/
mysql_query('UPDATE hitcounter SET count=count+1');
/*Lets fetch the updated value of the table*/
$resource=mysql_query('SELECT count FROM hitcounter');
$count=mysql_result($resource,0,'count');
echo 'This page has served paged '.$count.' times.';
?>

Also, if you want to have to store separate counts for each webpage, then we will have to
the 'ID' field that should be linked to the web page name or path. The way of calling the counter functions also would need to be modified.

Since you have asked for suggestions:
1. in PHP, understand when you should use double quotes and when single quotes. A string should be enclosed in double quotes when it contains a variable that should be parsed while outputting it. Enclosing string in double quotes means that PHP has to do extra work to search if there is any variable in the string. If its there, fine, else waste of time and energy for the parser. So if your string contains no variable, better to enclose it within single quotes.

2. In a MySQL table, have the appropriate database fields. There is no need to have 2 fields. Once will suffice. Also, since we need mathematical manipulations, we should SET the type of field to 'INT'. This way, we won't have to use PHP's math ability to update the count.

3. Since we are selecting only one column from the table, we can use 'mysql_result()' rather than 'mysql_fetch_*()' functions.

4. The ' or die(mysql_error())' tip: You already know it. So no need stressing it.
 
Last edited:

toofan

Technomancer
Thanks roshan,

You have made my long code to just few lines and I got the each and every word you said. Its really helpful to learn when someone guides you. I will be looking forward for more helpful tips.

One suggestions
There isn't any thread for PHP. Everyone talks about Java, C++, C, C# but no one for php. Could you start a new thread for php tutorials. In which you and other fellows can post tutorials, tips and tricks which comes only by experience. I know it will be a hard task to do so and devote your time to it but It will help many many.

Thanks once again.
 

victor_rambo

हॉर्न ओके प्लीज़
^^
The name is Rohan and not Roshan :)
As about me starting a thread on PHP tutorials, I guess that would amount to reinventing the wheel. For the interested ones, tips and tutorials are a just a Google search away! Of course, you can ask for help if you are held up somewhere!

And of course, Thanks :)

btw if you are interested, you can check the relevant categories on my website: *www.w3hobbyist.com/
 
Last edited:

toofan

Technomancer
Oh sorry Rohan but from starting I always read it Roshan, this is the good example of "we see that what we want to see" bug in human brain coding.:D

I am checking your website. :p
 

kapsicum

spice it up
hey guys this is my suggestion as per what i believe that counters must increment only once per user session ....
we need to update the DB counter only ONCE for a user's particular session. since i believe dats the Fair User Visiting Counter

So as to avoid the query execution & counter increments every time the page is refereshed by a user, following is a small addition/modification to ur code:

PHP:
<?php
session_start();
if(!isset($_SESSION['viewed']))
{
 $_SESSION['viewed']=1;
 //rohan's code except last line 
}

echo 'This page has served paged '.$count.' times.';
?>

let me know if u think I am wrong and/or my code is not proper ...since its long time i have worked on PHP
 
Last edited:

victor_rambo

हॉर्न ओके प्लीज़
@Kapsicum,
He has asked for webpage count, not visitor count, though visitor count is more significant that page count :).
Since you have focused on visitor count, I will suggest that you use a tracking cookie instead of sessions. As you will know that session cookies are cleared at the end of session, tracking cookies can be set to expire to a given time. So every time the visitor requests the page after closing the browser and restarting it will increment the counter.

PHP:
<?php
if(!isset($_COOKIE['my_tracking_cookie']))
{
//set cookie with a long expiry
//now update the cookie
}
?>
Of course, you can't prevent error due visitors clearing their cookies prematurely.
 
Status
Not open for further replies.
Top Bottom