PHP Warning Permission denied (13) on session_start()

PHP Warning Permission denied (13) on session_start()

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Warning Permission denied (13) on session_start()

When session_start() throws a "Permission denied (13)" error, it is almost never a bug in your application logic. The code snippet <?php session_start(); ?> is syntactically correct and functionally sound; the issue lies entirely with the operating system's file permissions preventing the web server process from writing to the default session directory. This specific warning indicates that PHP cannot create or update the session file because the current user running the web server lacks write access to /tmp.

In a typical shared hosting environment, you might see paths like /home/username/public_html in the stack trace, but the actual failure occurs when PHP attempts to open the session file specified by session.save_path, which defaults to /tmp. The error message explicitly states: open(/tmp/sess_49a20cbe1ef09a2d0262b3f7eb842e7b, O_RDWR) failed: Permission denied (13). This happens because the session.save_path is set to a system-wide temporary directory that your web server user (often www-data, apache, or nobody) cannot write to.

The reason this error "comes and goes" is often due to how operating systems handle temporary directories. On many Linux distributions, /tmp is mounted with the noexec, nosuid, and nodev options to enhance security. Additionally, some servers use tmpfs (RAM-backed storage) that gets wiped on every reboot, or log rotation tools might truncate files in specific ways that inadvertently lock them. If your application relies on the default /tmp location, you are at the mercy of the server administrator's configuration choices regarding which user owns that directory and what group permissions are applied.

To resolve this, you must explicitly change where PHP stores session data to a directory where the web server has confirmed write access. The most robust approach is to define a custom session save path within your application configuration or environment variables. You can achieve this by setting the session.save_path directive in your php.ini file or passing it via the ini_set() function before calling session_start().

For example, you can create a dedicated directory inside your project's root and configure PHP to use it:

sudo mkdir -p /var/www/mysite/session_data
sudo chown www-data:www-data /var/www/mysite/session_data
sudo chmod 750 /var/www/mysite/session_data

Then, in your PHP application, ensure this path is set before any session logic runs:

ini_set('session.save_path', '/var/www/mysite/session_data');
session_start();

This approach isolates the problem from the system's /tmp directory entirely. It ensures that even if the system administrator resets /tmp permissions or clears it, your application continues to function correctly because it owns its own storage space.

However, there is a critical trade-off to consider regarding session security and isolation. By storing sessions on the filesystem rather than in the database, you rely on the file system's integrity. If you ever need to migrate users between servers or scale horizontally, file-based sessions are inherently stateless only if you ensure the filesystem is replicated or if you switch to a database driver entirely (e.g., using session.save_handler = db).

Another common pitfall involves mixed environments where some requests succeed and others fail. This inconsistency usually points to race conditions in permission handling or transient locks on the session file, though "Permission denied" specifically rules out locking issues and points strictly to ownership mismatches. Always verify that the numeric UID of your web server process matches the UID of the owner of the directory you are writing to.

If you are managing a Laravel project, you might wonder if this is a framework issue. It is not; session_start() is a native PHP function. However, Laravel provides robust session handling that can abstract away some of these low-level details. In modern Laravel versions, you can configure the session driver in your .env file to use database, redis, or memcached, which completely bypasses filesystem permissions issues for sessions entirely.

SESSION_DRIVER=database

Switching drivers is often the cleanest long-term solution if you have permission to modify your application's architecture, as it removes the dependency on specific filesystem paths altogether. But if you are stuck with file-based sessions due to legacy constraints or shared hosting limitations, explicitly defining a writable path within your application scope is the definitive fix.

Our network