Eventually, I had determined that there was something about the PHP session variables that was not being set. I had no other problems elsewhere I employed PHP sessions; just the login script. Further investigation revealed that the real issue was with the cookie the PHP session creates.
Important Note: When session_start() is called, a cookie is created and the session id is stored in it. For more on PHP sessions, check out this article from php.net.
Here is how it breaks down:
- The user points their browser to http://url.com/index.php
- The user attempts to log into the site.
- The authentication script calls session_start() and logs the user in, setting a state variable $is_logged_in to true.
- When the script ends, it redirects the user to a page using header('Location: http://www.url.com/somepage.php').
- The $is_logged_in = true in the PHP session does not carry over from the authentication script.
Why?
The PHP session data cannot be accessed by somepage.php because when the authentication script called session_start(), a cookie (learn more about PHP and cookies) was created and assigned to the "http://url.com". When the redirect completes, the current domain is now "http://www.url.com". It is a simple mistake, but devastatingly annoying.
Solution 1: Using a PHP Configuration File
There are a number of ways to remedy this issue. I chose a preventative approach which requires an .htaccess file.
If you have access to (check with your hosting provider if you are unsure) your PHP Configuration File, php.ini, you can add a directive that forces PHP to always issue cookies to the top-level domain and all subdomains. Add the following line to your php.ini file:
session.cookie_domain = .url.com
For more information, read this. Please note the period before the "url.com". It is important. Also, note the lack of single or double quotation marks.
Solution 2: Inline Directive
Instead of having to modify your php.ini file, you can set the cookie domain inline with a particular script using the ini_set() function (see PHP manual). Add the following line to your PHP script: ini_set('session.cookie_domain', '.url.com'); Take note again of the period before "url.com".
Solution 3: .htaccess RewriteRule
This is the method I chose because it is an inherently proactive approach. It requires an .htaccess file and the following code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^url.com [NC]
RewriteRule ^(.*)$ http://www.url.com/$1 [L,R=301]
The first line tells Apache to honor symbolic file links. If you have a file symbolically linked to another file and get a HTTP 403: Forbidden when trying to access it, it may be because your .htaccess file lacks this line. The second line tells Apache to enable URL rewriting (see Apache manual). This is the first step to fixing the matter at hand. The third line is a rewrite condition (see Apache manual). If the condition is met, it executes the following rewrite command on the fourth line. The RewriteCond condition verifies that the base domain is exactly url.com. If the condition is met, line four takes the URI and appends it to the string "http://www.url.com/", forcing the www subdomain.
Problem solved.