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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!