douyanning3724 2016-01-19 22:31
浏览 55
已采纳

如何使用php管理cookie

I'm setting two cookies in a page called reservation.php where i read the content of two input through $POST method,

if (isset($_POST['chambre']) && isset($_POST['option_chambre'])  )
{
setcookie('preference',$_POST['option_chambre'],time()+3600*24*31,"http://127.0.0.1/partie_3_f/accueil.php",null, false, true);
setcookie('type',$_POST['chambre'],time()+3600*24*31,"http://127.0.0.1/partie_3_f/accueil.php",null, false, true);       
}

Then in other page i'm trying to check the DB and show the results where the value of two colums will have the same value as the one stocked in the cookies

<?php 
{
$con = mysqli_connect('localhost','root','root','pdxhotel');
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$type=$_COOKIE['type'];
$pre = $_COOKIE['preference'];
$soal = $con->query("select * from chambre where vide = 1 and   type_chambre='".$type."' and preference = '".$pre."'");
if (mysqli_num_rows($soal)==0)
{
    echo "pas de chambre disponible";
}
else
{ 
?>
<div id="titres" align="center">
<label >----------------------------</label>
<h2 id="tit"> Resultat de recherche </h2>
<label >----------------------------</label>
</div>
<?php 
for ($i = 0; $i<mysqli_num_rows($soal); $i++)
{
?>
    <div id="offre1" class="offre">
    <img src="Images/chambre3.jpg" width="100%" height="50%" />
    <p  > <?php echo "Chambre de type",$_COOKIE['type'],"and qui vérifie la condition:",$_COOKIE['preference']?> </p>
 </div>
<?php
}
}             
}   
?>

I'm having a problem Notice: Array to string conversion ! It's my first time to work with cookies can you please tell me what's wrong and what should i do ?

  • 写回答

1条回答 默认 最新

  • dongwan0574 2016-01-20 17:33
    关注

    The contents of $_SERVER['HTTP_COOKIE'] are the key to this issue:

    "PHPSESSID=vmejgnbc0ptq893eo7fei9sgc7; preference[$id]=1; type[$id]=a; preference=1; type=a"
    
    • PHPSESSID: this is is your php session id, this is fine

    • preference=1; type=a: these are the cookies set by your code in the OP because their the keys are set with plain text.

    • preference[$id]=1; type[$id]=a: these keys cause the issue. First of all, these seem to be duplicate of preference and type, put the bigger issue is that they have a [$id] in their names. When php parses the cookie string into the $_COOKIE array, these values are interpreted as arrays themselves with $i as their key. When you try to read the preference and type keys as strings to insert their values into the sql query string, you get the "Notice: Array to string conversion" message.

    Since we cannod debug your code, we cannot determine from where these duplicate and incorrect keys are coming from. My first guess is that these cookies are remnants of an earlier test. Try to remove them in the browser used for testing. If they reappear again, then you need to debug your code an establish where you set additional cookies.

    My other suggestion would be to check if you could use session or get parameters in the url instead of cookies for passing these variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上