dongpo5239 2012-10-22 09:45
浏览 38
已采纳

无法使用php读取cookie

I created a php file, order.php which is shown below

<?php
$plan=$_POST['plan'];
$expire= time()+60*60;
if (empty($plan))
{

echo '<p align="center">You did not select any plan.</p>';
}
else
{
$str1= "http://techbr.duoservers.com/hosting-order/?plan=";
$ur=$str1.$plan;
usleep(1500);
setcookie("ur", $ur, $expire);
header( 'Location: http://cheap-webhosting.co.in/signup.php') ;

}
?>

as you can see the script gets the value for the "plan" use that data to create a cookie ur which has the value say http://techbr.duoservers.com/hosting-order/?plan=44 where 44 is the plan value and then redirects to the page signup.php The code for signup.php is given below

<?php
$ur = $_COOKIE['ur']; 
echo "<iframe frameborder='0'";
echo 'src="';
echo $ur;
echo '" width="100%" height="1100px"></iframe>';
?>

For some reason when i pass the order.php file a plan value a cookie is created but the signup.php which loads automatically due to the redirect command in the order.php is not able to read the cookie ur. However when i run a simple <?php print_r($_COOKIE); ?> the cookie ur's correct value is displayed. On top of that if i manually load signup.php the page is loading just fine. But it isn't able to read the cookie when it is loaded automatically by using a redirect command. Any idea what is wrong? I might add that the code used to work just fine some 1 month ago it stopped working all of a sudden

  • 写回答

1条回答 默认 最新

  • dongzhu7329 2012-10-22 09:59
    关注

    You can't read a cookie that has been created for an other url/path :

    You created your cookie only for the directory http://techbr.duoservers.com/hosting-order/ (because you omited the 4th parameter path of the set_cookie function, php choose the current directory, see quote below)

    and try to access it in another one : http://cheap-webhosting.co.in/

    You can't do that.. You'll have to force the cookie path parameter to "/" for example.

    See the php doc :

    path:

    The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

    source: http://php.net/manual/en/function.setcookie.php

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看