i want to create a way to save the urls of recently viewed web pages in a session so when a user uses my site they can go on their account to see what they have looked at so far i have this
<?php
session_start();
$currentpageurl = $_GET['username'];
$_SESSION['pageurl'][] = $_SERVER['REQUEST_URI'];
foreach($_SESSION['pageurl'] as $key=>$value) {
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
?>
the problem is it just keeps going and doesn't stop when i need it to stop at the 10th last viewed pages and remove the oldest one when a new page is viewed
Use array_shift to remove the latest value if your array becomes longer than 10.
<?php
session_start();
$currentpageurl = $_GET['username'];
$_SESSION['pageurl'][] = $_SERVER['REQUEST_URI'];
if( count( $_SESSION['pageurl'] ) > 10 ){
array_shift( $_SESSION['pageurl'] );
}
foreach( $_SESSION['pageurl'] as $key=>$value) {
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
?>