PHP works differently on different occasions

Soldato
Joined
27 Dec 2005
Posts
17,316
Location
Bristol
Ok, I've got a strange error. I currently use Google Analytics like most of you I'm sure, which is great, but as I run a very very small business and am lucky to get maybe 10 visitors a day at the moment I wanted something a bit more detailed.

I've created a script that logs information into a database when people visit each page:

If they're a new person (based on their phpsessid not being in the dbb), it logs their ip, sessid, current page, date, time, checks if they've been on before (ip) and gives them an id.

If they're not a new person (phpsessid is already in the database), it just adds the page their on onto their viewing history as long as it's not the same as before, so that in the end you get something like "welcome -> about -> packages -> contact" etc.

Reason I've done this is because it's all well and good seeing that 60% of people exit my site after looking at our prices (which might suggest they're high), but that can be a bit deceptive if before that they looked at the prices, contacted us and then simply went back to bookmark the prices page.

So anyway, the problem is that for some bizarre reason the code tends to work differently every so often. Sometimes it doesn't log the first time they visit, sometimes it does.

The code is as follows (please excuse any messy programming, my theory is as long as it does its' job it's ok :p):

include("db.php");

$a = $_GET['a'];
$session = $PHPSESSID;
$today = date ("d/m/y");
$time = date ("H:i:s");
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];

if($a == ""){
$a = "welcome";
}

$sql = mysql_query("SELECT * FROM sessionlog WHERE session='$session'");
$check = mysql_num_rows($sql);

if($check > 0) {
$row_sessionlog = mysql_fetch_array($sql);
$currenthistory = $row_sessionlog['history'];
$last3 = substr($currenthistory, -3);
$matchcheck = substr($a, -3);
if($last3 == $matchcheck){
} else {
$history = "$currenthistory -> $a";
$query = "UPDATE sessionlog SET history='$history', time2='$time' WHERE session='$session'";
$result = mysql_query($query, $link);
}
} else {

$sql = mysql_query("SELECT * FROM sessionlog WHERE ip='$ip'");
$check = mysql_num_rows($sql);
if($check > 0) {
$row_repeat = mysql_fetch_array($sql);
$repeat = $row_repeat['id'];
}

$query = "INSERT INTO sessionlog VALUES ('', '$ip', '$session', '$a', '$today', '$time', '', '$repeat', '', '', '$ref')";
$result = mysql_query($query, $link);
}

Sorry for the long post. Usually trial and error and Google is my fix, but seeing as this seems to be random I can't figure out where it's wrong. If you need to see the DBB output just ask. Cheers in advance :)
 
Fixed after a lot of playing.

Was two things - my database had an entry in from my early testing where the session field was empty, and so the code would find a match from that when there was no session id.

And also the fact that the code wasn't returning a sessionid if it was the landing page. Changed it now so I'm using my own generated code instead of retrieving the users session id.
 
Back
Top Bottom