PHP login function code Example of PHP implementing a simple login function

WBOY
Release: 2016-07-25 08:55:58
Original
834 people have browsed it
  1. /**
  2. * php login function
  3. * edit: bbs.it-home.org
  4. */
  5. // Remove unsafe html code for php and mysql.
  6. function safestrip($string){
  7. $string = strip_tags($string);
  8. $string = mysql_real_escape_string($string);
  9. return $string;
  10. }
  11. //Login information display function
  12. function messages() {
  13. $message = '';
  14. if($_SESSION['success'] != '') {
  15. $message = ''
  16. .$_SESSION['success'].'< ;/span>';
  17. $_SESSION['success'] = '';
  18. }
  19. if($_SESSION['error'] != '') {
  20. $message = ' .$_SESSION['error'].'';
  21. $_SESSION['error'] = '';
  22. }
  23. return $message;
  24. }
  25. // User login function
  26. function login($username, $password){
  27. //Filter the username and password entered by the user
  28. $user = safestrip($username);
  29. $pass = safestrip($password);
  30. //Convert the password to md5 format
  31. $pass = md5($pass);
  32. // Query whether the username and password in the database match
  33. $sql = mysql_query("SELECT * FROM user_table WHERE username = '$user'
  34. AND password = '$pass'") or die(mysql_error());
  35. //If = 1, the authentication is successful
  36. if (mysql_num_rows($sql) == 1) {
  37. //Start recording in the session
  38. $_SESSION['authorized'] = true;
  39. // Reload the page
  40. $_SESSION['success'] = 'Login successful';
  41. header('Location: ./index.php');
  42. exit;
  43. } else {
  44. // Login failure record In session
  45. $_SESSION['error'] = 'Sorry, the username or password you entered is incorrect';
  46. }
  47. }
  48. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!