Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely unconnected with the current page. <em>Except</em> if you use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.</p> <p><strong>Session:</strong></p> <pre><code>//On page 1 $_SESSION['varname'] = $var_value; //On page 2 $var_value = $_SESSION['varname']; </code></pre> <p>Remember to run the <code>session_start();</code> statement on both these pages before you try to access the <code>$_SESSION</code> array, and also before any output is sent to the browser.</p> <p><strong>Cookie:</strong></p> <pre><code>//One page 1 $_COOKIE['varname'] = $var_value; //On page 2 $var_value = $_COOKIE['varname']; </code></pre> <p>The big difference between sessions and cookies is that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.</p> <p><strong>GET and POST</strong></p> <p>You can add the variable in the link to the next page:</p> <pre><code>&lt;a href="page2.php?varname=&lt;?php echo $var_value ?&gt;"&gt;Page2&lt;/a&gt; </code></pre> <p>This will create a GET variable. </p> <p>Another way is to include a hidden field in a form that submits to page two:</p> <pre><code>&lt;form method="get" action="page2.php"&gt; &lt;input type="hidden" name="varname" value="var_value"&gt; &lt;input type="submit"&gt; &lt;/form&gt; </code></pre> <p>And then on page two:</p> <pre><code>//Using GET $var_value = $_GET['varname']; //Using POST $var_value = $_POST['varname']; //Using GET, POST or COOKIE. $var_value = $_REQUEST['varname']; </code></pre> <p>Just change the method for the form to <code>post</code> if you want to do it via post. Both are equally insecure, although GET is easier to hack.</p> <p>The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.</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. 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.
 

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