Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  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.
  4.  
  5. if(isset($_POST['login'])) {
  6. //once the page has loaded, if the submit button is set then carry on with the login
  7.  
  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
  10.  
  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.
  14.  
  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
  17.  
  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
  21.  
  22.  
  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:
  26.  
  27. header('Location: news.php');
  28.  
  29. //provided that the first page you want to user to be logged in to is the page news.php
  30.  
  31.  
  32. }
  33.  
  34. }
  35.  
  36. }
  37.  
  38. ?>
  39.  
  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" />
  44.  
  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 -->
  46.  
  47. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement