Visitor stats

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
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Visitor stats

Post by therat »

I want to record the refering site to a db to see where people are coming from. I am using this to find the referer

Code: Select all

<?php
$refer   = $_SERVER['HTTP_REFERER'] ;
?>
and this to store the info to the database, which works as expected.

Code: Select all

<?php
if ( !isset($refer) == true) {$refer = 'Bookmark or Other'; }

$refer = "INSERT INTO stats_referer(referer) VALUES ('$refer')";

$result = mysql_query($refer, $skinz);
?>
This will currently create a new line in the database even if the referer already exists. How do I alter this to update a column called hits if the referer already exists in the database
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

first try to update the row with a hits=hits+1 statement
if this affects no row, insert a new one with an initial value of 1 for hits.
You might have to lock the table
or make the referer field unique and test wether INSERT was successful (if not update again)

see also:
http://www.mysql.com/doc/en/UPDATE.html
http://php.net/mysql_affected_rows


p.s.
what you're trying to do is exactly why my browser does not send any referer information to another site ;)
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Thanks for the links. Ill give those a try
Post Reply