a guest Jun 8th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. session_start();
  3. //this must be included at the top of every page that requires the use of sessions to enable them to be accessed and passed over to other pages.
  5. if(isset($_POST['login'])) {
  6. //once the page has loaded, if the submit button is set then carry on with the login
  8.     if(isset($_POST['username']) && isset($_POST['password'])) {
  9.     //if the username and password text fields are present on the page once the page has been posted then carry on with the login
  11.         $query = mysql_query('SELECT `username` FROM `members` WHERE `username` = '.$_POST['username'].' AND `password` = '.$_POST['password']);
  12.         //this is a mysql query. for this to work the page must also have a mysql connection at the top to connect with the database, which in this script it doesn't
  13.         this query is just attempting to select a record from the members table of the database where the username and password are the same as you have entered.
  15.         if(mysql_num_rows($query) == 1) {
  16.         //if the number of rows this query returns is 1, which it should only ever be if it does exist, then log the user in
  18.             $username = mysql_fetch_array($query);
  19.             //what this does is create an array of information from the record we selected earlier. as we are only selecting the username in this query, we can access it through the variable $username[0]
  20.             the reason we want to get the username from the database is so that it gives the username in its correct casing, as people do not generally type correct casing in text boxes
  23.             $_SESSION['username'] = $username[0];
  24.             //here we've creating a new session called username. this will be how each script around your site will find out the username of the person who is currently viewing the page. it's also assigning the correctly cased username of the person who is attempting to log in
  25.             Now that the session is created, we just need to forward the user inside the side to the logged in section. we do that through the following statement:
  27.             header('Location: news.php');
  29.             //provided that the first page you want to user to be logged in to is the page news.php
  32.         }
  34.     }
  36. }
  38. ?>
  40. <form action="" method="post">  <!-- this form tag must surround the text fields and button you wish to pass the details on from -->
  41.     username: <input type="text" name="username" /><br />
  42.     password: <input type="password" name="password" /><br />
  43.     <input type="submit" name="login" value="LOGIN" />
  45.     <!-- notice that the text fields are named username and password and the submit button is named login. these will be how php identifies these objects once the page has been posted -->
  47. </form>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand