PHP counter ... please help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
perik
Forum Newbie
Posts: 6
Joined: Tue May 10, 2005 5:18 am

PHP counter ... please help!

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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);
perik
Forum Newbie
Posts: 6
Joined: Tue May 10, 2005 5:18 am

Post 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
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.
Post Reply