Niixie Posted April 25, 2011 Share Posted April 25, 2011 Hey PHPFreaks! I have a problem with my login script, because when i login it sets a cookie and it all works. But when i got redirected and refresh the page, my script turns an error which says i'm not logged in? how come? This is what finds the cookie and redirects me if(!isset($_SESSION['auth']) && !isset($_COOKIE['authcookie'])) { $_SESSION['ERROR'] = 7; header('location: index.php?login'); } Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/ Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 Is session_start() somewhere above that point in the script? Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206027 Share on other sites More sharing options...
Niixie Posted April 25, 2011 Author Share Posted April 25, 2011 session_start(); is in the very top, just underneath <?php Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206033 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 I'd start with a <pre>formatted print_r() of both $_SESSION and $_COOKIE after the refresh to begin to sort this out. Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206035 Share on other sites More sharing options...
Niixie Posted April 25, 2011 Author Share Posted April 25, 2011 Can you explain me, please? Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206036 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 On the page you're redirected to, add this and see what values are actually present, and if they are what they're supposed to be. echo '<pre>$_SESSION:'; print_r($_SESSION); echo '$_COOKIE:'; print_r($_COOKIE); echo '</pre>'; EDIT: Actually, I'd probably do this in both scripts. Just comment out the header() redirect temporarily in the first one to prevent it sending you away. Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206042 Share on other sites More sharing options...
Niixie Posted April 25, 2011 Author Share Posted April 25, 2011 Output from before final page $_SESSION:Array ( [ERROR] => -1 [upvalid] => 1 [accountname] => -1 [email] => [email protected] [password] => e [username] => x [auth] => 1 ) $_COOKIE:Array ( [cookie] => Array ( [three] => cookiethree [two] => cookietwo [one] => cookieone ) [phpSESSID] => 61e1a8i0b3uifd0b1o3ejpkb77 ) Output from last page $_SESSION:Array ( [ERROR] => 7 [upvalid] => 1 [accountname] => -1 [email] => [email protected] [password] => e [username] => x ) $_COOKIE:Array ( [cookie] => Array ( [three] => cookiethree [two] => cookietwo [one] => cookieone ) [phpSESSID] => 61e1a8i0b3uifd0b1o3ejpkb77 ) The cookie isn't there, but three other cookies i created to test is there. Don't know how to remove them, there is no time limit on them. Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206047 Share on other sites More sharing options...
Niixie Posted April 25, 2011 Author Share Posted April 25, 2011 Can you modify this so it makes a cookie with a time on 1 hour? setcookie('authcookie', $row[0], time()+3600); <--- My old Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206052 Share on other sites More sharing options...
Niixie Posted April 25, 2011 Author Share Posted April 25, 2011 Nobody? Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206083 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2011 Share Posted April 25, 2011 Can you post the code where you set the cookie? Ken Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206094 Share on other sites More sharing options...
dcro2 Posted April 25, 2011 Share Posted April 25, 2011 Have you tried looking for the cookie in your browser? I know Firefox and Chrome let you look at the full list of cookies you have set. The directory or something might be set differently than you expect, since you don't specify the cookie domain or path. Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206121 Share on other sites More sharing options...
Niixie Posted April 26, 2011 Author Share Posted April 26, 2011 Ken: Can you modify this so it makes a cookie with a time on 1 hour? setcookie('authcookie', $row[0], time()+3600); <--- My old Dcro2: I only looked the way that Pikachu suggested, maybe a path and domain is good to have? Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206388 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2011 Share Posted April 26, 2011 So, are you developing and debugging this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed. I'll guess you are getting a header error that is preventing the setcookie() from working. You also might be changing the path or the host-name/subdomain (www. vs no www. on the URL) in the URL from where the setcookie() statement is at and the cookie is not being sent back to the server from the browser. See the 4th and 5th parameters of the setcookie() statement. You also might have the setcookie() statement inside of some conditional logic that is FALSE and the statement is not being executed. When kenrbnsn asked for the code where you are setting the cookie, he did not mean just the setcookie() statement. He meant all the code that would show when and where you are setting the cookie. Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206390 Share on other sites More sharing options...
Niixie Posted April 26, 2011 Author Share Posted April 26, 2011 So, are you developing and debugging this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed. I'll guess you are getting a header error that is preventing the setcookie() from working. You also might be changing the path or the host-name/subdomain (www. vs no www. on the URL) in the URL from where the setcookie() statement is at and the cookie is not being sent back to the server from the browser. See the 4th and 5th parameters of the setcookie() statement. You also might have the setcookie() statement inside of some conditional logic that is FALSE and the statement is not being executed. When kenrbnsn asked for the code where you are setting the cookie, he did not mean just the setcookie() statement. He meant all the code that would show when and where you are setting the cookie. Answer block 1,2: What can i do to find out if its a header error? Answer block 3: There is no problem where the setcookie() statement is placed, there is a $_SESSION['auth'] aswell, and it's set perfectly. Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206391 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2011 Share Posted April 26, 2011 You should set the error_reporting/display_errors settings in your master php.ini on your development system, so that you don't need to remember to put them into your code or remove them when you put your code onto a live server (display_errors should not be ON for a live server.) Setting them in your master php.ini will also get php to display fatal parse errors (putting the settings into your script won't show fatal parse errors because you script never runs and the lines of code to change the settings are never executed.) Just because you can set $_SESSION variables at one point in your code (just requires that the session_start() worked), does not mean that a setcookie() statement will work at that point in the code. If you output any character(s) to the browser before the setcookie() statement, it will prevent the setcookie() from working. Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206392 Share on other sites More sharing options...
Niixie Posted April 26, 2011 Author Share Posted April 26, 2011 COPY FROM php.ini ; display_errors ; Default Value: On ; Development Value: On ; Production Value: Off ; error_reporting ; Default Value: E_ALL & ~E_NOTICE ; Development Value: E_ALL | E_STRICT ; Production Value: E_ALL & ~E_DEPRECATED Anything needs to be changed? Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206472 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2011 Share Posted April 26, 2011 Those are comments telling you what the suggested values should be. Those aren't the lines that are setting the values. Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206490 Share on other sites More sharing options...
Niixie Posted April 26, 2011 Author Share Posted April 26, 2011 Found this error_reporting = E_ALL display_errors = On Is that it? Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206491 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2011 Share Posted April 26, 2011 Yes. But does a phpinfo statement show those are the actual settings being used by php? Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206501 Share on other sites More sharing options...
Niixie Posted April 26, 2011 Author Share Posted April 26, 2011 phpinfo() says display_errors On error_reporting 30719 Is that answer? Link to comment https://forums.phpfreaks.com/topic/234688-why-does-this-not-set-any-cookie/#findComment-1206585 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.