Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Setting and Retrieving Cookies in the Same Script

Posted on 2006-04-12
8
Medium Priority
?
173 Views
Last Modified: 2011-04-14
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?
0
Comment
Question by:dustinbuss
6 Comments
 
LVL 4

Expert Comment

by:markdoc
ID: 16442077
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
 
LVL 4

Expert Comment

by:markdoc
ID: 16442079
i forgot the  ?>  :D
0
 
LVL 1

Author Comment

by:dustinbuss
ID: 16442853
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
markdoc earned 200 total points
ID: 16443096
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
 
LVL 6

Expert Comment

by:ixti
ID: 16443386
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
 
LVL 3

Expert Comment

by:missilesilo
ID: 16458557
<?php

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

?>
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question