Page 1 of 1

PHP counter ... please help!

Posted: Tue May 10, 2005 5:19 am
by perik
Hi!

Ive tried to install a counter-script that displays the number of pageloads on my forum. The forum is made in php and are using mysql-database to store the articles that are possible to submit on the site. Ive tried several different counters which have used the “include” command to include the script on the page, in my case content.php.

The problem is that the counter are counting the total number of pageloads on all of the articles alltogether, instead of counting the number of pageloads on every unique article. The page that are calling the articles from the database are as I said content.php and every article are named content.php?article.1, content.php?article.2, content.php?article.3 etc. the number refering to the number in the database.

My question is:

Are there anyone who got a script or any suggestion on how I can manage to view the number of pageloads on every unique article? I just manage to view the total amount of pageloads.

I guess I, somehow have to have a unique code for each article but the prolem is how and what kind of code?

Hope you understood my problem.

Thanks for your help

Per

Posted: Tue May 10, 2005 5:43 am
by timvw
Add something like below to your content.php (untested).

This only works in the case there is no session_id or something added to the url. In the case session_id is added to the url, you will have to get all the other chunks out of $_GET array, and build a session_id-free uri :)

Code: Select all

mysql_connect(......);
mysql_select_db(......);

$uri = mysql_real_escape_string($_SERVER['REQUEST_URI']);
$query = "UPDATE pagehits SET count=count+1 WHERE uri='$uri'";
mysql_query($query);
And to display the number of hits:

Code: Select all

mysql_connect(.....);
mysql_select_db(.....);

$uri = mysql_real_escape_string($_SERVER['REQUEST_URI']);
$query = "SELECT * FROM pagehits WHERE uri='$uri'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

print_r($row);

Posted: Tue May 10, 2005 7:21 am
by perik
Ok! I will try that script!

But what shall I change in the script?

mysql_connect(......);
mysql_select_db(......);

is that login?

where shall i put the scripts? In content.php?

Sorry for stupid questions but I hardly know anything about coding

thanks

Perik

Posted: Tue May 10, 2005 6:31 pm
by Skara
mysql_connect(host,username,password);
mysql_select_db(name_of_database);

Whenever this script is ran, it will inc the value by one. Therefore place it in the php file you want to count from.