Guest User

Untitled

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