Have to login twice. PHP sessions and login troubles with Chrome and Opera?

Try adding a sleep(2) before issuing the redirect header; that'll tell you whether it's actually a bug in your code somewhere, or if the session data just isn't being written to file fast enough.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

The problem I am encountering is that for my login form I have to login twice for the session to register properly, but only in Chrome (my version is 4.0.249.89) and Opera (my version is 10.10). Here is the stripped down code that I am testing on: Login Page: session_start(); $_SESSION'user_id' = 8; $_SESSION'user_name' = 'Jim'; session_write_close(); header('Location: my-domain-name.com/'); exit(); Home Page: session_start(); if ( isset($_SESSION'user_id') ) { echo "You are logged in! "; } else { echo "You are NOT logged in!

"; } Logout Page: session_start(); session_unset(); session_destroy(); header('Location: my-domain-name.com/'); exit(); Currently, under a fresh load with no cookies, if I go to my-domain-name.com/login/ it will redirect to the home page and say "You are NOT logged in! " but if I go there again it will say "You are logged in! ".

Any ideas? Thanks for your help. Php session redirect header link|improve this question asked Mar 8 '10 at 23:40Robert234.

Also, here are some relevant php. Ini directives under Bluehost (most settings should be standard, I haven't changed anything): session. Cookie_domain: no value, session.

Cookie_lifetime: 0, session. Cookie_path: /, session. Gc_divisor: 100, session.

Gc_maxlifetime: 1440, session. Gc_probability: 1, session. Name: PHPSESSID, session.

Save_handler: files, session. Save_path: /tmp, session. Use_cookies: On, session.

Use_only_cookies: Off, session. Use_trans_sid: 0 – Robert Mar 8 '10 at 23:59.

Try adding a sleep(2); before issuing the redirect header; that'll tell you whether it's actually a bug in your code somewhere, or if the session data just isn't being written to file fast enough.

While I was testing your suggestion I happened to notice something. I have been testing these pages by going to -domain-name.com/login" rel="nofollow">my-domain-name.com/login, but I happened to try my-domain-name.com on one of the attempts and it worked, regardless of the sleep() value or without it altogether. I can't figure out why this would produce such a result on the non-www url though.

Would it be a php. Ini setting? So would that – Robert Mar 9 '10 at 0:19 1 A-ha.

The default behavior of PHP's session handler is to issue cookies to the exact domain, including subdomain, the visitor used to get to your page. To make the cookies applicable to all of your subdomains (including www), call session_set_cookie_params(0, '/', '. My-domain-name.com'); right before every call to session_start(); (note the dot at the beginning).

Or, if your PHP installation is only serving one top-level domain, then set session. Cookie_domain to . My-domain-name.com in your php.ini.

– Brock Batsell Mar 9 '10 at 0:26 Yes, thank you! That works now. I believe I had even tried setting that directive to my-domain-name.com, without the dot, and it hadn't worked.

Thanks again! – Robert Mar 9 '10 at 0:36.

Your code, as presented, seems that it would cause an infinite redirect loop with the home page calling session_start(), setting the cookie, and instructing the browser to load the home page. Is there some logic missing from the code presented here?

2 session_start() is just the command to initialize PHP's built-in session handler. If a session hasn't been instantiated, it won't redirect to a login page or anything, there just won't be any variables in the $_SESSION global variable. – Brock Batsell Mar 8 '10 at 23:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions