Guest User

Untitled

a guest
Aug 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. PHP Secure Session Login - Best Practice
  2. // Start session
  3. session_start();
  4. // Is the user already logged in?
  5. if (isset($_SESSION['username'])) {
  6. header('Location: members-only-page.php');
  7. }
  8.  
  9. // vars
  10. login string post
  11. password string post
  12.  
  13. // validation aside from ajax now
  14. login string is empty
  15. redirect to login form with error
  16. password string is empty
  17. redirect to login form with error
  18.  
  19. // mysql
  20. escape strings
  21. clean html strings
  22.  
  23. mysql connect external mysql server
  24. if login string is user
  25. if password md5 match with database md5
  26. session logged in
  27. else
  28. session failed password invalid
  29. redirect to login form user/pass error
  30. end if
  31. else
  32. session failed username invalid
  33. redirect to login form user/pass error
  34. end if
  35.  
  36. if file called direct
  37. redirect 404
  38. alert_admin function type hacking attempt login page
  39. end if
  40.  
  41. function sanitize($str)
  42. {
  43. $str = trim($str);
  44.  
  45. if (get_magic_quotes_gpc())
  46. $str = stripslashes($str);
  47.  
  48. return htmlentities(mysql_real_escape_string($str));
  49. }
Add Comment
Please, Sign In to add comment