save cookies with same name but different values in php -
i designing real estate website have many ads in website , when user click on ad goes page viewmore.php
gives user more details ad.
now see in viewmore.php
file save ad's id in cookies , send ad's id favorite page , user can review post time or wants in favorite page.
the problem:
consider visit page localhost/viewmore.php?id=10
when go favorite page u see ad data belong id when visit ad localhost/viewmore.php?id=11
, go favorite page see ad data belong id=11
, previous add gone. want save both of them in favorite page or matter of fact save posts visit.
how can 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." 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." 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." 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 id's array in cookie example, have serialize()/unserialize()
or json_encode()/json_decode()
array cookie supports text , 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 last cookie entered assume 1 want use in query
<!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 * $db_table 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"; ?>
Comments
Post a Comment