Simple Register Function


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



Copy this code and paste it in your HTML
  1. function register( $user, $pass, $pass2 )
  2. {
  3. if ( isset( $_POST['register'] ) )
  4. {
  5. //Escape is a function with mysql_real_escape_string() and more
  6. $user = escape( $user );
  7. $pass = escape( $pass );
  8. $pass2 = escape( $pass2 );
  9.  
  10. if ( empty( $user ) )
  11. {
  12. $error = "Username missing!";
  13. }
  14. else if ( empty( $pass ) )
  15. {
  16. $error = "Password missing!";
  17. }
  18. else if ( empty( $pass2 ) )
  19. {
  20. $error = "Please repeat your password!";
  21. }
  22. else if ( $pass != $pass2 )
  23. {
  24. $error = "Your passwords do not match!";
  25. }
  26. else if ( strlen( $user ) > 25 )
  27. {
  28. $error = "Username is to long, 25 characters max!";
  29. }
  30. else if ( strlen( $pass ) > 25 )
  31. {
  32. $error = "Password is to long, 25 characters max!";
  33. }
  34. else if ( strlen( $pass ) < 5 )
  35. {
  36. $error = "Password is to short, 5 characters min!";
  37. }
  38. //I use trim to check if a user has a username with only whitespace, not using regex since having a whitespace in the username should be ok
  39. else if ( trim( $user ) == "" )
  40. {
  41. $error = "Usernames only containing white-spaces is not allowed!";
  42. }
  43. else
  44. {
  45. $fetch = mysql_query( "SELECT `user` FROM `user` WHERE `user` = '".$user."'" ) or die ( mysql_error() );
  46. if ( mysql_num_rows( $fetch ) != 0 )
  47. {
  48. $error = "That username is already taken!";
  49. }
  50. else
  51. {
  52. $pass = md5( $pass );
  53. mysql_query( "INSERT INTO `user` ( `user`, `pass` ) VALUES ( '".$user."', '".$pass."' )" ) or die ( mysql_error() );
  54. }
  55. }
  56. }
  57. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.