I have some code to show the pages you have recently viewed on my website but if you haven't viewed a page yet i get an error which is
Notice: Undefined index: pageurl in C:\xampp\htdocs\project1\recent.php on line 97
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\project1\recent.php on line 97
I need it to say you haven't viewed a page yet instead of getting this error here's the code i have at the moment
<?php
foreach( $_SESSION['pageurl'] as $key=>$value) {
echo '<a href="'.$value.'">Click here </a>';
echo 'to see last page which is '."'localhost".$value."'".' <br />';
}
?>
any ideas?
if(isset($_SESSION['pageurl']))
foreach( $_SESSION['pageurl'] as $key=>$value) {
echo '<a href="'.$value.'">Click here </a>';
echo 'to see last page which is '."'localhost".$value."'".' <br />';
}
Add session_start before you check for session
if (!isset($_SESSION))
session_start ();
if (isset($_SESSION['pageurl'])) {
foreach( $_SESSION['pageurl'] as $key=>$value) {
echo '<a href="'.$value.'">Click here </a>';
echo 'to see last page which is '."'localhost".$value."'".' <br />';
}
} else {
echo "You haven't viewed a page yet";
}
Hope this helps :)
You should check if pageurl is set and is it an array before starting foreach.
<?php
if(isset($_SESSION['pageurl']) && is_array($_SESSION['pageurl']))
foreach( $_SESSION['pageurl'] as $key=>$value) {
echo '<a href="'.$value.'">Click here </a>';
echo 'to see last page which is '."'localhost".$value."'".' <br />';
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.