<p>One thing I've started doing more often recently is <strong>retrieving some data</strong> at the beginning of a task <strong>and storing it in a $_SESSION['myDataForTheTask']</strong>. </p>

<p>Now it seems very convenient to do so but I don't know anything about performance, security risks or similar, using this approach. Is it something which is regularly done by programmers with more expertise or is it more of an amateur thing to do?</p>

<p><strong>For example:</strong></p>

<pre><code>if (!isset($_SESSION['dataentry']))
{
    $query_taskinfo = "SELECT participationcode, modulearray, wavenum FROM mng_wave WHERE wave_id=" . mysql_real_escape_string($_GET['wave_id']);
    $result_taskinfo = $db-&gt;query($query_taskinfo);
    $row_taskinfo = $result_taskinfo-&gt;fetch_row();

        $dataentry = array("pcode" =&gt; $row_taskinfo[0], "modules" =&gt; $row_taskinfo[1], "data_id" =&gt; 0, "wavenum" =&gt; $row_taskinfo[2], "prequest" =&gt; FALSE, "highlight" =&gt; array());

        $_SESSION['dataentry'] = $dataentry;
}
</code></pre>

[{'answer_id': 77846, 'author': 'nsayer', 'author_id': 13757, 'author_profile': 'https://Stackoverflow.com/users/13757', 'pm_score': 1, 'selected': False, 'text': "<p>$_SESSION items are stored in the session, which is, by default, kept on disk. There is no need to make your own array and stuff it in a 'dataentry' array entry like you did. You can just use $_SESSION['pcode'], $_SESSION['modules'] and so on.</p>\n\n<p>Like I said, the session is stored on disk and a pointer to the session is stored in a cookie. The user thus can't easily get ahold of the session data.</p>\n"}, {'answer_id': 77852, 'author': 'Steve M', 'author_id': 1693, 'author_profile': 'https://Stackoverflow.com/users/1693', 'pm_score': 0, 'selected': False, 'text': "<p>I use this approach a fair bit, I don't see any problem with it. Unlike cookies, the data isn't stored at the client-side, which is often a big mistake.</p>\n\n<p>Like anything though, just be careful that you're always sanitising user input, especially if you're putting user input into the $_SESSION variable, then later using that variable in an SQL query.</p>\n"}, {'answer_id': 77877, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 2, 'selected': False, 'text': '<p>There are a few factors you\'ll want to consider when deciding where to store temporary data.  Session storage is great for data that is specific to a single user.  If you find the default file-based session storage handler is inefficient you can implement something else, possibly using a database or memcache type of backend.  See <a href="http://us.php.net/manual/en/function.session-set-save-handler.php" rel="nofollow noreferrer">session_set_save_handler</a> for more info.</p>\n\n<p>I find it is a bad practice to store common data in a user\'s session.  There are better places to store data that will be frequently accessed by several users and by storing this data in the session you will be duplicating the data for each user who needs this data.  In your example, you might set up a different type of storage engine for this wave data (based on wave_id) that is NOT tied specifically to a user\'s session.  That way you\'ll pull the data down once and them store it somewhere that several users can access the data without requiring another pull.</p>\n'}, {'answer_id': 77882, 'author': 'Lucas Oman', 'author_id': 6726, 'author_profile': 'https://Stackoverflow.com/users/6726', 'pm_score': 1, 'selected': False, 'text': '<p>IMO, it\'s perfectly acceptable to store things in the session. It\'s a great way to make data persistent. It\'s also, in many cases, more secure than storing everything in cookies.  Here are a few concerns:</p>\n\n<ul>\n<li>It\'s possible for someone to hijack a session, so if you\'re going to use it to keep track of user authorization, be careful. Read <a href="http://en.wikipedia.org/wiki/Session_hijacking" rel="nofollow noreferrer">this</a> for more information.</li>\n<li>It can be a very lazy way to keep data. Don\'t just throw everything in the session so that you don\'t have to query for it later.</li>\n<li>If you\'re going to store objects in the session, either their class files will need to be included before the session is started on the next request or you\'ll need to have configured an auto loader.</li>\n</ul>\n'}, {'answer_id': 77885, 'author': 'jfs', 'author_id': 6223, 'author_profile': 'https://Stackoverflow.com/users/6223', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re running on your own server, or in an environment where nobody can snoop on your files/memory on the server, session data are secure. They\'re stored on the server and just an identification cookie sent to the client. The problem is if other people can snatch the cookie and impersonate someone else, of course. Using HTTPS and making sure to not put the session ID in URLs should keep your users safe from most of those problems. (XSS might still be used to snatch cookies if you aren\'t careful, see <a href="http://www.codinghorror.com/blog/archives/001167.html" rel="nofollow noreferrer">Jeef Atwoods post on this</a> too.)</p>\n\n<p>As for what to store in a session variable, put your data there if you want to refer to it again on another page, like a shopping basket, but don\'t put it there if it\'s just temporary data used for producing the result of this page, like a list of tags for the currently viewed post. Sessions are for per-user persistent data.</p>\n'}, {'answer_id': 77899, 'author': 'foxxtrot', 'author_id': 10369, 'author_profile': 'https://Stackoverflow.com/users/10369', 'pm_score': 0, 'selected': False, 'text': "<p>This is a fairly common thing to do, and the session is generally going to be faster than continuous database hits.  They're also reasonably secure, as the PHP devs have worked hard to prevent Session Hijacking.</p>\n\n<p>The only issue is that you need to remember to rebuild the session entry when something changes. And, if anything is changed by a user other than the one who owns the session that would result in a need to refresh this key, there is no easy way to notify the system to refresh this session key.  Possibly not a big deal, but something you should be aware of.</p>\n"}, {'answer_id': 77906, 'author': 'Ryan Smith', 'author_id': 10420, 'author_profile': 'https://Stackoverflow.com/users/10420', 'pm_score': 3, 'selected': False, 'text': '<p>I use the session variable all the time to store information for users.  I haven\'t seen any issues with performance.  The session data is pulled based on the cookie (or <em>PHPSESSID</em> if you have cookies turned off).  I don\'t see it being any more of a security risk than any other cookie based authentication, and probably more secure than storing the actual data in the users cookie.</p>\n\n<p>Just to let you know though, you do have a security issue with your SQL statement:</p>\n\n<pre><code>SELECT participationcode, modulearray, wavenum FROM mng_wave WHERE wave_id=".$_GET[\'wave_id\'];\n</code></pre>\n\n<p>You should <strong>NEVER, I REPEAT NEVER</strong>, take user provided data and use it to run a SQL statement without first sanitizing it.  I would wrap it in quotes and add the function <code>mysql_real_escape_string()</code>.  That will protect you from most attacks.  So your line would look like:</p>\n\n<pre><code>$query_taskinfo = "SELECT participationcode, modulearray, wavenum FROM mng_wave WHERE wave_id=\'".mysql_real_escape_string($_GET[\'wave_id\'])."\'";\n</code></pre>\n'}, {'answer_id': 77910, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Zend Framework has a useful library for session data management which helps with expiry and security (for stuff like captchas). They also have a useful explanation of sessions. See <a href="http://framework.zend.com/manual/en/zend.session.html" rel="nofollow noreferrer">http://framework.zend.com/manual/en/zend.session.html</a></p>\n'}, {'answer_id': 77916, 'author': 'HappySmileMan', 'author_id': 14073, 'author_profile': 'https://Stackoverflow.com/users/14073', 'pm_score': 5, 'selected': True, 'text': "<p>Well Session variables are really one of the only ways (and probably the most efficient) of having these variables available for the entire time that visitor is on the website, there's no real way for a user to edit them (other than an exploit in your code, or in the PHP interpreter) so they are fairly secure.</p>\n\n<p>It's a good way of storing settings that can be changed by the user, as you can read the settings from database once at the beginning of a session and it is available for that entire session, you only need to make further database calls if the settings are changed and of course, as you show in your code, it's trivial to find out whether the settings already exist or whether they need to be extracted from database.</p>\n\n<p>I can't think of any other way of storing temporary variables securely (since cookies can easily be modified and this will be undesirable in most cases) so $_SESSION would be the way to\xa0go</p>\n"}, {'answer_id': 78089, 'author': 'William Macdonald', 'author_id': 2725, 'author_profile': 'https://Stackoverflow.com/users/2725', 'pm_score': 2, 'selected': False, 'text': '<p>Another way to improve the input validation is to cast the _GET[\'wave_id\'] variable:</p>\n\n<pre><code>$query_taskinfo = "SELECT participationcode, modulearray, wavenum FROM mng_wave WHERE wave_id=".(int)$_GET[\'wave_id\']." LIMIT 1";\n</code></pre>\n\n<p>I\'m presuming wave_id is an integer, and that there is only one answer.</p>\n\n<p>Will</p>\n'}, {'answer_id': 78514, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>I have found sessions to be very useful, but a few things to note:</p>\n\n<p>1)  That PHP may store your sessions in a tmp folder or other directory that may be accessible to other users on your server. You can change the directory were sessions are stored by going to the php.ini file.</p>\n\n<p>2)  If you are setting up a high value system that needs very tight security you might want to encrypt the data before you send it to the session, and decrypt it to use it. Note:  this might create too much overhead depending on your traffic / server capacity.</p>\n\n<p>3)  I have found that session_destroy(); doesn’t delete the session right away, you still have to wait for the PHP garbage collector to clean the sessions up. You can change the frequency that the garbage collector is run in the php.ini file. But is still doesn’t seem very reliable, more info <a href="http://www.captain.at/howto-php-sessions.php" rel="nofollow noreferrer">http://www.captain.at/howto-php-sessions.php</a></p>\n'}, {'answer_id': 196809, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>$_SESSION mechanism is using cookies. </p>\n\n<p>In case of Firefox (and maybe new IE, I didn\'t check myself) that means that <strong>session is shared between opened tabs</strong>. That is not something you expect by default. And it means that session is no longer "something specific to a single window/user".</p>\n\n<p>For example, if you have opened two tabs to access your site, than logged as a root using  the first tab, you will gain root privileges in the other one.</p>\n\n<p>That is really inconvenient, especially if you code e-mail client or something else (like e-shop). In this case you will have to manage sessions manually or introduce constantly regenerated key in URL or do something else.</p>\n'}, {'answer_id': 634423, 'author': 'Matt', 'author_id': 17759, 'author_profile': 'https://Stackoverflow.com/users/17759', 'pm_score': 1, 'selected': False, 'text': '<p>You might want to consider how REST-ful this is?</p>\n\n<p>i.e. see "Communicate statelessly" paragraph in "<a href="http://www.infoq.com/articles/rest-introduction" rel="nofollow noreferrer">A Brief Introduction to REST</a>"...</p>\n\n<blockquote>\n  <p>"REST mandates that state be either\n  turned into resource state, or kept on\n  the client. In other words, a server\n  should not have to retain some sort of\n  communication state for any of the\n  clients it communicates with beyond a\n  single request."</p>\n</blockquote>\n\n<p>(or any of the other links on wikipedia for <a href="http://en.wikipedia.org/wiki/REST" rel="nofollow noreferrer">REST</a>)</p>\n\n<p>So in your case, the \'wave_id\' is a sensible Resource to GET, but do you really want to store it in the SESSION? Surely <a href="http://www.danga.com/memcached/" rel="nofollow noreferrer">memcached</a> is your solution to cacheing the object Resource?</p>\n'}, {'answer_id': 767861, 'author': 'Tom', 'author_id': 42754, 'author_profile': 'https://Stackoverflow.com/users/42754', 'pm_score': 2, 'selected': False, 'text': '<p>A few other disadvantages of using sessions:</p>\n\n<ol>\n<li><code>$_SESSION</code> data will expire after <em>session.gc_maxlifetime</em> seconds of inactivity.</li>\n<li>You\'ll have to remember to call <code>session_start()</code> for every script that will use the session data.</li>\n<li>Scaling the website by load balancing over multiple servers could be a problem because the user will need to be directed to the same server each time. Solve this with "Sticky Sessions".</li>\n</ol>\n'}, {'answer_id': 803140, 'author': 'Bryan Grezeszak', 'author_id': 89633, 'author_profile': 'https://Stackoverflow.com/users/89633', 'pm_score': 0, 'selected': False, 'text': '<p>$_SESSION is very useful in security, since it is a server side way to store information while a user is actively on your pages, therefore hard to hack unless your actual php file or server has weaknesses that are exploited. One very good implementation is storing a variable to confirm that the user is logged in, and only allowing actions to be taken if they are confirmed logged in.</p>\n'}]
['https://Stackoverflow.com/questions/77826', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11995/']