Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2013, 06:28 AM   PM User | #1
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,432
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
cookies

in the script below i'm setting a cookie and then trying to print the cookies but I don't see the cookie i set

what am I doing wrong

PHP Code:

<? 
setcookie
('email',"testtest",time() + (86400 365));



print_r($_COOKIE);



?>
esthera is offline   Reply With Quote
Old 02-14-2013, 12:34 PM   PM User | #2
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,229
Thanks: 2
Thanked 190 Times in 188 Posts
djm0219 is on a distinguished road
The cookie will not be available until another page is loaded (or the same page is reloaded).
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 02-14-2013, 12:52 PM   PM User | #3
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,432
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
refreshing didn't help -it's not keeping the cookie - is there anything else i'm doing wrong here?
esthera is offline   Reply With Quote
Old 02-14-2013, 02:00 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
If that's exactly what you have, you have a whitespace before the <?. Cookies are sent via header requests, so if you have output already its impossible to send the headers.
Open your error reporting:
PHP Code:
ini_set('display_errors'1);
error_reporting(E_ALL); 
If it complains of headers already sent on the setcookie call, that'll be your cause.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 02-14-2013, 03:27 PM   PM User | #5
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,432
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
that doesn't help

it is the first line
esthera is offline   Reply With Quote
Old 02-14-2013, 07:16 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by esthera View Post
that doesn't help

it is the first line
This response doesn't really clarify anything since you don't mention whether the first line is the <? or if you are referring to setcookie being the first instruction.
Let me show you what I'm talking about:
Code:
1. 
2. <? 
3. setcookie('email',"testtest",time() + (86400 * 365));
Your code as it is shows a whitespace on line 1. That is output. Its already been mentioned as well that $_COOKIE does not get populated until the next request is performed.

One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.

Finally, the cookie itself doesn't specify a path nor domain. This code all assumes that you are within the same directory. It is also assumed that you are directly attaching to this script and not including it.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 02-14-2013, 09:35 PM   PM User | #7
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,191
Thanks: 217
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Quote:
Originally Posted by Fou-Lu View Post
One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.
Very nice explanation on the whole cookie deal Fou-Lu, but in particular on the short tags which is one reason why every time i see a short tag i replace it with a full tag. The only time i use anything like a short tag is when im displaying something like
Code:
<?=$value;?>
other than that its all full tags. For some reason i have a problem or i just dont like php echo ???? i even try to stay away from php print, i dont know why im a fussy guss i guess lol.

Awhile back i had a tough time with cookie as well, fought it for days till Fou-Lou helped me out, same issue. In that case i pulled the value of the cookie to use on another value when the site loaded.

What i had to do was when the site first loaded i had to hard code a value for the var. Then after that it was fine, because the page was reloaded by the visitor after that and the cookie was set. But at first load of the site if you need that cookie value it is not there yet. Thats just the way it is.

You might have to use a session value for first load and then the cookie after that.
durangod is offline   Reply With Quote
Old 02-15-2013, 12:11 AM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by Fou-Lu View Post
This response doesn't really clarify anything since you don't mention whether the first line is the <? or if you are referring to setcookie being the first instruction.
Let me show you what I'm talking about:
Code:
1. 
2. <? 
3. setcookie('email',"testtest",time() + (86400 * 365));
Your code as it is shows a whitespace on line 1. That is output. Its already been mentioned as well that $_COOKIE does not get populated until the next request is performed.

One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.

Finally, the cookie itself doesn't specify a path nor domain. This code all assumes that you are within the same directory. It is also assumed that you are directly attaching to this script and not including it.
Just to add to Fous cookie explanation..

If you don't understand what request and reply headers are, how cookies are sent and recieved etc, I have a post here which will explain this:
http://www.codingforums.com/showpost...64&postcount=7

Read that, then you will start to understand what Fou is getting at. As you will see, you can set a cookie but until the client sends a new http request, the cookie will not be sent back to the server. If it isn't sent to the server then the $_COOKIE array will not contain your cookie. The $_COOKIE array only contains cookies that have been sent from the client to your server and script - not cookies that you set with setcookie().

Whenever you output *anything* it causes the reply headers to be sent. If you have whitespace as Fou has shown, that will also send the reply headers. Once those headers have been sent, you can not send a cookie or any other header. This is because the headers can only be sent once - otherwise you would have mixed html and headers and the browser would not know what is what. There is another explanation of this in my signature - see the link about headers already sent.
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 02-15-2013 at 12:20 AM..
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 AM.


Advertisement
Log in to turn off these ads.