Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are looking for POST and GET variables, it's done in the method parameter of your HTML form:</p> <p><strong>login.php</strong></p> <pre><code>&lt;form name="myform" action="secondpage.php" method="post"&gt; &lt;div&gt;Username: &lt;input type="text" name="username" value="" /&gt;&lt;/div&gt; &lt;div&gt;Password: &lt;input type="password" name="password" value="" /&gt;&lt;/div&gt; &lt;/form&gt; </code></pre> <p>Then in this other page:</p> <p><strong>secondpage.php</strong></p> <pre><code>$username = isset($_POST['username']) ? $_POST['username'] : ''; $password = isset($_POST['password']) ? $_POST['password'] : ''; if ($username != '') { // do your validations here } </code></pre> <h2>Explanation</h2> <p>When you use the GET method, the parameters are visible in the URL, so let's say we change the method="GET" in login.php, you'll end up with something like secondpage.php?username=jsmith&amp;password=1234. And then you could get the values using <code>$_GET['username']</code>.</p> <p>Using POST makes it possible to send larger quantity of data (there is a <a href="http://www.boutell.com/newfaq/misc/urllength.html" rel="noreferrer">vague limit</a> to the size of a URL) and it's not visible in the URL. You should note though that <strong>it's still sent in clear text</strong>, so it does not means it's secure.</p> <p>POST and GET were made for different purposes. GET should be use to extract information that you could want to extract again in the future, information that is not special to this very instant. It's useful to have mypage.php?product=123 because you'll potentially want to send this URL to a friend. A POST should be used when you'll modify the state of data: updating a product, creating a new user, deleting an article and so on. It's something you want to happen once.</p> <h2>Structure</h2> <p>In conclusion, I just want to add that normally you wouldn't necessarily want to use another PHP script just to avoid some code to run or not. So without knowing the specifics of your project, I can nevertheless say that you would probably want to do something like that to benefit from the same code (such as the form's HTML).</p> <p>Please note it's simplified code.</p> <p><strong>login.php</strong></p> <pre><code>&lt;?php $error = false; $username = isset($_POST['username']) ? $_POST['username'] : ''; $password = isset($_POST['password']) ? $_POST['password'] : ''; // if, and only if something was posted... so not on first display if ($username != '') { // do your validations here if ($properlyLogged) { session_start(); $_SESSION['loggedAt'] = time(); header('Location: http://localhost/secondpage.php'); exit(); } else { $error = true; } } ?&gt; &lt;?php if($error): ?&gt;Login failed. Please try again.&lt;?php endif; ?&gt; &lt;form name="myform" action="login.php" method="post"&gt; &lt;div&gt;Username: &lt;input type="text" name="username" value="&lt;?php echo($username) ?&gt;" /&gt;&lt;/div&gt; &lt;div&gt;Password: &lt;input type="password" name="password" value="" /&gt;&lt;/div&gt; &lt;/form&gt; </code></pre> <p><strong>secondpage.php</strong></p> <pre><code>&lt;?php session_start(); if (!isset($_SESSION['loggedAt'])) { // if not properly logged in, return user to login header('Location: http://localhost/login.php'); exit(); } ?&gt; You are now logged in! </code></pre> <p>Hope that's what you were looking for!</p>
 

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