tags:

views:

163

answers:

4

Hi,

I have a script that shows the current URL:

<? function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

       $CurrentPage = curPageURL();
       $_session['pages']=$CurrentPage; 
       print_r($_session['pages']);

     ?>

I cannot work out how to make it display the last 10 pages that have been viewed, any ideas please?

Thanks, B.

A: 

You are constantly overriding $_SESSION['pages']. Create an array and add URLs to that array:

$_SESSION['pages'][] = $CurrentPage;

then check if there are more than 10 items in the array and remove the first item if it does:

if ( Count ( $_SESSION['pages'] ) > 10 )
  Array_Shift ( $_SESSION['pages'] );

then display the URLs

Jan Hančič
A: 

It looks like you are just assigning/replacing the value in the session with $_session['pages']=$CurrentPage;. What you should do is put an array in there.

Here would be my logic:

RecentPages()
{
    if (count of array > 10)
         Remove Top Item from Array
    Push Current Page to Array
    Print Array
}
Daniel A. White
You should check if there are more than 10 items in the array, otherwise you would get the same "problem" he is having now :)
Jan Hančič
A: 

You need to make it into an array:

if (empty($_SESSION['pages']))
    $_SESSION['pages'] = array();

$_SESSION['pages'][] = curPageURL();

$_SESSION['pages'] = array_slice($_SESSION['pages'], -10);
Greg
A: 

I think that the only reason the script knows the last 1 page visited is because the refering address is in the actual HTTP header.

If you want to show the last 10 pages visited on your site specifcally, you could do this with a session variable. Something like:

//Knock the oldest page off when array count gets to 11:
if(array_count($_SESSION["pagehistory"]) > 10) {
    array_shift($_SESSION["pagehistory"]);
}

//Print the list of pages:
if($_SESSION["pagehistory"]) {
   echo "<h2>Page History</h2>";
   echo "<ul>";
   foreach($_SESSION["pagehistory"] as $page) {
      echo "<li>$page</li>";
   }
   echo "</ul>";
}

//Add the current page to the recent list:
$_SESSION["pagehistory"][] = $_SERVER["HTTP_REFERER"];
Anthony