Jump to content

Why does this not set any cookie?


Niixie

Recommended Posts

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
Share on other sites

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
Share on other sites

Output from before final page

 

$_SESSION:Array
(
    [ERROR] => -1
    [upvalid] => 1
    [accountname] => -1
    [email] => example@yourdomain.com
    [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] => example@yourdomain.com
    [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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.