Rick Roy
Rick Roy

Reputation: 1738

Trouble reading a cookie using php

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

Upvotes: 0

Views: 96

Answers (1)

Jscti
Jscti

Reputation: 14450

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

Upvotes: 2

Related Questions