Guest convention Posted October 18, 2006 Share Posted October 18, 2006 I have a login script that will set a cookie containing the entered username in the form:[code]if ($username && $password && $submit) { // SQL queries to validate username/password will be added later. setcookie("username",$username,time()+36000,false);}[/code]Which, of course, does not work. I think I tried every single thing imaginable to get to work. This does not work on localhost, but I uploaded the same exact page to my webserver and it worked perfectly. I even added '<?php setcookie("test","asdfg"); ?>' at the very beginning of the page, and it did not work.Please, please help me fix this problem! Thank you very much.[size=8pt]PS: All $_POST variables are declared earlier in the script.[/size] Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/ Share on other sites More sharing options...
Daniel0 Posted October 18, 2006 Share Posted October 18, 2006 Try enabling errors and putting error_reporting(E_ALL); on top of your page to see if it returns any errors. Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110507 Share on other sites More sharing options...
Guest convention Posted October 18, 2006 Share Posted October 18, 2006 [quote author=Daniel0 link=topic=111865.msg453605#msg453605 date=1161156361]Try enabling errors and putting error_reporting(E_ALL); on top of your page to see if it returns any errors.[/quote]Hmm... not getting any errors. :\ Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110513 Share on other sites More sharing options...
Daniel0 Posted October 18, 2006 Share Posted October 18, 2006 Odd. Try this: [code]error_reporting(E_ALL);setcookie("test","this is a test);print_r($_COOKIE);[/code] and tell me what you get. Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110529 Share on other sites More sharing options...
Guest convention Posted October 18, 2006 Share Posted October 18, 2006 [quote author=Daniel0 link=topic=111865.msg453627#msg453627 date=1161159962]Odd. Try this: [code]error_reporting(E_ALL);setcookie("test","this is a test);print_r($_COOKIE);[/code] and tell me what you get.[/quote]All that is output is 'Array ( )'. Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110532 Share on other sites More sharing options...
HuggieBear Posted October 18, 2006 Share Posted October 18, 2006 Try this then...[code]<?phperror_reporting(E_ALL);setcookie("test","this is a test");foreach ($_COOKIE as $k => $v){ echo "$k - $v<br>\n";}?>[/code]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110540 Share on other sites More sharing options...
Daniel0 Posted October 18, 2006 Share Posted October 18, 2006 Well, then you haven't turned errors on. I meant to type this: [code]error_reporting(E_ALL);setcookie("test","this is a test");print_r($_COOKIE);[/code]But what I posted should give an error like this: [code]Parse error: parse error, unexpected $end in *file* on line *line*[/code]Set [tt]display_errors[/tt] in php.ini to [tt]On[/tt] to display errors. Then try with the fixed code (you might have to refresh). Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110541 Share on other sites More sharing options...
xsist10 Posted October 18, 2006 Share Posted October 18, 2006 To set cookies you need to start a session.Put this at the top of your code before cookie creation:[code]<?phpsession_start();?>[/code] Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110549 Share on other sites More sharing options...
HuggieBear Posted October 18, 2006 Share Posted October 18, 2006 [quote author=xsist10 link=topic=111865.msg453647#msg453647 date=1161162066]To set cookies you need to start a session.Put this at the top of your code before cookie creation:[code]<?phpsession_start();?>[/code][/quote]I thought this was the case but when I re-checked the documentation I couldn't find anything about it.RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110558 Share on other sites More sharing options...
Daniel0 Posted October 18, 2006 Share Posted October 18, 2006 To set sessions session_start is required (or session.auto_start must be on in php.ini).It has nothing to do with cookies. Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110561 Share on other sites More sharing options...
xsist10 Posted October 18, 2006 Share Posted October 18, 2006 I'm pretty sure that you need a session ID to be able to create cookies. And you can only get that with session_start.[me=xsist10]off to do some Googling.[/me] Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110572 Share on other sites More sharing options...
Daniel0 Posted October 18, 2006 Share Posted October 18, 2006 Nope. Check [url=http://php.net/setcookie]the manual's examples[/url]. And my example do work on my system, and it is without session_start. Link to comment https://forums.phpfreaks.com/topic/24301-cookies-are-not-setting-on-localhost/#findComment-110577 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.