PHP session handling errors

PHP session handling errors

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Session Handling Errors: Why Your Data Disappears After a Redirect

The symptom you are experiencing—$_SESSION appearing as an empty array Array ( ) despite setting a value immediately before a redirect—is almost certainly not a logic error within your application code. It is a permissions issue on the server side, specifically regarding where PHP stores session data.

In your send.php, you correctly initialize output buffering with ob_start() and start the session using @session_start(). You then assign $_SESSION['id'] = $id; before issuing the redirect. The fact that you are using the suppression operator @ here is a common pattern to ignore "headers already sent" warnings if there is stray whitespace or BOM in your file, but it does not hide fatal permission errors.

When test.php arrives, PHP attempts to save the session state back to disk because the default session handler is the file system. The error log explicitly states: open(/var/lib/php/session/sess_isu2r2bqudeosqvpoo8a67oj02, O_RDWR) failed: Permission denied (13).

This indicates that the web server user (typically www-data, apache, or nobody) does not have write permissions to the directory /var/lib/php/session. PHP creates a new session file with restricted ownership. If the parent directory is owned by root and lacks the necessary sticky bit or group permissions, the web server cannot create or update these files. Consequently, when the redirect happens in send.php, the session data is stored temporarily in memory but fails to persist to disk upon request completion (or immediately if configured that way). When test.php starts a new session context, it reads the disk file, which either does not exist yet or contains empty data because the previous write failed silently.

Verifying and Fixing Permissions

To resolve this, you must ensure the directory designated by session.save_path is writable by the web server process. First, confirm the configuration in your php.ini:

session.save_path = "/var/lib/php/session"
session.save_handler = files

Once confirmed, check the ownership and permissions of that directory:

ls -ld /var/lib/php/session
# Example output showing root ownership without world-writable access
drwxr-xr-x 2 root root 4096 Jan 1 12:00 /var/lib/php/session

If the owner is root and permissions are 755, the web server user cannot write to it. You have two primary strategies to fix this:

  1. Change Ownership: If you want the web server to own the directory, change the group or user to match the web server process.
    # Assuming the web server runs as www-data
    chown -R www-data:www-data /var/lib/php/session
    chmod -R 750 /var/lib/php/session
    
  2. Make the Directory World-Writable (Not Recommended): You can give everyone write access using the sticky bit, which prevents users from deleting each other's files.
    chmod 1777 /var/lib/php/session
    chown root:root /var/lib/php/session
    

While making the directory world-writable is a quick fix for development environments or shared hosting where you cannot change ownership, it is generally discouraged in production due to security implications. Changing ownership is the robust solution.

The ob_start() Trap

Your code includes ob_start() at the very top of both files. This is technically redundant if you are strictly concerned with session data loss via permission errors, but it serves a specific purpose here: preventing "headers already sent" fatal errors. If there is any whitespace before <?php or an echo statement in send.php, PHP tries to flush output before the redirect header can be sent. The ob_start() buffers that output so the redirect works.

However, note that ob_start() does not clear the buffer automatically. If you had echo statements after setting the session variable but before the redirect without buffering them, they would corrupt the session data or trigger headers errors. Your current implementation is safe regarding logic, assuming the file has no leading whitespace.

Conclusion

The issue lies entirely in the operating system's filesystem permissions. The web server cannot write to /var/lib/php/session. Fixing the directory permissions via chown and adjusting chmod settings will allow PHP to successfully save the session data after the redirect, ensuring $_SESSION contains the expected values in test.php. If you are using a framework like Laravel or building a larger application where you need more granular control over session storage, consider switching to a database-backed driver (session.save_handler = db) or Redis, which handles these permissions differently and often simplifies state management across multiple server instances.

Our network