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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528081
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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528095
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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528101
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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528104
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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528121
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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528129
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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528130
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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528136
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
https://forums.phpfreaks.com/topic/103089-ereg-and-preg_match/#findComment-528143
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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