draw62188 2016-07-11 12:34
浏览 40
已采纳

在php中保存具有相同名称但不同值的cookie

I am designing a real estate website i have many ads in my website and when user click on a certain ad it goes to another page viewmore.php which gives user more details about that certain ad.

Now as you see in viewmore.php file I save ad's id in cookies and send ad's id to the favorite page and user can review that post any time he or she wants in favorite page.

The problem:

consider that i visit this page localhost/viewmore.php?ID=10 thus when you go to favorite page u see the ad data which belong to this id but when you visit another ad like localhost/viewmore.php?ID=11 and you go to the favorite page you see the ad data which belong to id=11 and previous add is gone. i want to save both of them in my favorite page or as a matter of fact save all the posts i visit.

how can i do that?

//reviewmore.php
<!doctype html>
<?php
(is_numeric($_GET['ID'])) ? $ID = $_GET['ID'] : $ID = 1;
?>
<?php 
$cookie_name = "favoritepost";
$cookie_value ="$ID";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
  <body>
    <?php
 error_reporting(0);
include("config.php");
(is_numeric($_GET['ID'])) ? $ID = $_GET['ID'] : $ID = 1;
$result = mysqli_query($connect,"SELECT*FROM ".$db_table." WHERE idhome = $ID");
?>
<?php
 error_reporting(0);
include("config.php");
(is_numeric($_GET['ID'])) ? $ID = $_GET['ID'] : $ID = 1;
$result = mysqli_query($connect,"SELECT*FROM ".$db_table." WHERE idhome = $ID");
?>
<?php $row = mysqli_fetch_array($result):
$price=$row['price'];
$rent=$row['rent'];
$room=$row['room'];
$date=$row['date'];
?>
<?php 
echo"price";
echo"room";
echo"date";
?>
</body>
  </html>

favoritepage.php

<!doctype html>
<html>
  <body>
 <?php 
$cookie_name = "favoritepost";
?>
<?php
 error_reporting(0);
include("config.php");
$result = mysqli_query($connect,"SELECT*FROM ".$db_table." WHERE idhome = $_COOKIE[$cookie_name]");
?>
<?php $row = mysqli_fetch_array($result):
$price=$row['price'];
$rent=$row['rent'];
$room=$row['room'];
$date=$row['date'];
?>
<?php 
echo"price";
echo"room";
echo"date";
?>
    </body>
  </html>
  • 写回答

1条回答 默认 最新

  • duanlu0075 2016-07-11 12:47
    关注

    Store the ID's as an array in your cookie like this for example, you have to serialize()/unserialize() or json_encode()/json_decode() the array into the cookie as it only supports text and not complex data

    <?php
    $ID = is_numeric($_GET['ID']) ? $_GET['ID'] : 1;
    
    $cookie_name = "favoritepost";
    
    if ( isset($_COOKIE[$cookie_name]) ) {
        $kookie = unserialize($_COOKIE[$cookie_name]);
    } else {
        $kookie = array();
    }
    if ( ! in_array($ID, $kookie) ) {
        $kookie[] = $ID;
    }
    
    setcookie($cookie_name, serialize($kookie), time() + (86400 * 30), "/"); // 86400 = 1 day
    ?>
    <html>
    

    Now the last cookie entered I assume will be the one that you want to use in your query so just get

    <!doctype html>
    <html>
    <body>
    <?php
    $cookie_name = "favoritepost";
    include("config.php");
    if ( ! isset($_COOKIE[$cookie_name]) ) {
        echo 'NO COOKIE SET';
        exit;
    }
    
    $kookie = unserialize($_COOKIE[$cookie_name]);
    $ID = $kookie[count($kookie)-1];
    $result = mysqli_query($connect,"SELECT * FROM $db_table WHERE idhome = $ID");
    
    $row = mysqli_fetch_array($result):
    $price=$row['price'];
    $rent=$row['rent'];
    $room=$row['room'];
    $date=$row['date'];
    
    echo"price";
    echo"room";
    echo"date";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看