Guest User

Untitled

a guest
Jul 24th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. <?php // file users/login.php
  2. //start session
  3. session_start();
  4. // build db connection, note database.php is one directory level above this file
  5. require_once('db_config.php');
  6. // username and password sent from form
  7. //Protect from MySQL Injection
  8. //strip out commands, and cut down amount of strings to 32
  9. $username = strip_tags(substr($_POST['username'],0,32));
  10. $password = strip_tags(substr($_POST['password'],0,32));
  11.  
  12. //use PHP crypt function (one way encryption - meaning once it's encrypted it cannot be changed back (decrypted).
  13.  
  14. //using the salt encryption depends on the character set you want to encrypt the default is (two-character salts)
  15.  
  16. //MD5 is a 12 character salt but higher level salts can be used depending on the level of security required
  17. $cleanpw = crypt(md5($password), md5($username));
  18.  
  19. //use MySWL_Real_Escape to encrypt the password
  20. // table users:
  21. $query = "SELECT username, password FROM users WHERE username='".mysql_real_escape_string($username)."' AND password='".mysql_real_escape_string($password)."'limit 1";
  22. $login_result = mysql_query($query);
  23. //print out the query result for testing
  24. //print_r($login_result);
  25. $count=mysql_num_rows($login_result);
  26. //print out the number of rows
  27. //print_r($count);
  28.  
  29. //check if the query was succcessful
  30. if($count==1) { // test for single row
  31.  
  32. // $rows=$login_result->fetch();
  33. // set session var for successful login and check for user authentication
  34. $_SESSION['username']= $count['username'];
  35. // print_r($_SESSION);//print out session for testing
  36. header('Location:login_success.php');
  37. }
  38. else{
  39. echo "wrong username and password";
  40. }
  41. ?>
Add Comment
Please, Sign In to add comment