Hi, I am trying to set a cookie using a value I am passing via URL. Nothing secretive, just language settings etc. I am able to get the value in a PHP variable but then when I try to set a cookie using this value, I get warning which says," header locations set by home/public.../", and the cookie is not set at all. Please have a look at the code below and let me know what am I doing wrong. Thank you. <?php // if url is domain.com?lan=value& $myvar = $_GET['lan']; setcookie("lansettings", $myvar , time()+360000); // below is just so to check display. echo $myvar; echo "<br> something here <br>"; if(setcookie($_COOKIE["lansettings"])) { echo "Bravo, Cookie set <br><br>"; } else { echo " BOOHOO, Cookie not set <br><br>"; } ?> Please suggest me a solution to get the value from the URL and then place a cookie. Thank you jeet
1. All cookie activity must be done before any text is sent. 2. If $_GET['lan'] is not set $myvar is invalid
you can't check it in the next step. you will be able to check the cookie on the next page load only. also, the status reurned by the setcookie doest not indeicates whether the cookie is accepted or rejected by the user browser. Simply it returns the status of the command execution.
Hi, I removed the cookie set status check. but still the cookie is not being set. Now the only thing I have is : <?php // if URL is domain.com?lan=1252& $myvar = $_GET['lan']; setcookie("lansettings", $myvar , time()+360000); ?> <html><head></head><body> My content here </body></html> But still I am getting a warning message. What am I doing wrong here? Is there an easy way to retrieve the "lan" value from the URL and then put it in a cookie? Thank you jeet
Thanks guys, but it's not working. I will see if I can do it with javascript. There will be a lot of extra lines, but that's fine if it works. What is the use of a cookie function in PHP if it has to go at the very top? One should be able to use it anywhere on the page like other good PHP functions. I am enjoying programming in PHP though! ( just learning.) Again, Thanks for your help. Best wishes jeet
JEET: You just have to understand HTTP to realise why setcookie needs to be run before any other output. The reason is that cookies are sent as page headers, by specification. As soon as you have sent output, your page has started and any and all headers must have been sent. Hopefully that makes sense. I'm surprised the error you're receiving didn't have a bit more information. Normally, PHP reports 2 or 3 lines with these sorts of header problems and one of them says something along the lines of 'output already started at filename.php line number' (or something like that). If you've included a PHP file that outputted anything, that will cause a problem. More importantly (as people don't think about it too much) having whitespace outside of any PHP tags prior to the setcookie call, that will cause a problem. Whitespace includes a new line: this means that if you're including a config file, for example, and you can move your editor cursor below the '?>' line in your editor, you have a new line character there and that will cause a problem...