Jump to content

Recommended Posts

I want to start focusing on making cleaner looking codes. How can I make the below look cleaner?

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];

if (isset($_POST['submit'])) {
//Do some filtering
$email = mysql_real_escape_string(htmlentities(strip_tags(trim($_POST['email']))));
$name = mysql_real_escape_string(htmlentities(strip_tags(trim($_POST['name']))));
$password = mysql_real_escape_string(htmlentities(strip_tags(trim($_POST['password']))));
$confirmpassword = mysql_real_escape_string(htmlentities(strip_tags(trim($_POST['confirmpassword']))));

//If cookie is not set then we allow form to be submit
if (!isset($_COOKIE['registerform'])) {

if (!empty ($_POST['agree'])) {
if (!empty ($_POST['email'])) {		
	if (preg_match ('/^[A-Za-z0-9._]{1,100}@[A-Za-z0-9._]{1,100}\.[A-Za-z0-9]{1,100}$/', $email)) {
	//Check for duplicate emails
	$query = "SELECT * FROM user WHERE email = '$email' LIMIT 1";

	if ($result = mysql_query ($query)) {
		while ($row = mysql_fetch_array ($result)) {
		$emailcheck = $row['email'];
		}
	}

	if ($email != $emailcheck) {				
		if (!empty ($_POST['name'])) {
			if (preg_match ('/^[A-Za-z0-9\s]{1,50}$/', $name)) {		
				if (!empty ($_POST['password'])) {
					if (preg_match ('/^[A-Za-z0-9]{4,15}$/', $password)) {
						if (preg_match ('/^[A-Za-z0-9]{4,15}$/', $confirmpassword)) {		
							if (!empty ($_POST['confirmpassword']) && ($_POST['password']) == ($_POST['confirmpassword']) ) {					

							//Sha1 password
							$password2 = sha1($password);

							//Database insert
							$rquery = "INSERT INTO user (uniqueid, email, name, password, rip, lip, dateregistered, lastlogin) VALUES ('0', '$email', '$name', '$password2', '$ip', '$ip', NOW(), NOW())";

							if (mysql_query($rquery)) {
							//Set cookie to help prevent spam
							setcookie ('registerform', 'registerform', time()+60);

							//Create unique id
							$uniqueid = sha1($email);

							$uquery = "SELECT * FROM user WHERE email = '$email' LIMIT 1";

								if ($uresult = mysql_query ($uquery)) {
									while ($urow = mysql_fetch_array ($uresult)) {
									$uid = $urow['id'];
									}
								}								
							//New unique id = uniqueid+id
							$uniqueid2 = $uniqueid.$uid;

							$uquery = "UPDATE user SET uniqueid = '$uniqueid2' WHERE email = '$email' LIMIT 1";

								if (mysql_query($uquery)) {

								echo '<span style="color:#FF0000;font-size:11px">Thanks<br /></span>';
								}
								else {
								echo '<span style="color:#FF0000;font-size:11px">There was an error registering<br /></span>';	
								}								
							echo '<span style="color:#FF0000;font-size:11px">You have successfully registered</span>';
							}
							else {
							echo '<span style="color:#FF0000;font-size:11px">There was an error registering</span>';	
							}						

							} else {
							echo '<span style="color:#FF0000;font-size:11px">Your password and confirm password does not match</span>';	
							}
						} else {
						echo '<span style="color:#FF0000;font-size:11px">You have invalid characters in your confirm password</span>';	
						}
					} else {
					echo '<span style="color:#FF0000;font-size:11px">You have invalid characters in your password</span>';	
					}
				} else {
				echo '<span style="color:#FF0000;font-size:11px">Your password is empty</span>';	
				}
			} else {
			echo '<span style="color:#FF0000;font-size:11px">You have invalid characters in your name</span>';	
			}
		} else {
		echo '<span style="color:#FF0000;font-size:11px">Your name is empty</span>';	
		}				
	} else {
	echo '<span style="color:#FF0000;font-size:11px">Email duplicate</span>';	
	}	
	} else {
	echo '<span style="color:#FF0000;font-size:11px">You have invalid characters in your email</span>';	
	}		
} else {
echo '<span style="color:#FF0000;font-size:11px">Your email is empty</span>';
}
} else {
echo '<span style="color:#FF0000;font-size:11px">You must agree to the Terms</span>';
}
}
else {
echo '<span style="color:#FF0000;font-size:11px">You have already registered.</span>';	
}
}//End isset submit

?>

Depending on how much data is in $_POST and whether the data needs 'filtering' as you call it, you could use a foreach loop to loop through $_POST and filter the data assigning it either to array values or I guess you could make seperate variables by using variable variable names (havent played around with them much). Any keys of $_POST that do not need filtering could be excepted using an exceptions array or even an exceptions list like: $exceptions = 'submit,agree'; Using a loop like this can also be reused in future scripts.

 

Other than that your code looks pretty neat to me.

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.