Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd recommend, as did Alma Do Mundo, to read about sessions. Here's how you would accomplish what you wanted:</p> <p>login.php:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt;&lt;title&gt;Login&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;form id='loginform' action='checklogin.php' method='post'&gt; &lt;div&gt;Username: &lt;input type='text' id='username' name='username' /&gt;&lt;/div&gt; &lt;div&gt;Password: &lt;input type='password' id='password' name='password' /&gt;&lt;/div&gt; &lt;div&gt;&lt;input type='submit' id='submit' value='Login'&gt;&lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Have the login page send the data to an intermediate page such as checklogin.php. Here's the code for that:</p> <pre><code>&lt;?php session_start(); // check if the username and password are correct // in real world scenario, this would be more complex as data // is cleaned and then checked against the database where username // and encrypted password (and salt) are stored in a *secured* manner if ($_POST['username'] === 'hello' &amp;&amp; $_POST['password'] === 'world') { $_SESSION['username'] = $_POST['username']; header('location:welcome.php'); } ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Login issue&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div class='badassredcolor'&gt;Invalid username or password. Please &lt;a href='login.php'&gt;login again&lt;/a&gt;.&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>checklogin.php will see if the username and password are right. If they are right, remember their username and send the person to welcome.php. Otherwise ask them to login again. Of course, the process should be much more graceful than this but this is just an example.</p> <p>Then, your welcome page will show them a welcome message like so:</p> <pre><code>&lt;?php session_start(); ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Welcome to the awesome website&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Welcome &lt;?php echo $_SESSION['username']; ?&gt;&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Hope this helps.</p> <p>Alright, let's say we want to use a single page to do show form and check login/password, use the example below. It is a quick draft that you can modify. In real world, you would do it a lot different but this example is fine for learning:</p> <pre><code>&lt;?php session_start(); // let's hold a variable to check if the user POST'ed the form // or if we got to this page without POST'ing to itself $isPosted = false; // let's first check if user POST'ed anything if (isset($_POST['username']) &amp;&amp; isset($_POST['password'])) { // okay, we got something. Let's flag the variable $isPosted = true; // Let's check if the username and password are good. If good, let's set up // a session variable appropriately if ($_POST['username'] === 'hello' &amp;&amp; $_POST['password'] === 'world') { $_SESSION['username'] = $_POST['username']; } } else { // seems like the form was not posted with any interesting data // or it is a fresh page-load. Let's ensure that no session variables // are alive if(isset($_SESSION['username'])) { session_unset(); } } ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt;&lt;title&gt;Login&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;?php // if the form was posted and session was set, show welcome message if ( $isPosted === true &amp;&amp; (isset($_SESSION['username'])) ) { ?&gt; &lt;div class='greetings'&gt;Welcome, &lt;?php echo htmlentities($_SESSION['username']); ?&gt;&lt;/div&gt; &lt;div&gt;more cool things here&lt;/div&gt; &lt;?php } // oh, either the page was freshly loaded or session was not set. Cool, show form first else { ?&gt; &lt;form id='loginform' action='newlogin.php' method='post'&gt; &lt;div&gt;Username: &lt;input type='text' id='username' name='username' /&gt;&lt;/div&gt; &lt;div&gt;Password: &lt;input type='password' id='password' name='password' /&gt;&lt;/div&gt; &lt;div&gt;&lt;input type='submit' id='submit' value='Login'&gt;&lt;/div&gt; &lt;/form&gt; &lt;?php // if the form was posted and we know that session is not set, // let's show an error if ($isPosted) { ?&gt; &lt;div class='badassREDcolor'&gt;Invalid username or password&lt;/div&gt; &lt;?php } } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    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.
 

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