Guest User

Untitled

a guest
Jun 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. <?php
  2. // login2.php
  3. include("connection.php");
  4.  
  5. // Start a session. Session is explained below.
  6. session_start();
  7.  
  8. // Same checking stuff all over again.
  9. if(isset($_POST['submit'])) {
  10. if(empty($_POST['username']) || empty($_POST['password'])) {
  11. echo "Sorry, you have to fill in all forms";
  12. exit;
  13. }
  14. // Create the variables again.
  15. $username = $_POST['username'];
  16. $password = $_POST['password'];
  17. // Encrypt the password again with the md5 hash.
  18. // This way the password is now the same as the password inside the database.
  19. $password = md5($password);
  20. // Store the SQL query inside a variable.
  21. // ONLY the username you have filled in is retrieved from the database.
  22. $query = "SELECT username,password
  23. FROM `users`
  24. WHERE username='$username'";
  25.  
  26. $result = mysql_query($query);
  27. if(!$result) {
  28. // Gives an error if the username given does not exist.
  29. // or if something else is wrong.
  30. echo "The query failed " . mysql_error();
  31. } else {
  32. // Now create an object from the data you've retrieved.
  33. $row = mysql_fetch_object($result);
  34. // You've now created an object containing the data.
  35. // You can call data by using -> after $row.
  36. // For example now the password is checked if they're equal.
  37.  
  38. // By storing data inside the $_SESSION superglobal,
  39. // you stay logged in until you close your browser.
  40. $_SESSION['username'] = $username;
  41. $_SESSION['sid'] = session_id();
  42. // Make it more secure by storing the user's IP address.
  43. $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
  44. // Now give the success message.
  45. // $_SESSION['username'] should print out your username.
  46. echo "You are now logged in as " . $_SESSION['username'] . ", you will be redirected in: ";
  47. }
  48. }
  49. ?>
Add Comment
Please, Sign In to add comment