我正在设计一个房地产网站我在我的网站上有很多广告,当用户点击某个广告时,它会转到另一个页面viewmore.php
,这会向用户提供有关该特定广告的更多详细信息。
现在,正如您在viewmore.php
文件中看到的,我在Cookie中保存了广告的ID,并将广告的ID发送到收藏页面,用户可以随时查看该帖子页。
问题:
考虑到我访问此页面localhost/viewmore.php?ID=10
,因此当您转到收藏页面时,您会看到属于此ID的广告数据,但是当您访问其他广告(例如localhost/viewmore.php?ID=11
)时,您会转到收藏页面您会看到属于id=11
的广告数据,之前的添加消失了。我想将它们保存在我最喜欢的页面中,或者事实上保存我访问的所有帖子。
我该怎么做?
//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>
答案 0 :(得分:1)
将ID作为数组存储在您的Cookie中,例如,您必须将serialize()/unserialize()
或json_encode()/json_decode()
数组放入Cookie中,因为它只支持文本而非复杂数据
<?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>
现在输入的最后一个cookie我假设您将在查询中使用,所以只需获取
<!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";
?>