我有一些代码可以显示您最近在我的网站上查看过的网页,但如果您还没有浏览过该网页,我会收到错误
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
我需要它说你还没有看过一个页面而不是得到这个错误 这是我目前的代码
<?php
foreach( $_SESSION['pageurl'] as $key=>$value) {
echo '<a href="'.$value.'">Click here </a>';
echo 'to see last page which is '."'localhost".$value."'".' <br />';
}
?>
任何想法?
答案 0 :(得分:2)
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 />';
}
答案 1 :(得分:2)
在检查会话
之前添加session_startif (!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";
}
希望这会有所帮助:)
答案 2 :(得分:0)
你应该检查是否设置了pageurl,并且在启动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 />';
}
?>