Advertisement
gitlez

YA: Form Registration With Check and Addition to Database WOC

May 11th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2.  
  3. /* In response to a Yahoo Answer's Question */
  4.  
  5. /*    Check if Page has been POSTed to    */
  6. if( $_SERVER['REQUEST_METHOD'] !== 'POST'){
  7.     echo '<h1>Invalid Permission</h1><p>Please Fill out the form first.</p>';
  8.     exit;
  9. }
  10.  
  11. /*    Functions to help with checks    */
  12. function minLength($str, $len){
  13.     return (strlen($str) >= $len);
  14. }
  15. function maxLength($str, $len){
  16.     return (strlen($str) <= $len);
  17. }
  18. function pvar($name, $mysqlEscape=false){
  19.     $v = (isset($_POST[$name]))? $_POST[$name] : '';
  20.     return ($mysqlEscape)? mysql_real_escape_string(trim($v)) : trim($v);
  21. }
  22. function hasValue(){
  23.     $args = func_get_args();
  24.     foreach($args as $arg){
  25.         if(!isset($args{0})){
  26.             return false;
  27.         }
  28.     }
  29.     return true;
  30. }
  31.  
  32. /*    Connect To the database    */
  33. $connect = mysql_connect("localhost","root","") or die('Internal Error. Couldn\'t connect to database');
  34. mysql_select_db("thorbis", $connect) or die('Internal Error. Could\'t select database');
  35.  
  36. /*    Variable Definitions with the help of pvar() function    */
  37. $fullname = strip_tags(pvar('fullname', true));
  38. $username = strtolower(strip_tags(pvar('username', true)));
  39. $password = strip_tags(pvar('password', true));
  40. $repeatpassword = strip_tags(pvar('repeatpassword', true));
  41. $email = strip_tags(pvar('email', true));
  42. $firstname = pvar('firstname', true);
  43. $lastname = pvar('lastname', true);
  44. $phone = pvar('phone', true);
  45. $address1 = pvar('address1', true);
  46. $address2 = pvar('address2', true);
  47. $country = pvar('counry', true);
  48. $state = pvar('state', true);
  49. $city = pvar('city', true);
  50. $zip = pvar('zip', true);
  51. $date = date("Y-m-d");
  52. $errorMsg = '';
  53.  
  54. /*    Check for Input Values    */
  55. if (!hasValue($fullname, $username, $password, $repeatpassword, $email, $firstname, $lastname, $phone, $address1, $address2, $country, $state, $city, $zip)){
  56.     $errorMsg .= 'All Fields are Required.<br>';
  57. }
  58.  
  59. /*    Check for Existing Username    */
  60. $namecheck = mysql_query("SELECT username FROM users WHERE username='$username' LIMIT 1") or die(mysql_error($connect));
  61. if( mysql_num_rows($namecheck) > 0){
  62.     $errorMsg .= "Username is already Registered! Please select another.<br>";
  63. }
  64.  
  65. /*    Check Passwords Match and Length   */
  66. if( !minLength($password, 6)){
  67.     $errorMsg .= 'Password needs to be a minimum of 6 characters in length.<br>';
  68. if ($password !== $repeatpassword){
  69.     $errorMsg .= 'Password and Confirmation Password DO NOT MATCH.<br>';
  70. }else{
  71.     $password = md5($password);
  72. }
  73.  
  74. /*    Username Length Checks and Fullname length checks    */
  75. if( !maxLength($username, 25) || !maxLength($fullname, 25)){
  76.     $errorMsg .= "Length of username or fullname is too long!<br>";
  77. }
  78.  
  79. /*    Output Error Message, if there is one. Otherwise, register User.    */
  80. if( strlen($errorMsg) > 0){
  81.     echo $errorMsg;
  82. }else{
  83.     $result = mysql_query( "INSERT INTO users(fullname, username, password,...) VALUES('{$fullname}','{$username}','{$password}',...)");
  84.     if($result){
  85.         echo "You have been registered! <a href='index.php'>click here</a> to go login";
  86.     }else{
  87.         echo 'There has been an Internal Error. Please  Try Again Later. ';
  88.  
  89.         /*    echo '<br>' . mysql_error($connect);    */
  90.     }
  91. }
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement