Jump to content

ereg and preg_match


DeanWhitehouse

Recommended Posts

Ok, i now have this

if(isset($_POST['signup']))
{
	if ($user_name && $user_password && $user_password2 && $user_email && $user_email2)
	{
	$checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
	if (preg_match($checkemail, $user_email)) {
	$user_check = mysql_num_rows(mysql_query("SELECT * FROM $user WHERE user_email='$user_email' OR user_name='$user_name'"));
	$length = strlen($user_password);
	$length1 = strlen($user_name);
		if ($length >= 6)
		{
		if ($length1 >= 4)
		{
		if ($user_check >= 1)
		{
		require_once 'register.php';
		echo "Sorry, but the username or email you requested is already in use. Please enter a different username.";
		}

			elseif ($user_password == $user_password2 && $user_email == $user_email2)
			{
			$userPswd = md5($user_password);
			$userpwsd = sha1($userPswd);
			mysql_query("INSERT INTO `$user` (user_id,  user_name, user_password, user_email, user_ip, userlevel) VALUES 		('','$user_name','$userpwsd','$user_email','$ip','2')")
			or die('Error ' . mysql_error());
			echo "Succesfully Registered. You will recieve a confirmation email shortly.";
			header ("http://".$_SERVER[HTTP_HOST].$success);
 			}
			}
			}
		}
			if($length <= 5)
			{
			require_once 'register.php';
			echo "Password Too Short<br>";
			}
			if($length1 <= 3)
			{
			require_once 'register.php';
			echo "Username Too Short.<br>";
			}
				if(!$checkpw)
				{
				require_once 'register.php';
				echo "The passwords you entered do not match. Please check these details and try again.<br>";
				}
				if(!$checkem)
				{
				require_once 'register.php';
				echo "The emails you entered do not match. Please check these details and try again.<br>";
				}
				}
			elseif (!preg_match($checkemail, $user_email)) {
   		 		echo "Invalid e-mail address<br>";
			}
				 else 
					{
					require_once 'register.php';
					echo "Please fill in all of the required fields.";
					}
}

?>

but i get an error when there is nothing entered and it also stops anything below it working properly

 

Link to comment
Share on other sites

but when i use this code,

<?php
// Random Game Design: PHP Website Template
//  Version 1
//  Copyright Dean Whitehouse, 2008

require_once 'includes/db_connect.php';

// Connect to database
mysql_connect($dbhost,$dbuser,$dbpass)
or die('Could not connect: ' . mysql_error());

$user_name = mysql_real_escape_string($_POST["user_name"]);		
$user_password =mysql_real_escape_string($_POST["user_password"]);		
$user_password2 =mysql_real_escape_string ($_POST["user_password2"]);
$user_email =mysql_real_escape_string ($_POST["user_email"]);		
$user_email2 =mysql_real_escape_string ($_POST["user_email2"]);
$ip = $_SERVER['REMOTE_ADDR'];
$success = "/reg_success.php";
$checkpw = $user_password == $user_password2;
$checkem = $user_email == $user_email2;
if ($_SESSION['is_valid'] == false)
{

//if(isset($_POST['agree'])){
if(isset($_POST['signup']))
{
	if ($user_name && $user_password && $user_password2 && $user_email && $user_email2)
	{
	$checkmail = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
	if (preg_match($checkmail, $user_email)) {
	$user_check = mysql_num_rows(mysql_query("SELECT * FROM $user WHERE user_email='$user_email' OR user_name='$user_name'"));
	$length = strlen($user_password);
	$length1 = strlen($user_name);
		if ($length >= 6)
		{
		if ($length1 >= 4)
		{
		if ($user_check >= 1)
		{
		require_once 'register.php';
		echo "Sorry, but the username or email you requested is already in use. Please enter a different username.";
		}

			elseif ($user_password == $user_password2 && $user_email == $user_email2)
			{
			$userPswd = md5($user_password);
			$userpwsd = sha1($userPswd);
			mysql_query("INSERT INTO `$user` (user_id,  user_name, user_password, user_email, user_ip, userlevel) VALUES 		('','$user_name','$userpwsd','$user_email','$ip','2')")
			or die('Error ' . mysql_error());
			echo "Succesfully Registered. You will recieve a confirmation email shortly.";
			header ("http://".$_SERVER[HTTP_HOST].$success);
 			}
			}
			}
		}
			if($length <= 5)
			{
			require_once 'register.php';
			echo "Password Too Short<br>";
			}
			if($length1 <= 3)
			{
			require_once 'register.php';
			echo "Username Too Short.<br>";
			}
				if(!$checkpw)
				{
				require_once 'register.php';
				echo "The passwords you entered do not match. Please check these details and try again.<br>";
				}
				if(!$checkem)
				{
				require_once 'register.php';
				echo "The emails you entered do not match. Please check these details and try again.<br>";
				}
				}
			if (!preg_match($checkmail, $user_email)) {
   		 		echo "Invalid e-mail address<br>";
			}
				 else 
					{
					require_once 'register.php';
					echo "Please fill in all of the required fields.";
					}
}

?>
<html>
<table bgcolor='#999999' align='center' width="400px">
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='POST'>
<tr><td width="10px">Username: </td><td><input type='text' name='user_name' maxlength="20" width="400px" /><br /></td></tr>
<tr><td width="10px">Maximum Length<br /> 20 characters.</td></tr>
<tr><td width="10px">E-mail Address:</td> <td><input type='text' name='user_email' /><br /></td></tr>
<tr><td width="10px">Confirm E-mail Address: </td><td><input type='text' name='user_email2' /><br /></td></tr>
<tr><td width="10px">Password:</td><td> <input type='password' name='user_password' maxlength="30" /><br /></td></tr>
<tr><td width="10px">Min. 6<br>Max. 10</td></tr>
<tr><td width="10px">Confirm Password:</td><td> <input type='password' name='user_password2'  maxlength="30"/><br /></td></tr>
<tr><td><input type='submit' value='Complete Registration' name='signup' /></td><td><input type="reset" value="Reset Fields" name="reset" /></td></tr>
</form>
</table>
</html>
<?php
mysql_close();
}
else
{
header("Location:http://".$_SERVER[HTTP_HOST]);
}
?>

 

if it is a valid email it works if it isn't i get echoed there aswell

Password Too Short

Username Too Short.

even when it isn't, and this only happens when the regex is there

Link to comment
Share on other sites

It is a long script so u should debug it yourself. What i can see is multiple levels of error checking, happening more then once. Normally u should have:

 

if(this){
   if(that){
       do smth
   } else{
       dont do that
   }
} else{
   dont do this
}

 

Correctly nested if-else statements should have the work done.

Link to comment
Share on other sites

ok, but that still leaves the error, this is part of the code, where it is happening

$checkmail = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
	if (preg_match($checkmail, $user_email)) {
	$user_check = mysql_num_rows(mysql_query("SELECT * FROM $user WHERE user_email='$user_email' OR user_name='$user_name'"));
	$length = strlen($user_password);
	$length1 = strlen($user_name);
		if ($length >= 6)
		{
		if ($length1 >= 4)
		{
		if ($user_check >= 1)
		{
		require_once 'register.php';
		echo "Sorry, but the username or email you requested is already in use. Please enter a different username.";
		}

			elseif ($user_password == $user_password2 && $user_email == $user_email2)
			{
			$userPswd = md5($user_password);
			$userpwsd = sha1($userPswd);
			mysql_query("INSERT INTO `$user` (user_id,  user_name, user_password, user_email, user_ip, userlevel) VALUES 		('','$user_name','$userpwsd','$user_email','$ip','2')")
			or die('Error ' . mysql_error());
			echo "Succesfully Registered. You will recieve a confirmation email shortly.";
			header ("http://".$_SERVER[HTTP_HOST].$success);
 			}
			}
			}
		}
			if($length <= 5)
			{
			require_once 'register.php';
			echo "Password Too Short<br>";
			}
			if($length1 <= 3)
			{
			require_once 'register.php';
			echo "Username Too Short.<br>";
			}
				if(!$checkpw)
				{
				require_once 'register.php';
				echo "The passwords you entered do not match. Please check these details and try again.<br>";
				}
				if(!$checkem)
				{
				require_once 'register.php';
				echo "The emails you entered do not match. Please check these details and try again.<br>";
				}
				}
			if (!preg_match($checkmail, $user_email)) {
   		 		echo "Invalid e-mail address<br>";
			}
				 else 
					{
					require_once 'register.php';
					echo "Please fill in all of the required fields.";
					}
}

Link to comment
Share on other sites

This is how if-else statements should be writte, and not write 100 if() without having an idea what ure doing. Why double check, theres no need!

 

<?php
$checkmail = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
if (preg_match($checkmail, $user_email)) {
$user_check = mysql_num_rows(mysql_query("SELECT * FROM $user WHERE user_email='$user_email' OR user_name='$user_name'"));
$length = strlen($user_password);
$length1 = strlen($user_name);
	if ($length >= 6){
		if ($length1 >= 4){
			if ($user_check == 1){
				if($user_password == $user_password2 && $user_email == $user_email2){
					$userPswd = md5($user_password);
					$userpwsd = sha1($userPswd);
					mysql_query("INSERT INTO `$user` (user_id,  user_name, user_password, user_email, user_ip, userlevel) VALUES 		('','$user_name','$userpwsd','$user_email','$ip','2')")
					or die('Error ' . mysql_error());
					echo "Succesfully Registered. You will recieve a confirmation email shortly.";
					header ("http://".$_SERVER[HTTP_HOST].$success);
				} else{
					$error = 'I have no idea what those _2 variables are';
				}
			} else{
				$error = 'Username or email are already taken';
			}
		} else{
			$error = 'Username is too short';
		}
	} else{
		$error = 'Password is too short';
	}
} else{
$error = 'Email is invalid';
}
?>

Link to comment
Share on other sites

As i thought that code doesn't work, it now doesn't echo anything or do anything.

Please check your code before posting, and stop with the comments. I dunno what you have against me.

This is how if-else statements should be writte, and not write 100 if() without having an idea what ure doing. Why double check, theres no need!

Link to comment
Share on other sites

Ok, ok, sorry for being a bit harsh. Mean it.

 

What i mean with (thats how if-else should be written) is that u must know the flow of the error checking. U should optimize the code by yourself basing on the "template" i gave, as i have no idea how the flow goes.

Link to comment
Share on other sites

I no how to do if-else, but i want the code to check each bit, but anway.

How can i stop these errors appearing when the user enters nothing

 

Warning: preg_match() [function.preg-match]: Empty regular expression in /home/www/deanwhitehouse.awardspace.co.uk/register.php on line 80

 

line 80

if (!preg_match($checkmail, $user_email)) {
   		 		echo "Invalid e-mail address<br>";
			}

Link to comment
Share on other sites

ok, this is my code,

<?php
if ($_SESSION['is_valid'] == false)
{
if(isset($_POST['signup']))
{
	if ($user_name && $user_password && $user_password2 && $user_email && $user_email2)
	{
		if (strstr($user_email, '@'))
		{
			$user_check = mysql_num_rows(mysql_query("SELECT * FROM $user WHERE user_email='$user_email' 
																			OR user_name='$user_name'"));
			$length = strlen($user_password);
			$length1 = strlen($user_name);
				if ($length >= 6)
				{
					if ($length1 >= 4)
					{
						if ($user_check >= 1)
						{
							require_once 'register.php';
							echo "Sorry, but the username or email you
							 requested is already in use. Please enter a different username.";
						}

							elseif ($user_password == $user_password2 && $user_email == $user_email2)
							{
							$userPswd = md5($user_password);
							$userpwsd = sha1($userPswd);
							mysql_query("INSERT INTO `$user` (user_id, 
							 user_name, user_password, user_email, user_ip, userlevel
							 ) VALUES 		('','$user_name','$userpwsd','$user_email','$ip','2')")
							or die('Error ' . mysql_error());
							echo "Succesfully Registered. You will recieve a confirmation email shortly.";
							header ("http://".$_SERVER[HTTP_HOST].$success);
 							}
					}
				}
			}	
		}
							if(!strstr($user_email, '@'))
							{
								echo "Invalid Email<br>";
							}

						if($length <= 5)
						{
							require_once 'register.php';
							echo "Password Too Short<br>";
						}

					if($length1 <= 3)
					{
						require_once 'register.php';
						echo "Username Too Short.<br>";
					}

				if(!$checkpw)
				{
						require_once 'register.php';
						echo "The passwords you entered do not match. Please check these details and try again.<br>";
				}

			if(!$checkem)
			{
				require_once 'register.php';
				echo "The emails you entered do not match. Please check these details and try again.<br>";
			}

		 else 
		{
			require_once 'register.php';
			echo "Please fill in all of the required fields.";
		}
}
?>

 

Can someone see if they can see why it isn't working properly, my problem is that it echoes some of the other IF's , if the email is incorrect. i have tried for a long time to try and fix it, can someone just have a look and see.

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.