Codeigniter会话刷新页面后销毁,新服务器需配置调整建议
Hey there, let’s work through this CodeIgniter session issue together—since it runs flawlessly on your other servers, the problem is definitely tied to your new server’s setup. Here’s a step-by-step breakdown of what to check and fix:
1. Verify PHP Session Configuration in php.ini
First, dig into your server’s core PHP settings, as mismatches here are the most common culprit:
session.cookie_domain: Ensure this matches your new server’s domain. Use.yourdomain.comto support subdomains, or leave it blank for auto-detection (avoid addinghttp://orhttps://).session.cookie_secure: If your new server uses HTTPS, set this toOn—modern browsers block non-secure cookies on HTTPS sites. If you’re still using HTTP, set it toOff.session.save_path: Confirm this path exists and the PHP process has read/write permissions. A common fix is to set it to/tmp(most servers grant access here) or create a custom directory and set permissions to700.- Quick check: Add
phpinfo();at the top of your CodeIgniterindex.phpto pull up a full PHP config report. Compare the session-related values to your working server—any differences are likely suspects.
2. Audit CodeIgniter’s Session Settings
Open application/config/config.php and cross-verify these key settings:
$config['sess_driver']: Match this to your working server (e.g.,files,database). If usingfiles, double-check$config['sess_save_path']points to a writable directory.$config['sess_match_ip']: If this is set toTRUE, your new server might be behind a CDN or load balancer, causing the client IP to change on each request. Switch this toFALSEto test if sessions stabilize.$config['sess_expiration']: Avoid setting this to0(permanent session) if your server enforces strict cookie timeouts. Try a value like86400(24 hours) instead.$config['sess_cookie_name']: Ensure this is unique to your app to avoid conflicts with other services on the new server.
3. Check Server-Side Cookie Restrictions
- Browser Dev Tools Test: Open your browser’s DevTools (F12), go to the Application > Cookies tab. Look for your CodeIgniter session cookie (usually
ci_session)—confirm it’s being set, has the correct domain/path (/), and includes expected flags likeHttpOnlyandSecure(if using HTTPS). - Web Server Config: If using Nginx, check for misconfigured
proxy_set_header Cookiedirectives. For Apache, ensure ModSecurity or other security modules aren’t blocking session cookies. - SameSite Cookie Policy: If your app sets
SameSite=Nonefor cookies, make sure it’s paired with theSecureflag—modern browsers rejectSameSite=Nonecookies without it.
4. Fix File/Directory Permissions
- If using the
filessession driver:- Locate the session storage directory (default is
application/sessionsor the path insess_save_path). - Set permissions to
700withchmod 700 /path/to/your/session/dirto restrict access. - Set ownership to the PHP process user (e.g.,
www-datafor Apache/Nginx) withchown www-data:www-data /path/to/your/session/dir.
- Locate the session storage directory (default is
- If using the
databasedriver: Ensure the session table has the correct structure, and your CodeIgniter database user has full read/write permissions for it.
5. Quick PHP Session Test (Isolate the Issue)
To rule out CodeIgniter-specific bugs, create a simple standalone PHP script in your server’s root:
<?php session_start(); if (!isset($_SESSION['test_session'])) { $_SESSION['test_session'] = "Created at: " . date("Y-m-d H:i:s"); echo $_SESSION['test_session']; } else { echo "Session persists: " . $_SESSION['test_session']; }
Refresh this page multiple times. If the session doesn’t persist, the problem is with your server’s PHP environment—not CodeIgniter. If it does persist, circle back to your framework’s session config.
内容的提问来源于stack exchange,提问作者Abhay Dwivedi

