Jump to content

[SOLVED] Email validation


pro_se

Recommended Posts

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..

Link to comment
Share on other sites

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
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.