PHP Session Not Saving
When a session variable vanishes immediately after a request, the immediate reaction is often to assume the server configuration is broken or that session_start() isn't firing fast enough. In your specific scenario, where authentication works perfectly on a local testing environment like eZPHP but fails on production, the root cause is almost certainly a mismatch in how the session cookie path and domain are configured between the two environments.
The code snippet you provided relies heavily on $_SESSION persistence. You include restd.php at the very top of every page:
include("restd.php");
// ... rest of the page logic
Inside restd.php, you guard your application with a session check:
@session_start();
if(isset($_SESSION['id'])) {
// User is logged in
} else {
header("location:index.php");
}
The problem arises when the browser sends a cookie containing PHPSESSID to the server, but the server interprets that request as belonging to a different session than the one where the user just logged in. Consequently, $_SESSION['id'] is empty (or non-existent), triggering the redirect to index.php.
The Cookie Path Mismatch
The most common culprit in production vs. local discrepancies is the default cookie path. By default, PHP sets the cookie path to /. This means the session cookie is sent only for requests starting with that root path. However, if your application logic or server configuration implicitly changes this, or if you are using a subdirectory structure differently on production, the browser might not send the session ID where expected.
Check your php.ini settings or .htaccess files on the production server. Specifically, look for session.cookie_path. If it is set to something other than /, or if your application is deployed in a subfolder (e.g., https://example.com/app/) but the session cookie path is hardcoded to /, the browser will only send the cookie for https://example.com/ requests, not for https://example.com/app/page.php.
Ensure that on your production server:
- The document root matches where your files actually live relative to the URL structure.
- If you are using a subdirectory for your app, explicitly set the session cookie path in
php.inior via.htaccess:
# In .htaccess if Apache is handling this
php_value session.cookie_path "/"
Or programmatically in restd.php before any output:
session_set_cookie_params([
'path' => '/',
'secure' => true, // Only if using HTTPS
'httponly' => true
]);
@session_start();
Domain and Subdomain Isolation
Another frequent issue occurs when production domains differ from local hosts. If your testing server is localhost or a specific IP, and your production domain is example.com, this usually isn't an issue unless you have subdomains involved.
However, if you have a setup like auth.example.com for login and app.example.com for the main site, browsers treat these as separate domains by default. A session cookie set on auth.example.com will not be sent to requests to app.example.com. You must explicitly set the domain attribute in the session parameters to include all subdomains:
session_set_cookie_params([
'path' => '/',
'domain' => '.example.com', // Note the leading dot
'secure' => true,
'httponly' => true
]);
Without the leading dot in domain, the cookie is scoped strictly to auth.example.com and won't travel to app.example.com. This creates a scenario where the user logs in successfully on one subdomain, but when they navigate to another subdomain, the session ID isn't available, causing your isset($_SESSION['id']) check to fail.
Security and Implementation Trade-offs
You mentioned using restd.php at the start of every file to prevent URL guessing. This is a valid security practice known as "session fixation prevention" by ensuring the session starts early. However, placing logic inside an included file can sometimes lead to variable scope issues or premature output buffering if not handled carefully.
Ensure that no whitespace or BOM (Byte Order Mark) exists before the include statement in your main files, and similarly in restd.php. While PHP is forgiving, strict header requirements mean any output (even a space) before session_start() can cause issues depending on server configuration.
Additionally, verify that session.cookie_secure is set appropriately. If your production site uses HTTPS but this setting is false (or vice versa), the browser will refuse to send the session cookie due to security policies, effectively logging the user out on every request from a secure connection.
Finally, check session.save_path. On some shared hosting environments or specific Linux distributions, the default save path might be /tmp, which could have different permissions or cleanup schedules than your local machine. If the directory is full or permissions are restrictive, the session file cannot be written, causing data loss between requests.
If you need a more robust and scalable solution that handles these complexities automaticallyâespecially as your application growsâconsider migrating to a framework like Laravel. It abstracts much of this session configuration logic, managing storage backends (database, Redis) and security defaults in a way that minimizes the risk of environment-specific bugs. You can learn more about secure authentication patterns at https://laravelcompany.com.