pro_se Posted April 8, 2007 Share Posted April 8, 2007 Hello, I am working on a project and I am stuck... I am looking for a way to validate email addresses when someone registers for my website, this is what I have so far and its not working... //register user function function registeruser($fname, $lname, $uname, $pword, $cpword, $email) { $fname = ucfirst($fname); $lname = ucfirst($lname); $sql_user_check = "SELECT * FROM user WHERE username='$uname'"; $result_name_check = mysql_query($sql_user_check); $usersfound = mysql_num_rows($result_name_check); $pword = md5($pword); $cpword = md5($cpword); if ($_SESSION['logged_in'] == 'yes') { header("Location: http://notexting.com/error"); } //check if username exists\\ elseif ($usersfound > 1) { header("Location: http://notexting.com/error/username"); } //check if passwords match\\ elseif ($cpword !== $pword) { header("Location: http://notexting.com/error/password"); } //check if email is vaild\\ elseif (checkemail($email)) { header("Location: http://notexting.com/error/email"); } else { $result=MYSQL_QUERY("INSERT INTO user (id,fname,lname,username,email,password)". "VALUES ('NULL', '$fname', '$lname', '$uname', '$email', '$pword')"); header("Location: http://notexting.com/register/success"); } } function checkemail($email) { if (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)) { return false; } return true; } I think something in my if/else structure is wrong because if I enter wrong information in particular orders sometimes it responds different.. It would be greatly appreciated if someone could take a look at it and tell me what may be the problem.. Quote Link to comment Share on other sites More sharing options...
pro_se Posted April 9, 2007 Author Share Posted April 9, 2007 any ideas? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 9, 2007 Share Posted April 9, 2007 i have no clue, never done it, but i looked it up on google... and found this one... it even validates the email domain so people can't type like "you@suck.com"... http://www.devshed.com/c/a/PHP/Email-Address-Verification-with-PHP/ hope that helped you a little =) Quote Link to comment Share on other sites More sharing options...
esukf Posted April 9, 2007 Share Posted April 9, 2007 Here's a revised version of your code your can try. <?php //register user function function registeruser($fname, $lname, $uname, $pword, $cpword, $email) { $fname = ucfirst($fname); $lname = ucfirst($lname); $sql_user_check = "SELECT * FROM user WHERE username='".mysql_real_escape_string($uname)."'"; $result_name_check = mysql_query($sql_user_check); $usersfound = mysql_num_rows($result_name_check); $pword = md5($pword); $cpword = md5($cpword); if ($_SESSION['logged_in'] == 'yes') { header("Location: http://notexting.com/error"); } //check if username exists\\ if ($usersfound > 0) { header("Location: http://notexting.com/error/username"); } //check if passwords match\\ if ($cpword !== $pword) { header("Location: http://notexting.com/error/password"); } //check if email is vaild\\ if (checkemail($email)) { header("Location: http://notexting.com/error/email"); } else { $sql = sprintf("INSERT INTO user (fname,lname,username,email,password)". " VALUES ('%s', '%s', '%s', '%s', '%s')", mysql_real_escape_string($fname), mysql_real_escape_string($lname), mysql_real_escape_string($uname), mysql_real_escape_string($email), mysql_real_escape_string($pword) ); $result = mysql_query($sql) or die(mysql_error()); if($result){ header("Location: http://notexting.com/register/success"); } else { header("Location: http://notexting.com/register/failed"); } } } ?> Not sure how your site is structured but a better way than redirecting on every error, is to use an array to hold the errors when you validate the variables. You then check if any error exists in the array and handle the error. For Example <?php $errors = array(); //array to holding any errors if ($usersfound > 0) { $errors[] = "User already exists"; } //check if passwords match\\ if ($cpword !== $pword) { $errors[] = "Confirmation password doesn't match"; } //check if email is vaild\\ if (checkemail($email)) { $errors[] = "email entered is invalid"; } if(count($errors) > 0){ //display errors/return the error array etc echo "errors found"; foreach($errors as $error){ echo $error; } } else { //do your db stuff etc } ?> Quote Link to comment Share on other sites More sharing options...
pro_se Posted April 9, 2007 Author Share Posted April 9, 2007 THANKS! YOU`RE ALL WONDERFUL! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.