Page 1 of 1

php registrationform code

Posted: Tue Apr 14, 2015 1:37 pm
by steve46
hi, i am trying to create a simple registration form with php, but i keep getting this error message :

"Parse error: syntax error, unexpected end of file in C:\Abyss Web Server\htdocs\register.php on line 113"

here is my code:

Code: Select all

<!DOCTYPE html>
<?php
$page_title = 'Register';
include ('includes/header.html');

if($_SERVER['REQUEST_METHOD'] == 'POST') {

	require ('C:\Abyss Web Server\htdocs\connect_db.php');
	$errors = array();

	if(empty($_POST['first_name'])) {
		$errors [] = 'Enter your first name.';
	} else {
		$fn = mysqli_real_escape_string($dbc, trim($_POST[first_name]));
	}



	if(empty($_POST['last_name'])) {
		$errors[] = 'Enter your last name.';
	} else {
		$ln = mysqli_real_escape_string($dbc, trim($_POST[last_name]));
	}


	if(empty($_POST [email])) {
		$errors[] = 'Enter your email address.';
	} else {
		$e = mysqli_real_escape_string($dbc, trim($_POST[email]));
	}

	if(!empty($_POST[pass1])) {
		if($_POST [pass1] != $_POST[pass2]) {
			$errors[] = 'Passwords do not match';
		} else {
			$p = mysqli_real_escape_string($dbc, trim($_POST[pass1]));
		}
	} else {
		$errors[] = 'Enter your password.';
	}

	if(empty($errors)) {
		$q = "SELECT user_id FROM users WHERE email=$e";
		$r = mysqli_query($dbc, $q);
		if(mysqli_num_rows($r) != 0) {
			$errors [] = 'Email address already registered.<a href="login.php">Login</a>';
		}
	}
	if(empty($errors)) {
		$q = "INSERT INTO users
(first_name,last_name,email,pass,reg_date)
VALUES($fn,$ln,$e,SHA1($P),NOW())";
		$r = mysqli_query($dbc, $q);

		if($r) {
			echo '<h1>Registered!</h1>
<p>	You are now registered.</p>
<p><a href="login.php">login</a></p>';
		}
		mysqli_close($dbc);
		include (includes / footer . html);
		exit();
	} else {
		echo'<h1>Error!</h1>
<p id="err_msg">The following error(s) have occured:<br>';
		foreach($errors as $msg) {
			echo "-$msg<br>";
		}
		echo 'Please try again.</p>';
		mysqli_close($dbc);
	}
	?>


	<h1>Register</h1>

	<form action="register.php" method="POST">
		<p>


			First name:<input type="text" name="first name"
							  value="<?php if(isset($_POST['first_name']))
		echo $_POST ['first_name'];
	?>">

			Last name:<input type="text" name="last name"
							 value="<?php if(isset($_POST[last_name]))
		echo$_POST ['last_name'];
	?>"></p>
		<p>
			Email address:<input type="text" name="email"
								 value="<?php if(isset($_POST['email']))
		echo $_POST[email];
	?>"></p>

		<p>
			Password:<input type="text" name="pass1"
							value="<?php if(isset($_POST['pass1']))
		echo $_POST['pass1'];
	?>"></p>

		Confirm Password:<input type="password" name="pass1"
								value="<?php if(isset($_POST['pass2']))
								 echo $_POST['pass2'];
	?>"></p>
		<p><input type="submit" value="Register"></p>

	</form>
	<?php include ('footer.html'); ?>
</html>
what is wrong with my code? if anyone can help, that would be great thankyou.

Re: php registrationform code

Posted: Tue Apr 14, 2015 1:48 pm
by Celauran

Code: Select all

if($_SERVER['REQUEST_METHOD'] == 'POST') {
if statement is never closed

Re: php registrationform code

Posted: Tue Apr 14, 2015 1:52 pm
by Celauran
Also,

Code: Select all

require ('C:\Abyss Web Server\htdocs\connect_db.php');
Don't do that. Use relative paths to keep your code portable.

Code: Select all

include (includes / footer . html);
Wrap in quotes and lose the spaces?

Pro tip: include and require aren't functions; don't use () with them.

Code: Select all

                        Password:<input type="text" name="pass1"
                                                        value="<?php if(isset($_POST['pass1']))
                echo $_POST['pass1'];
        ?>"></p>

                Confirm Password:<input type="password" name="pass1"
                                                                value="<?php if(isset($_POST['pass2']))
                                                                 echo $_POST['pass2'];
 
You've got two form elements with the same name.

Code: Select all

                $q = "INSERT INTO users
(first_name,last_name,email,pass,reg_date)
VALUES($fn,$ln,$e,SHA1($P),NOW())";
Don't use SHA1 for password hashing. Use PHP's built-in password_hash()

Re: php registrationform code

Posted: Tue Apr 14, 2015 1:57 pm
by requinix
Ah, too slow with my reply. Distractions.

FYI when I edited the post to add the syntax tags, I also applied some automatic code formatting. It added the indentation.

Re: php registrationform code

Posted: Thu Apr 23, 2015 4:30 pm
by Pazuzu156
Celauran wrote:Also,

Code: Select all

require ('C:\Abyss Web Server\htdocs\connect_db.php');
Don't do that. Use relative paths to keep your code portable.

Also, you can use:

Code: Select all

<?php
require_once __DIR__.'/connect_db.php';
?>