Forum Moderators: coopster

Message Too Old, No Replies

PHP4/MySQL Help needed on custom hit counter

I need help with a php script of mine

         

jd80

10:28 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



I am trying to implement a program that acts as a hit counter. First, the user gets a session id. After that, the hit counter is incremented once. If that user should return to the page (within 3 hours), then the counter will not be incremented. Here's the php code:

session_start();
header('Cache-control: private'); // IE 6 fix

if(empty($_SESSION[sessID])){
$Pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
."abcdefghijklmnopqrstuvwxyz"
."0123456789";
$lastChar = strlen($Pool) - 1;

for($i = 0; $i < 16; $i++){
$_SESSION[sessID] .= $Pool[mt_rand(0, $lastChar)];
}

// insert session into db
$dbLink = mysql_pconnect("localhost", "user", "password");
mysql_select_db("SessionStore", $dbLink);

$Query = "INSERT INTO tblSessions(fldSessID, fldLastAction) "."VALUES('$_SESSION[sessID]', 'now()')";

if(!$dbResult = mysql_query($Query, $dbLink)){
$error = "couldn't insert session";
}

header('Location: home.php');
}
else{
// connect and select db
$dbLink = mysql_pconnect("localhost", "user", "password");
mysql_select_db("SessionStore", $dbLink);

// get rid of old sessions first
$Query = "DELETE FROM tblSessions "
."WHERE fldLastAction < '"
. date("Y-m-d H:i:s", (time() - 10800))
."'";

if(!$dbResult = mysql_query($Query, $dbLink)){
$error = "error... could not delete old session records";
}

// check to see if session is already in table
$Query = "SELECT fldSessID "
."FROM tblSessions "
."WHERE fldSessID = '$_SESSION[sessID]'";

if($dbResult = mysql_query($Query, $dbLink)){
//do nothing;
}
else{//session is new
// connect and select db
$dbLink = mysql_pconnect("localhost", "user", "password");
mysql_select_db("GYHitCounters", $dbLink);

//get hit counter for page;
$Query = "SELECT fldHome "
."FROM tblHitCounters";
$dbResult = mysql_query($Query, $dbLink);

$hits = mysql_result($dbResult,0);
$hit++;

// update fldPage
$Query = "UPDATE tblHitCounters "
."SET fldHome = '$hits'";
mysql_query($Query, $dbLink);
}

Any help is appreciated and will help this newbie 8)

Thanks,

John

Timotheos

11:05 pm on Feb 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi John,

...and the problem is?

Tim

jd80

12:14 am on Feb 21, 2004 (gmt 0)

10+ Year Member



The problem is that the sessID is not stored in the database for some reason. But echoing $_SESSION[sessID] comes up with the value. Why isn't it inserted into the database? (Sorry for omitting the problem!)

John

Timotheos

12:40 am on Feb 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok...

Is this line
$hit++;
suppossed to be
$hits++;

So I take it this is included in every page put somehow it's stopping the output. Does your html source show you anything?

jd80

6:52 am on Feb 21, 2004 (gmt 0)

10+ Year Member



Timotheos, good call on the "s". I rearranged some things... I've got it so that my database is updated counter and all only when one first views the page! However, I can't seem to get $hits to print... here's the updated code (by the way, I checked the source also and it gives me no leads :( ):


<?php
session_start();
header('Cache-control: private');

if(empty($_SESSION['sessID'])){
$Pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
."abcdefghijklmnopqrstuvwxyz"
."0123456789";
$lastChar = strlen($Pool) - 1;

for($i = 0; $i < 16; $i++){
$_SESSION['sessID'] .= $Pool[mt_rand(0, $lastChar)];
}

header("Location: ["...] . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/SessionTestE.php");
}

if(isset($_SESSION['sessID'])){
// connect and select db
$dbLink = mysql_pconnect("localhost", "user", "password");
mysql_select_db("SessionStore", $dbLink);

// get rid of old sessions first
$Query = "DELETE FROM tblSessions "
."WHERE fldLastAction < '"
. date("Y-m-d H:i:s", (time() - 10800))
."'";
mysql_query($Query, $dbLink);

// check to see if session is already in table
$Query = "SELECT fldSessID "
."FROM tblSessions "
."WHERE fldSessID = '$_SESSION[sessID]'";
$dbResult = mysql_query($Query, $dbLink);
$row = mysql_result($dbResult,0);

if(!$row){
// insert session into db
mysql_select_db("SessionStore", $dbLink);

$Query = "INSERT INTO tblSessions(fldSessID, fldLastAction) "
."VALUES('$_SESSION[sessID]', now())";
mysql_query($Query, $dbLink);

// select db
mysql_select_db("GYHitCounters", $dbLink);

//get hit counter for page;
$Query = "SELECT fldHome "
."FROM tblHitCounters";
$dbResult = mysql_query($Query, $dbLink);

$hits = mysql_result($dbResult,0);
$hits++;

// update fldPage
$Query = "UPDATE tblHitCounters "
."SET fldHome = '$hits'";
mysql_query($Query, $dbLink);
}
}

echo "hits= $hits<br />";
?>

What I really don't get is why if the database counter is updated fine, the $hits variable wont echo? Sorry about the indentation... can't seem to get the pre tags to work.

John

Timotheos

8:01 am on Feb 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Uh... I can't see anything wrong with it either.

jd80

8:33 am on Feb 21, 2004 (gmt 0)

10+ Year Member



I got it working now... I replaced all occurences of "$hits" with "$_SESSION[hits]" and so now it works 8)

John