Session Not Working in PHP
From session.save_path permissions to cookie domain issues. A troubleshooting guide for when $_SESSION data won't persist across pages.
If your PHP session is not working, not saving values, or getting destroyed automatically — don’t worry. This is a very common real-world problem. In this guide, I will explain the exact reasons and simple solutions that work in production servers.
First Understand the Problem Clearly
When session is not working in PHP, usually you may see issues like:
- Session value not storing
- Session lost after redirect
- User automatically logged out
- $_SESSION array empty
- Warning: session_start(): Cannot send session cache limiter
Now let’s fix this step by step.
1. Did You Add session_start()?
This is the most common mistake.
You must add session_start(); at the top of every PHP file where session is used.
<?php
session_start();
$_SESSION['user'] = "Admin";
?>
Important:
- session_start() must be before any HTML output
- No space, no echo, no HTML before it
2. Check “Headers Already Sent” Error
If you see error like:
Warning: session_start(): Cannot send session cache limiter - headers already sent
This means some output already started before session_start().
Possible reasons:
- Extra space before <?php
- Extra blank line at end of file
- UTF-8 BOM issue
- echo or print before session_start()
You can also read our detailed solution guide on Headers already sent error in PHP.
3. Session Not Working After Redirect?
Many developers face this:
<?php
session_start();
$_SESSION['user'] = "Admin";
header("Location: dashboard.php");
exit;
?>
If session not available in dashboard.php, check:
- Did you add session_start() in dashboard.php?
- Is redirect happening properly?
- Are cookies enabled in browser?
4. Check php.ini Session Settings
Sometimes issue is from server side configuration.
Check these settings:
- session.save_path
- session.auto_start
- session.cookie_lifetime
Create a phpinfo file:
<?php phpinfo(); ?>
Make sure session.save_path folder exists and is writable.
On shared hosting, this is common issue.
5. Session Destroying Automatically?
If user logout automatically after few minutes, check:
- session.gc_maxlifetime value
- Server cron cleaning session files
- Load balancer without sticky session
In production systems like eCommerce websites or admin dashboards, this becomes critical. Especially when handling login systems, payment gateways, or high CPC keyword areas like web hosting services or cloud server hosting platforms.
6. HTTPS and Cookie Issue
If your website is running on HTTPS, but session cookie is not secure, session may fail.
Check this:
ini_set('session.cookie_secure', 1);
Also check:
- session.cookie_domain
- session.cookie_path
If domain mismatch (www and non-www), session may break.
7. Multiple Servers or Load Balancer?
In real commercial production systems, when your application runs on multiple servers (AWS, DigitalOcean, etc.), session may not work properly.
Why?
User login request goes to Server 1. Next request goes to Server 2. Session file not available there.
Solutions:
- Use Redis for session storage
- Use database session storage
- Enable sticky sessions
This is very important for scalable applications and high traffic SaaS products.
8. Debugging Checklist (Quick Fix Summary)
- ✔ Add session_start() at top
- ✔ No output before session_start()
- ✔ Add session_start() on every page
- ✔ Check php.ini settings
- ✔ Check folder permission
- ✔ Check HTTPS cookie settings
- ✔ Check multi-server architecture
Real Experience Note
In one client project (large admin CRM system), sessions were randomly logging out users. Issue was load balancer without sticky session. After moving session storage to Redis, issue was completely solved.
So always think beyond code. Sometimes problem is server architecture.
Final Words
Session not working in PHP is not a big problem. In 90% cases, it is either missing session_start() or headers already sent issue.
Follow this guide step by step. Don’t panic. Debug slowly.
If you are also facing performance problems, you can read our guide on How to Fix Slow MySQL Query to optimize backend performance.
Ketan Patel
PHP & MySQL Performance Optimization Specialist
I specialize in diagnosing and fixing slow PHP applications, optimizing MySQL queries, and resolving backend bottlenecks in live production systems. My approach is metric-driven — identifying root causes through profiling, execution analysis, and structured optimization instead of guesswork.