问题描述
我在设计一个房地产网站,在我的网站上有很多广告,当用户点击某个广告时,它会转到另一个页面 viewmore.php
有关该特定广告的详情。
现在,您可以在 viewmore.php
文件中看到我将Cookie中的广告ID保存到Cookie中,页面并且用户可以在他或她在喜爱的页面中想要的任何时间审阅该帖子。
问题:
考虑到我访问此页面 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天$ b $ b?>
< 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
echoprice;
echoroom;
echodate;
?>
< / 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
echoprice;
echoroom;
echodate;
?>
< / body>
< / html>
存放的ID在你的cookie一个这样的数组例如,你必须连载()/反序列化()
或 json_encode()/ json_decode()
的数组该cookie因为它仅支持文本,而不是复杂的数据
< PHP
$ ID = is_numeric($ _ GET ['ID']) ? $ _GET ['ID']:1;
$ cookie_name =favoritepost;
如果(使用isset($ _ COOKIE [$ cookie_name])){
$ =库基反序列化($ _ COOKIE [$ cookie_name]);
} else {
$ kookie = array();
}
if(!in_array($ ID,$ kookie)){
$ kookie [] = $ ID;
}
setcookie($ cookie_name,serialize($ kookie),time()+(86400 * 30),/); // 86400 = 1天$ b $ b?>
< html>
现在输入的最后一个cookie假设将是您要在查询中使用的那个,所以只是get
<!doctype html>
< html>
< body>
<?php
$ cookie_name =favoritepost;
include(config.php);
if(!isset($ _ COOKIE [$ cookie_name])){
echo'NO COOKIE SET';
exit;
}
$ kookie = unsserialize($ _ 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'];
echoprice;
echoroom;
echodate;
?>
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>
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";
?>
这篇关于在PHP中保存具有相同名称但值不同的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!