利用cookie来写计时器和历史记录(简略)

计时器:

    

if(!isset($_COOKIE['num'])){

    $num = 1;

   

}else{

    $num = $_COOKIE['num']+1;

    

}

 setcookie('num',$num);


echo '你这是第',$num,'次来';

        

浏览历史记录:

$url = $_SERVER['REQUEST_URI'];

$id = isset($_GET['id'])?$_GET['id']:0;


if(!isset($_COOKIE['history'])){

    $his[] = $url;

}else{

    $his = explode('|',$_COOKIE['history']);

    array_unshift($his,$url);

    $his = array_unique($his);


    if(count($his) > 10){

        array_pop($his);

    }

}


setcookie('history',implode('|',$his));



?>


<p><a href="history.php?id=<?php echo $id-1 ?>">上一页</a></p>

<p><a href="history.php?id=<?php echo $id+1 ?>">下一页</a></p>

<h3>历史记录</h3>

<ul>

<?php foreach($his as $k => $v){ ?>

<li><?php echo '<a href="',$v,'">',$v,'</a>'; }?></li>

</ul>



你可能感兴趣的:(PHP,Web)