Session code not working as expected

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
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Session code not working as expected

Post by bruceg »

Hello,

I am using the following code to track a session.

Code: Select all

<?php
session_start ();
?>

Code: Select all

<?php
if (!IsSet($_SESSION['visit_count'])) {
echo "<h2>Welcome to Inspired-Evolution.com!</h2>";
  $_SESSION['visit_count'] = 1;
}
else {
 $visit_count=$_SESSION['visit_count'] +1;
 echo "<h2>Welcome Back!  I see you have visited $visit_count times</h2>";
 $_SESSION['visit_count'] = $visit_count;
 }
 
 $self_url = $_SERVER['PHP_SELF'];
 $session_id = SID;
 if (IsSet($session_id) &&
      $session_id) {
      $href ="$self_url?$session_id";
}
else {
$href = $self_url;
}
?>
can be seen in action here:
http://www.inspired-evolution.com/index.php

The problem is if you close the browser window and go to the site again, your treated as a first time visitor, your only tracked if you hit refresh or navigate away and back to the home page. Is this how it is supposed to work? I was wanting some code that would recognize you after you leave completely and come back. Can this be done with PHP?

thanks!
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Sessions are useful to track variables through a user's, well, session... but when the user closes their browser, the session on the server ends and therefore your tracking will end also.

If you are looking to echo out the number of visits, or whatever, should the user return to the site a few days later, you need to to set a cookie on their machine with the relevant info.

Something like this...

Code: Select all

// Set the cookie
set_cookie("hits", $visit_count, time()+60*60*24*30) // A cookie called "hits" with a value of $visit_count, expiring in 30 days

// Retrieve the cookie
echo $_COOKIE["hits"];
// Or
echo $HTTP_COOKIE_VARS["hits"]; // Depending on your PHP version
I'd suggest setting the cookie at the end of the script so that the $_SESSION part runs first. That way, a cookie will be set on the user machine with the correct value. At the beginning of the script, you need to delete an existing cookie before the new one is set:

Code: Select all

setcookie ("hits", "", time() - 3600); // Replaces the cookie with an expired one - in fact, deleting it.
So, when the script begins to run (and after the session_start() function), an existing cookie called "hits" is deleted. Then, the $_SESSION variable for counting hits is set and finally, at the end of the script, a new cookie is set with the correct value in it.

You'll get a more accurate figure, by the way, if you also track the user's IP - at the moment, the index page tracks every visit during that session. Since very few people have a static IP address, your best bet is to only update the cookie if the IP addresses do not match, thereby suggesting that it's a unique visit. Otherwise, you're left with a script that tells the user how many times they've clicked refresh...

Hope this helps!
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

can you or anyone else offer come code help to track an IP address?

My php version is 4.4.1

I received a parsing error with this part of the code

Code: Select all

// Set the cookie 
set_cookie("hits", $visit_count, time()+60*60*24*30) // A cookie called "hits" with a value of $visit_count, expiring in 30 days 

// Retrieve the cookie 
echo $_COOKIE["hits"]; 
// Or 
echo $HTTP_COOKIE_VARS["hits"]; // Depending on your PHP version 
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sessions are destroyed if you close your browser, that's their nature. You can bypass this functionality by making the session an expiring cookie by calling session_set_cookie_params()
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

bruceg wrote:I received a parsing error with this part of the code
My mistake, should be setcookie():

Code: Select all

// Set the cookie 
setcookie("hits", $visit_count, time(+60*60*24*30) // REMOVE THE UNDERSCORE BETWEEN SET and COOKIE!! 

// Retrieve the cookie 
echo $_COOKIE["hits"];
can you or anyone else offer come code help to track an IP address?

Code: Select all

$ip = $_SERVER['REMOTE_ADDR']; // assigns the IP to the variable $ip
Not sure how best you'd go about storing both values together (that is, $visit_count & $ip), but I would consider concatenating them and then doing a eregi() or some such test on the cookie you retrieve to ascertain the presence of the user's current IP.

Hope this helps...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ahem

Code: Select all

setcookie('hits', $visit_count, time() + 60 * 60 * 24 * 30);
mattcooper forgot a few bits. ;)
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

where does this need to go in the code, rg: in the beginning end etc.

Code: Select all

$ip = $_SERVER['REMOTE_ADDR']; // assigns the IP to the variable $ip 
also, do I still need

Code: Select all

<?php
session_start();
?>
thanks for any assistance
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

bruceg wrote:where does this need to go in the code, rg: in the beginning end etc

Code: Select all

$ip = $_SERVER['REMOTE_ADDR']; // assigns the IP to the variable $ip 
.
This can go anywhere, but you'll need to assign the value to the variable before you use any more code that may refer to it.
also, do I still need

Code: Select all

<?php
session_start();
?>
When you are using session variables, you need to call the session_start() function at the beginning of every page that uses them.

Apologies for the incorrect code earlier - cheers for pulling me up on it, Feyd!

Hope this helps
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

this may be a silly question, but what value do I assign to the varaible?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Which value do I assign to the variable?
You don't need to assign anything per-se, I meant that the variable would need to have a value before it was used by any other part of your script.

I would place this at the top of your script so that one of he first things that happens is that the remote address is identified. Then you can carry on with your tracking app...

Hope this helps!
Post Reply