Reputation: 55
I have 2 files, index.php and content.php ...
I am setting a cookie on the content.php
but when i am trying to retrieve that cookie in index.php
, it says undefined index
...
I dnt know the reason of this error
!
I am using this code for setting the cookie-
$loader = $_GET['id'];
$expire=time()+60*60*24*365;
setcookie("loader", $loader, $expire);
and this for retriving-
if (isset($_COOKIE["loader"])) echo $_COOKIE["loader"];
else echo "no cookie found !";
Please Help Me Guys !
Upvotes: 1
Views: 168
Reputation: 64
if(isset($_REQUEST['id'])) {
setcookie('loader',$_REQUEST['id'],time()+60*60*24*365, '/');
} else {
setcookie('loader','',time()-3600, '/');
unset($_COOKIE['loader']);
}
if(isset($_COOKIE['loader']) && $_COOKIE['loader'] != "") {
echo $_COOKIE['loader'];
} else {
echo "no cookie found !";
}
Upvotes: 1
Reputation: 5784
Edit2:
If you get this error 'Undefined Index' that means that your $_GET['id'] isn't set properly. Make sure you've set the $_GET['id']; when setting the cookie.
$loader = $_GET['id'];
$expire=time()+60*60*24*365;
if(isset($_GET['id'])){
setcookie("loader", $loader, $expire, '/');
}else{
echo 'no cookie set';
Edit:
when you've set the cookie you first have to refresh the page BEFORE you read it. otherwise the cookie isn't sent in the header.
Just add a path to your cookie like this:
$loader = $_GET['id'];
$expire=time()+60*60*24*365;
setcookie("loader", $loader, $expire, '/');
NOTE: I've added the '/'
Hope it helps.
Upvotes: 1