Advertisement
Guest User

login

a guest
May 6th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. <?php
  2. session_start();
  3. include("connection.php"); //Establishing connection with our database
  4.  
  5. $error = ""; //Variable for storing our errors.
  6. if(isset($_POST["submit"]))
  7. {
  8. if(empty($_POST["username"]) || empty($_POST["password"]))
  9. {
  10. $error = "Both fields are required.";
  11. }else
  12. {
  13. // Define $username and $password
  14. $username=$_POST['username'];
  15. $password=$_POST['password'];
  16.  
  17. // To protect from MySQL injection
  18. $username = stripslashes($username);
  19. $password = stripslashes($password);
  20. $username = mysql_real_escape_string($db, $username);
  21. $password = mysql_real_escape_string($db, $password);
  22. $password = md5($password);
  23.  
  24. //Check username and password from database
  25. $sql="SELECT uid FROM users WHERE username='$username' and password='$password'";
  26. $result=mysql_query($db,$sql);
  27. $row=mysql_fetch_array($result,MYSQL_ASSOC);
  28.  
  29. //If username and password exist in our database then create a session.
  30. //Otherwise echo error.
  31.  
  32. if(mysql_num_rows($result) == 1)
  33. {
  34. $_SESSION['username'] = $username; // Initializing Session
  35. header("location: home.php"); // Redirecting To Other Page
  36. }else
  37. {
  38. $error = "Incorrect username or password.";
  39. }
  40.  
  41. }
  42. }
  43.  
  44. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement