Simple Login Function


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function login( $user, $pass )
  2. {
  3. if ( isset( $_POST['login'] ) )
  4. {
  5. $user = escape( $user );
  6. $pass = escape( $pass );
  7.  
  8. if ( empty( $user ) )
  9. {
  10. $error = "Username field is empty!";
  11. }
  12. else if ( empty( $pass ) )
  13. {
  14. $error = "Password field is empty!";
  15. }
  16. else
  17. {
  18. $pass = md5( $pass );
  19. $fetch = mysql_query( "SELECT * FROM `user` WHERE `user` = '".$user."' AND `pass` = '".$pass."' LIMIT 1" ) or die ( mysql_error() );
  20. if ( mysql_num_rows( $fetch ) != 1 )
  21. {
  22. $error = "Username or password is incorrect!";
  23. }
  24. else
  25. {
  26. $got = mysql_fetch_array( $fetch );
  27. $_SESSION['id'] = $got['id'];
  28. header( "Location: home.php" );
  29. }
  30. }
  31. }
  32. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.