Sho Gum Lew
Sho Gum Lew

Reputation: 339

The Cookie set is not detected

Hi guys i set a cookie using my script known as cookieset.php

setcookie("atid", $atid, time() + 60 * 60 * 24 * 365, "/", ".mydomain.com");

and it is shown in the browser

Name    atid
Content 1234

but when i try to retrieve it like this from another script

echo 'value is: ' . $_COOKIE['atid'];

it gives the error saying

undefnied index: atid in.........

can anybody help me over this

Upvotes: 1

Views: 377

Answers (3)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27202

The problem (from the error), seems to be that $_COOKIE['atid'] (depending on what line the error is at) is undefined - that means it has not been set, and seeing that you actually set the cookie, I am saying that its the atid that is undefined. Make sure you are getting it, check with isset()

Try this :

if (isset($_COOKIE['atid'])) {
    echo $_COOKIE['atid'];
} else {
    echo "No cookie Set";
}

And one more point :

it won't be available until the next page load or by doing the page request again.

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

Upvotes: 0

user3778428
user3778428

Reputation:

setcookie("atid",$atid,time()+315360,"/");


// use

if (isset($_COOKIE['atid'])) {
    echo "cookeies set ";
} else {
    echo "cookeies not set ";
}

use mozila firebug / cookies to see cookies file

Upvotes: 2

motanelu
motanelu

Reputation: 4025

The $_COOKIE array is populated with information sent from the browser. The first time you request the file that calls setcookie - cookieset.php - the server sends the cookie to the browser, but at this time the $_COOKIE array was already populated without the cookie you just set. The cookie will be available in subsequent requests until it expires.

To see the cookie in PHP, just do like this.

if (isset($_COOKIE['atid'])) {
    echo 'Cookie found with value ' . $_COOKIE['atid'];
} else {
    setcookie('atid', $atid, time() + 60 * 60 * 24 * 365, "/", ".mydomain.com");
    echo 'Cookie was set. Please refresh to see it working';
}

Upvotes: 0

Related Questions