Guest User

Untitled

a guest
Jul 14th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. if(isset($_POST['login']))
  2. {
  3.  
  4. if(!$_POST['username'] || strlen($_POST['username']) <= 3 || strlen($_POST['username']) >= 20) //Check user input for validity
  5. {
  6. $loginerror['username'] = "Username is required. Must be between 3 and 20 characters long.";
  7. }
  8. if(!$_POST['password'])
  9. {
  10. $loginerror['password'] = "Password is required.";
  11. }
  12.  
  13.  
  14. if(count($loginerror) == 0)
  15. {
  16. $username = mysql_real_escape_string(trim($_POST['username'])); //Do whatever to the user input
  17. $password = mysql_reql_escape_string(trim($_POST['password']));
  18.  
  19. $sql = mysql_query("SELECT `username`,`password`,`etc` FROM `users` WHERE `username` = '$username' AND `password` = '$password' LIMIT 1"); //Select both at the same time
  20.  
  21. if(mysql_num_rows($sql) == 0)
  22. {
  23. $loginerror['login'] = "Username or Password incorrect or does not exist."; //It's smart not to let people know which they got wrong.
  24. }
  25. else
  26. {
  27. $_SESSION['username'] = $username;
  28. $_SESSION['loggedin'] = true;
  29. $loginmessage = 'Welcome ' . $username. ', you are successfully logged in';
  30. }
  31. }
  32. }
  33.  
  34. function dispError($name,&$errors)
  35. {
  36. if(isset($errors[$name]))
  37. {
  38. return '<span class="error">' . $errors[$name] . '</span>';
  39. }
  40. return '';
  41. }
  42.  
  43. if(isset($loginmessage))
  44. {
  45. echo $loginmessage;
  46. }
  47. elseif(isset($_SESSION['username']) && isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true)
  48. {
  49. echo 'Welcome, ' . $_SESSION['username'];
  50. }
  51. else
  52. {
  53. if(!isset($loginerror) || !is_array($loginerror))
  54. {
  55. $loginerror = array(); //Gotta make sure it exists for the next part if it hasn't been set.
  56. }
  57. echo dispError('login',$loginerror);
  58. echo '<form method="post" action="">';
  59. echo '<input name="username" placeholder="Username..." type="text" maxlength="15" />' . dispError('username',$loginerror) . '<br /><br />';
  60. echo '<input name="password" placeholder="Password..." type="password" maxlength="20" />' . dispError('password',$loginerror) . '<br /><br />';
  61. echo '<input name="login" type="submit" value="Login" style="width:100px;">';
  62. echo '</form>';
  63. }
  64.  
  65. if(isset($_POST['login']))
  66. {
  67. require_once("loginCheck.php");
  68. }
  69.  
  70. //various other includes and requires
  71.  
  72. require_once("loginForm.php");
  73.  
  74. $_SESSION['loggedin'] = true;
  75.  
  76. <?php
  77. session_start();
  78.  
  79. if(!loggedIn())
  80. {
  81. header('Location: login.php');
  82. die();
  83. }
  84.  
  85. function loggedIn()
  86. {
  87. if($_SESSION['loggedin'])
  88. return true;
  89. else
  90. return false;
  91. }
  92. ?>
Add Comment
Please, Sign In to add comment