Jump to content
dalawh

Quick question on setcookie

Recommended Posts

Does setcookie work after the script stops? When I make my script print the cookie after I set it, I get a notice saying it is undefined. If I run another script to print the cookies after the script that sets it, it works fine.

Share this post


Link to post
Share on other sites

Depending on how long it's valid, yes. Please post your cookie code so we can make sure in your case.

Edited by niche

Share this post


Link to post
Share on other sites

The cookie exists for as long as you tell it to, but cookies are not available in the script where you set them because cookies are sent with the browser's request and it has already finished the request by the time your script runs. The cookie will be sent on the next request.

Share this post


Link to post
Share on other sites
Depending on how long it's valid, yes. Please post your cookie code so we can make sure in your case.
<?php$first="Booga";$last="Wooga";  setcookie("firstName",$first,time()+2592000); //30 days  setcookie("lastName",$last,time()+2592000); //30 days  $num=0;  while($num!=100){   echo 'First: '.$_COOKIE['firstName'].'<br />';   echo 'Last: '.$_COOKIE['lastName'].'<br />';   echo '----------<br />';   $num++;  }?>

Every echo will give an undefined index. If you run another script after echoing the cookies, it will display it. That was just a hypothesis I came up with when I tested it and I wanted to confirm it.

Share this post


Link to post
Share on other sites

See justsomeguy's post (#3). I missed that you were trying to access your cookie with the script that created it.

Share this post


Link to post
Share on other sites
See justsomeguy's post (#3). I missed that you were trying to access your cookie with the script that created it.
Yea, I saw it. Must of posted before I refreshed.
The cookie exists for as long as you tell it to, but cookies are not available in the script where you set them because cookies are sent with the browser's request and it has already finished the request by the time your script runs. The cookie will be sent on the next request.
Oh okay. Is there a way where I could set the cookie and then use the cookies? I can't do it in the same script for obvious reasons. If I require or include another script, it will be considered as the same script. So is there a way to make one script occur and then the other one automatically?

Share this post


Link to post
Share on other sites
Guest So Called

I'm pretty sure that your cookie will not exist until the visitor makes a subsequent site access where the $_COOKIE array is updated. There are any number of ways to cause the current script to update into a new access. (Search "redirect" for this topic.) An "include" script is not a new script in that it does not create a new site access.

Edited by So Called

Share this post


Link to post
Share on other sites
Is there a way where I could set the cookie and then use the cookies?
no. it is not possible. if its essential to show up the cookie data where it sets you can use redirect header to the same page after cookie is being set. including file will not send other http request unless it is called via URL. so if you set cookie in certain page and include another script via HTTP it will evaluate $_COOKIE array in the same request.

Share this post


Link to post
Share on other sites

You could, at the same time you're setting the cookie value, manually create the variable for use in the rest of the script.

setcookie( ... ...);$_COOKIE['name'] = 'value';

Share this post


Link to post
Share on other sites
Guest So Called
So would using header(location:) work?
Yes, but you can only send that if you have not sent any HTML before it. You could JavaScript up a new request. There's lots of ways.

Share this post


Link to post
Share on other sites
Yes, but you can only send that if you have not sent any HTML before it. You could JavaScript up a new request. There's lots of ways.
There is no HTML involved until the very end. I take you step by step. User fills out a form and presses submit. I use a PHP script to get that information and create a cookie and redirect it (using header) to another script. This new script will log everything.and redirect (using header) to the HTML stuff. Is that fine? Any idea if we can have two separate PHP scripts in one PHP file like...
<?php//CODE?><?php//CODE?>

Share this post


Link to post
Share on other sites
Guest So Called

Yeah, you can do that if it is as simple as you described. I do it myself after user/administrator login. I evaluate the PUT and validate the login form, and then if password is successful (this is all before any output is generated) I'll redirect if login was successful. Otherwise I present the login form again and allow another attempt. You should be able to easily make all that work. Note that one PHP file = one PHP script. What you described is nothing more than a single script. You need a different URL to have a different script. My login.php redirects to index.php if login is successful.

Share this post


Link to post
Share on other sites
Yeah, you can do that if it is as simple as you described. I do it myself after user/administrator login. I evaluate the PUT and validate the login form, and then if password is successful (this is all before any output is generated) I'll redirect if login was successful. Otherwise I present the login form again and allow another attempt. You should be able to easily make all that work. Note that one PHP file = one PHP script. What you described is nothing more than a single script. You need a different URL to have a different script. My login.php redirects to index.php if login is successful.
Okay, guess I will just have to redirect. 3 scripts and 2 redirects. Hope nothing gets more complicated than that.

Share this post


Link to post
Share on other sites

There's no reason to redirect for something like this. Just use a regular variable instead of the $_COOKIE array. At the top of your script, if the cookie was sent, then set the variable to the value from the cookie, or else set the cookie and set the variable to the same value that you just stored in the cookie. Then in your script use the variable instead of accessing the $_COOKIE array, the variable will either contain the value from the cookie or the value you set in the cookie.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...