Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To understand the difference, let's go through in detail how each of the solutions would work and their security risks. <em>In this example we are going to store the count of page views of the user.</em></p> <h2>$_SESSION</h2> <p>We start the session, increment the counter and store it in the <code>$_SESSION</code> array:</p> <pre><code>&lt;?php session_start(); if(!isset($_SESSION["pageviews"])) { $_SESSION["pageviews"] = 0; } $_SESSION["pageviews"]++; ?&gt; </code></pre> <p>When a user visits this page for the first time, PHP will generate a random session identifier that looks like the following, and ask the browser to store this ID in a cookie:</p> <pre><code>fh4giqncq25ntgs7gjunvj6i33 </code></pre> <p><em>On the server</em>, it will store and remember that there is a <code>pageviews</code> variable with the value <code>1</code> that belongs to the session ID <code>fh4giqncq25ntgs7gjunvj6i33</code>.</p> <p>The next time the user visits the page, his or her browser will send the previous session ID along with the request (given that the cookie hasn't expired or got deleted). PHP then recognizes this ID, and populates the <code>$_SESSION</code> array with <code>pageviews = 1</code>, then increments it: <code>pageviews = 2</code></p> <p>In terms of security, consider the following questions:</p> <p><strong>Is the user able to read the stored data? No</strong> &ndash; The only thing the client sees is the random session ID in the cookie; the data itself is stored on the server.</p> <p><strong>Is the user able to alter or manipulate the stored data? Again, no</strong> &ndash; If the session ID is altered in the browser, PHP will not be able to tie the browser to the stored data any more. In this worst case scenario the user will get a new session, starting with <code>pageviews = 1</code>.</p> <p>The main security risk of sessions is <strong>session hijacking</strong>, when an attacker somehow manages to get the session ID from someone else's browser and then presents it to the server, thereby impersonating the other user. In our example this would not make much sense since we're only storing a page view count; however, <em>most sites use sessions to keep track of which user is logged on from which browser</em>. In that scenario, stealing someone else's session would mean getting access to their account.</p> <h2>Hidden field</h2> <p>In this case we have a form with a hidden field:</p> <pre><code>&lt;form action="..." method="post"&gt; &lt;input type="hidden" name="pageviews" value="&lt;?php print($pageviews); ?&gt;" /&gt; ... &lt;/form&gt; </code></pre> <p>On the server, we retrieve the <code>pageviews</code> variable from <code>$_POST</code> and increment it:</p> <pre><code>&lt;?php $pageviews = @$_POST["pageviews"]; $pageviews++; ?&gt; </code></pre> <p>So, instead of storing it on the server, we essentially send the data down to the client and expect it back in the subsequent request. Apart from the fact that it only works with <strong>POST</strong> requests, let's look at the security-related downsides of this solution:</p> <p><strong>Is the user able to read the stored data? Yes</strong> &ndash; it goes straight to the browser in the HTML code.</p> <p><strong>Is the user able to alter or manipulate the stored data? Yes</strong> &ndash; there is nothing to prevent the user from opening up the developer tools in his or her browser and changing the hidden value to whatever he or she likes. Upon submitting the form, the server gets the altered data.</p> <p>The problem with <code>&lt;input type="hidden"&gt;</code> is that you just <strong>can't trust the client</strong>, so you have to verify the data you get <em>in every request</em>. It might be reasonable to do this in some cases, such as filling out multi-page forms, but even that can often be better solved with sessions.</p> <h2>Summary</h2> <p>Persisting data via <code>$_SESSION</code> is generally <strong>safer</strong> than using <code>&lt;input type="hidden"&gt;</code> because the session data is stored on the server, and thus cannot be tampered with by the client. Only a random session identifier is sent to the browser in a cookie which ties the data on the server to that particular browser.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload