Setting and Retrieving Cookies in the Same Script

This should be easier for some Cookie Expert out there.  

I want to set and retrieve a cookie in the same script.  It's part of a much larger script, but at some point I want to set a cookie, and later in the same sript I want to retrieve it.  

A simple expample of this is the following script:

<?php

setcookie("test",1);
echo $_COOKIE["test"];

?>

This doesn't work.  You have to run it once, and then run it again to read the cookie.  I just want the cookie to be available immediately.  Any suggestions?
LVL 1
dustinbussAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

markdocCommented:
perhaps you could do a redirect after you set it?

<?php

session_start();

if (!isset($_COOKIE["test"]))
{
    setcookie("test", 1);
    header("location: $_SERVER[PHP_SELF]");
}
else
{
    echo $_COOKIE["test"];
}
0
markdocCommented:
i forgot the  ?>  :D
0
dustinbussAuthor Commented:
well, this would solve that example, but the problem is that it's part of a much larger script which can't be redirected in the middle of it.  i am not looking for a workaround; i'm looking for a way to get the cookie to show up  in the same script.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

markdocCommented:
i'm sorry but i think there is really no way of knowing when the cookie is sent to the browser along with the other http headers.  $_COOKIE is an array of cookies already sitting in your browser and if the particular cookie your trying to access is not yet sent to the browser then you can never see it with $_COOKIE.  thus, i think what you're trying to achieve is near to if not impossible without doing a redirect to make sure that the cookie is sent to the browser first. ;)

can't you use session cookies instead? or a combination of browser and session cookies?

how about a soda to match? :) (cookies, cookies-- im getting hungry...)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ixtiCommented:
Create your own function. For example:

<?php

function setMyCookie($cookieName)
{
    $args_num   = func_num_args();
    $args       = func_get_args();
    if ((6 === $args_num) && (setcookie($args[0], $args[1], $args[2], $args[3], $args[4], $args[5])))
    {
        $_COOKIE[$args[0]] = $args[1];
        return true;
    }
    if ((5 === $args_num) && (setcookie($args[0], $args[1], $args[2], $args[3], $args[4])))
    {
        $_COOKIE[$args[0]] = $args[1];
        return true;
    }
    if ((4 === $args_num) && (setcookie($args[0], $args[1], $args[2], $args[3])))
    {
        $_COOKIE[$args[0]] = $args[1];
        return true;
    }
    if ((3 === $args_num) && (setcookie($args[0], $args[1], $args[2])))
    {
        $_COOKIE[$args[0]] = $args[1];
        return true;
    }
    if ((2 === $args_num) && (setcookie($args[0], $args[1])))
    {
        $_COOKIE[$args[0]] = $args[1];
        return true;
    }
    if ((1 === $args_num) && (setcookie($args[0])))
    {
        $_COOKIE[$args[0]] = "";
        return true;
    }
    return false;
}
   
?>
0
missilesiloCommented:
<?php

$_COOKIE["test"] = 1;
setcookie("test", $_COOKIE["test"]);
echo $_COOKIE["test"];

?>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.