0

I am trying to track the pages a user visits and the day of the visits using session variables. In my pages, I assigned the variables as arrays like this:

session_start();
$_SESSION['page']=array();
$_SESSION['page'][]=$_SERVER['REQUEST_URI'];
$_SESSION['time']=array();
$_SESSION['time'][]=date("m/d/y", time());

I used the following script to print the variables out:

<?php
session_start();
print_r($_SESSION['page']);
print_r($_SESSION['time']);
?>

After I visited several pages, the above script only printed the last page I visited instead of showing all the pages. Can anyone help me troubleshoot please? Thank you.

2
  • you could just let google analytics do it for you Commented May 7, 2013 at 20:21
  • 2
    $_SESSION['page']=array(); Every time you load a page, you are replacing 'page' with a blank array. Commented May 7, 2013 at 20:23

6 Answers 6

5

That's because you initialize the arrays again and again on every page visit. Use something like this instead:

// initialize array only if it not already exists:
if(!is_array($_SESSION['page'])) {
    $_SESSION['page']=array();
}
$_SESSION['page'][]=$_SERVER['REQUEST_URI'];

... 
Sign up to request clarification or add additional context in comments.

Comments

1

You redeclare array each time, thus cleaning it:

$_SESSION['page']=array();

Solution: Check if array exists, add an element.

if (!isset($_SESSION['page'])){
  $_SESSION['page'] = array();
}
$_SESSION['page'][] = "New data" ;

Comments

1

yes, you must do it this way:

if (!isset($_SESSION['time'])) {
   $_SESSION['time'] = array();
}
if (!isset($_SESSION['page'])) {
   $_SESSION['page'] = array();
}
$_SESSION['time'][] = date("m/d/y", time());

otherwise, you just overwrite again and again the 'time' key..

Comments

0

Remove below lines from your code.

$_SESSION['page']=array();
$_SESSION['time']=array();

Above lines are resetting your session variables.

Comments

0

You are re-setting them to empty array each time you save new session:

$_SESSION['page']=array(); // the $_SESSION['page'] now is empty
$_SESSION['page'][]=$_SERVER['REQUEST_URI'];

Comments

0

I would store that kind of information in a DB and call it whenever you need it.

But to answer your exact question about doing it in the session variable, just use something like the following:

session_start();

/****Record visit****/
$newvisit=array();
$newvisit['page']=$_SERVER['REQUEST_URI'];
$newvisit['time']=date("m/d/y",time());

/****Store visit in SESSION****/
if(!empty($_SESSION['visits'])){
    array_push($_SESSION['visits'],$newvisit);
}else{
    $_SESSION['visits'][0]=$newvisit;
}

Like others have mentioned, the main problem with your code is that you were writing over the same session variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.