+1  A: 

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.

Brock Batsell
While I was testing your suggestion I happened to notice something. I have been testing these pages by going to my-domain-name.com/login, but I happened to try www.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
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
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
A: 

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?

sutch
`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