Home > Backend Development > PHP Tutorial > 用cookie显示浏览历史时, unserialize出错解决思路

用cookie显示浏览历史时, unserialize出错解决思路

WBOY
Release: 2016-06-13 13:37:42
Original
1038 people have browsed it

用cookie显示浏览历史时, unserialize出错

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php $id = $_GET['id']+0;

echo '<a href="history.php?id=' . ($id-1) . '">上一页<br>';
echo '<br><a href="history.php?id='%20.%20(%24id+1)%20.%20'">下一页</a>';

    

if(isset($_COOKIE['his'])) {
    //echo $_COOKIE['his'];exit;
    $data = unserialize($_COOKIE['his']);
    $url = $_SERVER['REQUEST_URI'];
    $data[] = $url;
    $data = array_unique($data);
    
   
    if(count($data) >10) {
        array_shift($data);
    }
    
    $urls = serialize($data);
    setcookie('his',$urls,time()*2*24*3600);

} else {
    $url = $_SERVER['REQUEST_URI'];
    $data[] = $url;
    $urls = serialize($data);
    setcookie('his',$urls,time()*2*24*3600);
}


?>

<br><br>

Copy after login
  • 浏览历史

在浏览器中打开并在地址栏上输入id后,浏览器给了一个提示
HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
Notice: unserialize() [function.unserialize]: Error at offset 9 of 41 bytes in D:\server\apache\www\20120413\history.php on line 16

Copy after login

而且显示的历史记录也就一条,我想可能和上面的错误有关,可找不到解决的办法,语法上我看了几片,暂时没发现上面错误,望达人解惑!!!!!!

------解决方案--------------------
$data = unserialize(stripslashes($_COOKIE['his']));

这样试试,,不对打印出cookie值贴上来,
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template