PHP user registration form


SUBMITTED BY: Guest

DATE: Oct. 22, 2013, 12:52 p.m.

FORMAT: PHP

SIZE: 3.6 kB

HITS: 1079

  1. <?php
  2. if(isset($_POST['submit'])){
  3. # connect to the database here
  4. # search the database to see if the user name has been taken or not
  5. $query = sprintf("SELECT * FROM users WHERE user_name='%s' LIMIT 1",mysql_real_escape_string($_POST['user_name']));
  6. $sql = mysql_query($query);
  7. $row = mysql_fetch_array($sql);
  8. #check too see what fields have been left empty, and if the passwords match
  9. if($row||empty($_POST['user_name'])|| empty($_POST['fname'])||empty($_POST['lname'])|| empty($_POST['email'])||empty($_POST['password'])|| empty($_POST['re_password'])||$_POST['password']!=$_POST['re_password']){
  10. # if a field is empty, or the passwords don't match make a message
  11. $error = '<p>';
  12. if(empty($_POST['user_name'])){
  13. $error .= 'User Name can\'t be empty<br>';
  14. }
  15. if(empty($_POST['fname'])){
  16. $error .= 'First Name can\'t be empty<br>';
  17. }
  18. if(empty($_POST['lname'])){
  19. $error .= 'Last Name can\'t be empty<br>';
  20. }
  21. if(empty($_POST['email'])){
  22. $error .= 'Email can\'t be empty<br>';
  23. }
  24. if(empty($_POST['password'])){
  25. $error .= 'Password can\'t be empty<br>';
  26. }
  27. if(empty($_POST['re_password'])){
  28. $error .= 'You must re-type your password<br>';
  29. }
  30. if($_POST['password']!=$_POST['re_password']){
  31. $error .= 'Passwords don\'t match<br>';
  32. }
  33. if($row){
  34. $error .= 'User Name already exists<br>';
  35. }
  36. $error .= '</p>'
  37. }else{
  38. # If all fields are not empty, and the passwords match,
  39. # create a session, and session variables,
  40. $query = sprintf("INSERT INTO users_table(`user_name`,`f_name`,`l_name`,`email`,`password`)
  41. VALUES('%s','%s','%s','%s',PASSWORD('%s'))",
  42. mysql_real_escape_string($_POST['user_name']),
  43. mysql_real_escape_string($_POST['fname']),
  44. mysql_real_escape_string($_POST['lname']),
  45. mysql_real_escape_string($_POST['email']),
  46. mysql_real_escape_string($_POST['password']))or die(mysql_error());
  47. $sql = mysql_query($query);
  48. # Redirect the user to a login page
  49. header("Location: login.php");
  50. exit;
  51. }
  52. }
  53. # echo out each variable that was set from above,
  54. # then destroy the variable.
  55. if(isset($error)){
  56. echo $error;
  57. unset($error);
  58. }
  59. ?>
  60. <!-- Start your HTML/CSS/JavaScript here -->
  61. <form action=" <? echo $_SERVER['PHP_SELF']; ?> " method="post">
  62. <p>User Name:<br /><input type="text" name="user_name" <? if(!$row){echo 'value="'.$_POST['user_name'].'"';} ?> /></p>
  63. <p>First Name:<br /><input type="text" name="fname" <? echo 'value="'.$_POST['fname'].'"'; ?> /></p>
  64. <p>Last Name:<br /><input type="text" name="lname" <? echo 'value="'.$_POST['lname'].'"'; ?> /></p>
  65. <p>Email:<br /><input type="text" name="email" <? echo 'value="'.$_POST['email'].'"'; ?> /></p>
  66. <p>Password:<br /><input type="password" name="password" /></p>
  67. <p>Re-Type Password:<br /><input type="password" name="re_password" /></p>
  68. <p><input type="submit" name="submit" value="Sign Up" /></p>
  69. </form>

comments powered by Disqus