<p>What are some guidelines for maintaining responsible session security with PHP?  There's information all over the web and it's about time it all landed in one place!</p>

[{'answer_id': 329, 'author': 'saint_groceon', 'author_id': 59, 'author_profile': 'https://Stackoverflow.com/users/59', 'pm_score': 4, 'selected': False, 'text': '<p>One guideline is to call <a href="http://www.php.net/session_regenerate_id" rel="nofollow noreferrer">session_regenerate_id</a> every time a session\'s security level changes.  This helps prevent session hijacking.</p>\n'}, {'answer_id': 331, 'author': 'cmcculloh', 'author_id': 58, 'author_profile': 'https://Stackoverflow.com/users/58', 'pm_score': 4, 'selected': False, 'text': '<p>I think one of the major problems (which is being addressed in PHP 6) is register_globals. Right now one of the standard methods used to avoid <code>register_globals</code> is to use the <code>$_REQUEST</code>, <code>$_GET</code> or <code>$_POST</code> arrays.</p>\n\n<p>The "correct" way to do it (as of 5.2, although it\'s a little buggy there, but stable as of 6, which is coming soon) is through <a href="http://us.php.net/filter" rel="nofollow noreferrer">filters</a>.</p>\n\n<p>So instead of:</p>\n\n<pre><code>$username = $_POST["username"];\n</code></pre>\n\n<p>you would do:</p>\n\n<pre><code>$username = filter_input(INPUT_POST, \'username\', FILTER_SANITIZE_STRING);\n</code></pre>\n\n<p>or even just:</p>\n\n<pre><code>$username = filter_input(INPUT_POST, \'username\');\n</code></pre>\n'}, {'answer_id': 335, 'author': 'helloandre', 'author_id': 50, 'author_profile': 'https://Stackoverflow.com/users/50', 'pm_score': 2, 'selected': False, 'text': '<p>This is pretty trivial and obvious, but be sure to <a href="http://www.php.net/session_destroy" rel="nofollow noreferrer">session_destroy</a> after every use. This can be difficult to implement if the user does not log out explicitly, so a timer can be set to do this.</p>\n\n<p>Here is a good <a href="http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/" rel="nofollow noreferrer">tutorial</a> on setTimer() and clearTimer().</p>\n'}, {'answer_id': 681, 'author': 'John Downey', 'author_id': 200, 'author_profile': 'https://Stackoverflow.com/users/200', 'pm_score': 2, 'selected': False, 'text': "<p>The main problem with PHP sessions and security (besides session hijacking) comes with what environment you are in. By default PHP stores the session data in a file in the OS's temp directory. Without any special thought or planning this is a world readable directory so all of your session information is public to anyone with access to the server.</p>\n\n<p>As for maintaining sessions over multiple servers. At that point it would be better to switch PHP to user handled sessions where it calls your provided functions to CRUD (create, read, update, delete) the session data. At that point you could store the session information in a database or memcache like solution so that all application servers have access to the data.</p>\n\n<p>Storing your own sessions may also be advantageous if you are on a shared server because it will let you store it in the database which you often times have more control over then the filesystem.</p>\n"}, {'answer_id': 1611, 'author': 'Teifion', 'author_id': 1384652, 'author_profile': 'https://Stackoverflow.com/users/1384652', 'pm_score': 2, 'selected': False, 'text': "<p>I would check both IP and User Agent to see if they change</p>\n\n<pre><code>if ($_SESSION['user_agent'] != $_SERVER['HTTP_USER_AGENT']\n    || $_SESSION['user_ip'] != $_SERVER['REMOTE_ADDR'])\n{\n    //Something fishy is going on here?\n}\n</code></pre>\n"}, {'answer_id': 3965, 'author': 'Eric Lamb', 'author_id': 538, 'author_profile': 'https://Stackoverflow.com/users/538', 'pm_score': 3, 'selected': False, 'text': "<p>Using IP address isn't really the best idea in my experience. For example; my office has two IP addresses that get used depending on load and we constantly run into issues using IP addresses. </p>\n\n<p>Instead, I've opted for storing the sessions in a separate database for the domains on my servers. This way no one on the file system has access to that session info. This was really helpful with phpBB before 3.0 (they've since fixed this) but it's still a good idea  I think.</p>\n"}, {'answer_id': 6585, 'author': 'ejunker', 'author_id': 796, 'author_profile': 'https://Stackoverflow.com/users/796', 'pm_score': 2, 'selected': False, 'text': '<p>If you you use <a href="http://www.php.net/manual/en/function.session-set-save-handler.php" rel="nofollow noreferrer">session_set_save_handler()</a> you can set your own session handler. For example you could store your sessions in the database. Refer to the php.net comments for examples of a database session handler.</p>\n\n<p>DB sessions are also good if you have multiple servers otherwise if you are using file based sessions you would need to make sure that each webserver had access to the same filesystem to read/write the sessions.</p>\n'}, {'answer_id': 7488, 'author': 'grom', 'author_id': 486, 'author_profile': 'https://Stackoverflow.com/users/486', 'pm_score': 7, 'selected': True, 'text': "<p>There are a couple of things to do in order to keep your session secure:</p>\n\n<ol>\n<li>Use SSL when authenticating users or performing sensitive operations.</li>\n<li>Regenerate the session id whenever the security level changes (such as logging in). You can even regenerate the session id every request if you wish.</li>\n<li>Have sessions time out</li>\n<li>Don't use register globals</li>\n<li>Store authentication details on the server. That is, don't send details such as username in the cookie.</li>\n<li>Check the <code>$_SERVER['HTTP_USER_AGENT']</code>. This adds a small barrier to session hijacking. You can also check the IP address. But this causes problems for users that have changing IP address due to load balancing on multiple internet connections etc (which is the case in our environment here).</li>\n<li>Lock down access to the sessions on the file system or use custom session handling</li>\n<li>For sensitive operations consider requiring logged in users to provide their authenication details again</li>\n</ol>\n"}, {'answer_id': 14938, 'author': 'Dinoboff', 'author_id': 1771, 'author_profile': 'https://Stackoverflow.com/users/1771', 'pm_score': 2, 'selected': False, 'text': '<p>You need to be sure the session data are safe. By looking at your php.ini or using phpinfo() you can find you session settings. _session.save_path_ tells you where they are saved.</p>\n\n<p>Check the permission of the folder and of its parents. It shouldn\'t be public (/tmp) or be accessible by other websites on your shared server.</p>\n\n<p>Assuming you still want to use php session, You can set php to use an other folder by changing _session.save_path_ or save the data in the database by changing _session.save_handler_ .</p>\n\n<p>You might be able to set _session.save_path_ in your php.ini (some providers allow it) or for apache + mod_php, in a .htaccess file in your site root folder:\n<code>php_value session.save_path "/home/example.com/html/session"</code>. You can also set it at run time with _session_save_path()_ .</p>\n\n<p>Check <a href="http://shiflett.org/articles/storing-sessions-in-a-database" rel="nofollow noreferrer">Chris Shiflett\'s tutorial</a> or <a href="http://framework.zend.com/manual/en/zend.session.savehandler.dbtable.html" rel="nofollow noreferrer">Zend_Session_SaveHandler_DbTable</a> to set and alternative session handler.</p>\n'}, {'answer_id': 616969, 'author': 'raspi', 'author_id': 71964, 'author_profile': 'https://Stackoverflow.com/users/71964', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.acros.si/papers/session_fixation.pdf" rel="nofollow noreferrer">This session fixation paper</a> has very good pointers where attack may come. See also <a href="http://en.wikipedia.org/wiki/Session_fixation" rel="nofollow noreferrer">session fixation page at Wikipedia</a>.</p>\n'}, {'answer_id': 2586332, 'author': 'takeshin', 'author_id': 234780, 'author_profile': 'https://Stackoverflow.com/users/234780', 'pm_score': 4, 'selected': False, 'text': '<p>My two (or more) cents:</p>\n\n<ul>\n<li>Trust no one</li>\n<li>Filter input, escape output (cookie, session data are your input too)</li>\n<li>Avoid XSS (keep your HTML well formed, take a look at <a href="http://phptal.org">PHPTAL</a> or <a href="http://htmlpurifier.org/">HTMLPurifier</a>)</li>\n<li><a href="http://en.wikipedia.org/wiki/Defense_in_Depth_%28computing%29">Defense in depth</a></li>\n<li>Do not expose data</li>\n</ul>\n\n<p>There is a tiny but good book on this topic: <a href="http://phpsecurity.org/">Essential PHP Security by Chris Shiflett</a>.</p>\n\n<p><a href="http://shiflett.org/images/essential-php-security-small.png">Essential PHP Security http://shiflett.org/images/essential-php-security-small.png</a></p>\n\n<p>On the home page of the book you will find some interesting code examples and sample chapters.</p>\n\n<p>You may use technique mentioned above (IP &amp; UserAgent), described here: <a href="http://devzone.zend.com/article/11906">How to avoid identity theft</a></p>\n'}, {'answer_id': 6754508, 'author': 'Chad', 'author_id': 852876, 'author_profile': 'https://Stackoverflow.com/users/852876', 'pm_score': 2, 'selected': False, 'text': "<p>I set my sessions up like this-</p>\n\n<p>on the log in page:</p>\n\n<pre><code>$_SESSION['fingerprint'] = md5($_SERVER['HTTP_USER_AGENT'] . PHRASE . $_SERVER['REMOTE_ADDR']);\n</code></pre>\n\n<p>(phrase defined on a config page)</p>\n\n<p>then on the header that is throughout the rest of the site:</p>\n\n<pre><code>session_start();\nif ($_SESSION['fingerprint'] != md5($_SERVER['HTTP_USER_AGENT'] . PHRASE . $_SERVER['REMOTE_ADDR'])) {       \n    session_destroy();\n    header('Location: http://website login page/');\n    exit();     \n}\n</code></pre>\n"}, {'answer_id': 7748887, 'author': 'user956584', 'author_id': 979474, 'author_profile': 'https://Stackoverflow.com/users/979474', 'pm_score': 2, 'selected': False, 'text': '<h1>php.ini</h1>\n\n<pre><code>session.cookie_httponly = 1\nchange session name from default PHPSESSID\n</code></pre>\n\n<h1>eq Apache add header:</h1>\n\n<pre><code>X-XSS-Protection    1\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/328', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/59/']