PHP实现cookie浏览历史纪录

1.思路:用户查看物品id的时候,记录查看物品id,以及物品的其他信息,[以ID-物品信息,ID-物品信息]格式存入cookie

数据库Helper:mysqlHelper.class.php

<?php

/*
    @author:luowen
    @time:2013-12-17
    @desc:mysql helper
 */

class MysqlHelper{
    private static $instance = null;
    private $host = null;
    private $username = null;
    private $password = null;
    private $database = null;
    private $conn = null;

    final private function __construct($conf){
        $this -> host = $conf['host'];
        $this -> username = $conf['username'];
        $this -> password = $conf['password'];
        $this -> database = $conf['database'];
        $this -> conn();
        $this -> query("use " . $this -> database);
    }
    private function __clone(){}
    private function conn(){
        $this -> conn = mysql_connect($this -> host,$this -> username, $this -> password);
        if(!$this -> conn){
            die(mysql_error());
        }
    }
    public static function getInstance($conf){
        if(!(self::$instance instanceof self))
            MysqlHelper::$instance = new self($conf);
        return MysqlHelper::$instance;
    }

    public function getOne($sql){
        $source = $this -> query($sql);
        $resSource = null;
        if($row = mysql_fetch_assoc($source)){
            $resSource = $row;
        }
        $this -> freeSource($source);
        return $resSource;

    }

    public function getAll($sql){
        $source = $this -> query($sql);
        $resSource = array();
        while($row = mysql_fetch_assoc($source))
        {
         $resSource[] = $row;
        }
        $this -> freeSource($source);
        return $resSource;
    }

    public function query($sql){
        return mysql_query($sql,$this -> conn);
    }
    
    public function close(){
        mysql_close($this -> conn);
    }

    private function freeSource($resource){
        mysql_free_result($resource);
    }

}

 2,数据库配置文件db.inc.php

<?php

/*
    db configure file
 */

$conf = array(
    "host" => "localhost",
    "username" => "root",
    "password" => "luowen",
    "database" => "test"
);

  3,数据表模型luowenModel.class.php

<?php

    /*
        luowen表模型
     */

class LuowenModel{
    private $tableName = "luowen";
    private $dbInstance = null;
    public function __construct($db){
         $this -> dbInstance = $db;
    }
    public function getOne($id){
        $sql = "select * from " . $this -> tableName . " where id = " . $id;
        $res = $this -> dbInstance -> getOne($sql);
        return $res;
    }

    public function getAll(){
        $sql = "select * from " . $this -> tableName;
        return $this -> dbInstance -> getAll($sql);
    }
}

  4,首页index.php

<?php

/*
    @author:luowen
    @time:2013-12-17
    @desc:web view history store
 */
include_once("./db.inc.php");
include_once("./mysqlHelper.class.php");
include_once("./luowenModel.class.php");

$db = MysqlHelper::getInstance($conf);
$luowenModel = new LuowenModel($db);
//获取所有数据
$res = $luowenModel -> getAll();


?>

<html>
    <head> 
    <title>index</title>
    </head>
<body>
    <table cellspacing = 0 cellpadding=0 style='border-collapse:collapse;' border = '1'>
    <thead>
        <th>Id</th>
        <th>Username</th>
        <th>Age</th>
        <th>Date</th>
        <th>CreateTime</th>
        <th>Detial</th>
    </thead>
    <tbody>
<?php foreach($res as $k => $v){ ?>
    <tr>
    <?php foreach($v as $val){ ?>
        <td><?php echo $val; ?></td>
    <?php } ?>
    <td><a href="./detial.php?id=<?php echo $v["id"] ?>">查看详情</a></td>
</tr>
<?php } ?>
        </tbody>
<table>
<h1>浏览历史</h1>
<table border="1"cellpadding=0 cellspacing=0 style="border-collapse:collapse;">
<thead>
    <th>ID</th>
    <th>Name</th>
</thead>
<tbody>

<?php 
//获取cookie里面存的数据 格式为:ID1-name1,ID2-name2
@$list = $_COOKIE['viewHistory'];
$viewArr = array_reverse(explode(',',$list));
foreach($viewArr as $val){ ?>
<tr>
    <td><?php echo substr($val,0,strpos($val,'-'));?></td>
    <td> <?php echo substr($val,strpos($val,'-')+1,strlen($val));?></td>
</tr>
<?php } ?>
</tbody>
</table>
<hr/>
</body>
    
</html>

  5,物品详细页

<?php
include_once("./db.inc.php");
include_once("./mysqlHelper.class.php");
include_once("./luowenModel.class.php");

$db = MysqlHelper::getInstance($conf);
$luowenModel = new LuowenModel($db);

@$id = $_GET['id'];

if(!is_numeric($id)){
    echo "ID错误!";
    echo "<br/><a href='./index.php'>返回首页</a>";
    return;
}

$res = $luowenModel -> getOne($id);
if($res == null)
{
    echo "没有此数据!";
    return;
}
    $name = $res['name'];
    buildCookie($id, $name);
//构造history历史记录
function buildCookie($id,$name){
    $viewArr = array();
    @$viewBooks = $_COOKIE['viewHistory'];
    if(!isset($viewBooks)){
        setCookie("viewHistory",$id.'-'.$name,time()+3600);
        return;
    }
    $list = explode(',',$viewBooks);
    foreach($list as $key => $val){
      
        $flag = isInArr($list,$val,$key,$id,$name);
        if($flag)break;
    }
    if(!$flag){
        if(count($list) >= 10){
            unset($list[0]);
            array_push($list,$id.'-'.$name);
        }else{
            array_push($list,$id.'-'.$name);
        }
    }
    $resHistory = implode(',',$list);
    //$trimHistory = substr($resHistory,0,strlen($resHistory));

    setCookie("viewHistory",$resHistory,time()+1000);
    return;
  }
//判断是否在数组中
function isInArr(&$list,$val,$key,$id,$name)
{
          if($val == $id.'-'.$name){
            unset($list[$key]);
            array_push($list,$id."-".$name);
            return true;
        }
}
?>
<html>
    <head>
        <title>detial </title>
    </head>
<body>
    <h1>detial</h1>
    <hr/>
    <table border='1' cellspacing=0 style="border-collapse:collapse;" cellpadding='0'>
    <thead>
    <th>Id</th>
    <th>name</th>
    <th>age</th>
    <th>date</th>
    <th>createdate</th>
    </thead>
    <tbody>
        <tr>
            <?php   foreach($res as $val){ ?>
        <td><?php echo $val; ?></td>
    <?php } ?>
</body>

</html>

  

posted @ 2013-12-18 10:33  arvim  阅读(775)  评论(0编辑  收藏  举报