Jump to content

Website login


kasitzboym

Recommended Posts

I could use some help with creating a website login page with registration for new users. My goal is to create a page that uses mysql with php for people to post information to the database and delete it later... The actual Goal is to create a Lost and Found page for an Animal Hospital. This page Clients will create posts and it will display on the main page then only those clients or the administrator will be able to delete  the posts.

Link to comment
Share on other sites

You might try going to some of the popular php framework sites, check to see if they have an authentication library, and then implement it per their user manual. That would be the easiest thing for you. Kohana has an auth library, and I'm sure a bunch of other ones do to. Actually, this is a good question for the frameworks forum.

Link to comment
Share on other sites

I wasn't trying to be rude or anything, it just sounded like you wanted the code. Like kasitzboym said, I bet there are tons of tutorials out there.

 

To let someone login, you'll want a page with a form. You'll want that form to submit to a page where you compare the username/password that was entered in the form to usernames/passwords in your database, probably using $_POST and using SELECT.

 

To let someone register, you'll want another form to let them fill out with whatever info you require when they register. You'll want to have this form submit to a page where the info is processed, again probably using $_POST. On that same page, you'll want to INSERT this info into your database.

Link to comment
Share on other sites

Hey thanks for the help... i now have a code for registration of new users on my website although i am receiving an error that i cannot find. The error i am recieveing is

 

Parse error: syntax error, unexpected $end in C:\xampp\htdocs\sahnew\Final\session tracking\register.php on line 137

 

 

The code i have written that has the error is below.

 


<?php
session_start();

include 'db.inc.php';

$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect. Please contact the webmaster.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));

$state_list = array('AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, PR, RI, SC, SD, TN, TX UT, VT, VA, WA, WV, WI, WY');

//filter incomming values
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? trim($_POST['password']) : '';
$first_name = (isset($_POST['first_name'])) ? trim($_POST['firstname']) : '';
$last_name = (isset($_POST['last_name'])) ? trim($_POST['last_name']) : '';
$email = (isset($_POST['email'])) ? trim($_POST['email']) : '';
$city = (isset($_POST['city'])) ? trim($_POST['city']) : '';
$state = (isset($_POST['state'])) && is_array($_POST['state']) ? $_POST['state'] : array();
$zip = (isset($_POST['zip'])) ? trim($_POST['zip']) : '';

if(isset($_POST['submit']) && $_POST['submit'] == 'Register') {

$errors = array();

//make sure manditory fields have been entered
if(empty($username)) {
	$errors[] = 'Username cannot be blank.';
}

//check if username already is registered
$query = 'SELECT username FROM users WHERE username = "' . $username . '"';
$result = mysql_query($query, $db) or die(mysql_error());
if (mysql_num_rows($result)>0) {
	$errors[] = 'Username' .$username. ' is already registered.';
	$username = '';
}
mysql_free_result($result);

if(empty($password)) {
	$errors[] = 'Password cannot be blank.';
}

if(empty($first_name)) {
	$errors[] = 'First name cannot be blank.';
}

if(empty($last_name)) {
	$errors[] = 'Last name cannot be blank.';
}

if(empty($email)) {
	$errors[] = 'Email address cannot be blank.';
}

if(count($errors) > 0) {
	echo '<p><Strong style="color:#FF000;">Unable to process your '.'registration.</strong></p>';
	echo '<p>Please check the following errors:</p>';
	echo '<ul>';
	foreach ($errors as $error) {
		echo '<li>' .$error. '</li>';
		}
	echo '</ul>';
} else {
	// No errors so enter the information into the database.

	$query = 'INSERT INTO users (user_id, username, password) VALUES (NULL, "' . mysql_real_excape_string($username, $db) . '", '.'PASSWORD("' . mysql_real_excape_string($password, $db) . '"))';

	$result = mysql_query($query, $db) or die(mysql_error());

	$user_id = mysql_insert_id($db);

	$query = 'INSERT INTO user_info (user_id, first_name, last_name, email, phone, city, state, zip) 
		VALUES 
			(' . $user_id. ' , ' .
			 '"' .mysql_real_excape_string($first_name, $db) .'", ' . 
			 '"' .mysql_real_excape_string($last_name, $db) .'", '.
			 '"' .mysql_real_excape_string($email, $db) . '", '.
			 '"' .mysql_real_excape_string($phone, $db) . '", '.
			 '"' .mysql_real_excape_string($city, $db) . '", '.
			 '"' .mysql_real_excape_string(join(', ', $state),$db) . '", '.
			 '"' .mysql_real_excape_string($zip, $db) . '")';
	$result = mysql_query($query, $db) or die(mysql_error());

	$_SESSION['logged'] = 1;
	$_SESSION['username'] = $username;

	header('Refresh: 5; URL=main.php');
?>
<html>
<head>
	<title>Register</title>
	<style type="text/css">
	td{ vertical-align: top;}
	</style>
</head>
<body>
	<form action="register.php" method="post">
		<table>
			<tr>
				<td><label for="username">Username:</label></td>
				<td><input type="text" name="username" id="username" size="20" maxlength="20" value="<?php echo $username; ?>"/></td>
			</tr><tr>
				<td><label for="password">Password:</label></td>
				<td><input type="password" name="password" id="password" size="20" maxlength="20" value="<?php echo $password; ?>"/></td>
			</tr><tr>
				<td><label for="email">Email:</label></td>
				<td><input type="text" name="email" id="email" size="20" maxlength="50" value="<?php echo $email; ?>"/></td>
			</tr><tr>
				<td><label for="first_name">First name:</label></td>
				<td><input type="text" name="first_name" id="first_name" size="20" maxlength="20" value="<?php echo $first_name ?>"/></td>
			</tr><tr>
				<td><label for="last_name">Last name:</label></td>
				<td><input type="text" name="last_name" id="last_name" size="20" maxlength="20" value="<?php echo $last_name ?>"/></td>
			</tr><tr>
				<td><label for="city">City:</label></td>
				<td><input type="text" name="city" id="city" size="20" maxlength="20" value="<?php echo $city ?>"/></td>
			</tr><tr>
				<td><label for="state">State</label></td>
				<td><select name="state[]" id="state" multiple="multiple">
			<?php
			foreach ($state_list as $states){
				if(in_array($states, $state)) {
					echo '<option value="'.$states.'" selected="selected">' .$states . '</option>';
					} else {
						echo '<option value="'.$states.'">'.$states.'</option>';
					}
			}
			?>
				</select></td>
			</tr><tr>
				<td></td>
				<td><input type="submit" name="submit" value="Register"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

 

If anyone can help me find the error i would greatly appreciate it  :D

Link to comment
Share on other sites

When you get that error it means you have unbalanced curly brackets "{ }" or unbalacenced quotes.

 

On line 63 (at least in my editor) you have

<?php
} else {
?>

 

I don't see a closing "}" for the opening "{" on that line.

 

Ken

Link to comment
Share on other sites

Here you go.

 

<?php
session_start();

include 'db.inc.php';

$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect. Please contact the webmaster.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));

$state_list = array('AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, PR, RI, SC, SD, TN, TX UT, VT, VA, WA, WV, WI, WY');

//filter incomming values
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? trim($_POST['password']) : '';
$first_name = (isset($_POST['first_name'])) ? trim($_POST['firstname']) : '';
$last_name = (isset($_POST['last_name'])) ? trim($_POST['last_name']) : '';
$email = (isset($_POST['email'])) ? trim($_POST['email']) : '';
$city = (isset($_POST['city'])) ? trim($_POST['city']) : '';
$state = (isset($_POST['state'])) && is_array($_POST['state']) ? $_POST['state'] : array();
$zip = (isset($_POST['zip'])) ? trim($_POST['zip']) : '';

if(isset($_POST['submit']) && $_POST['submit'] == 'Register') {

$errors = array();

//make sure manditory fields have been entered
if(empty($username)) {
	$errors[] = 'Username cannot be blank.';
}

//check if username already is registered
$query = 'SELECT username FROM users WHERE username = "' . $username . '"';
$result = mysql_query($query, $db) or die(mysql_error());
if (mysql_num_rows($result)>0) {
	$errors[] = 'Username' .$username. ' is already registered.';
	$username = '';
}
mysql_free_result($result);

if(empty($password)) {
	$errors[] = 'Password cannot be blank.';
}

if(empty($first_name)) {
	$errors[] = 'First name cannot be blank.';
}

if(empty($last_name)) {
	$errors[] = 'Last name cannot be blank.';
}

if(empty($email)) {
	$errors[] = 'Email address cannot be blank.';
}

if(count($errors) > 0) {
	echo '<p><Strong style="color:#FF000;">Unable to process your '.'registration.</strong></p>';
	echo '<p>Please check the following errors:</p>';
	echo '<ul>';
	foreach ($errors as $error) {
		echo '<li>' .$error. '</li>';
		}
	echo '</ul>';
} else {
	// No errors so enter the information into the database.

	$query = 'INSERT INTO users (user_id, username, password) VALUES (NULL, "' . mysql_real_excape_string($username, $db) . '", '.'PASSWORD("' . mysql_real_excape_string($password, $db) . '"))';

	$result = mysql_query($query, $db) or die(mysql_error());

	$user_id = mysql_insert_id($db);

	$query = 'INSERT INTO user_info (user_id, first_name, last_name, email, phone, city, state, zip) 
		VALUES 
			(' . $user_id. ' , ' .
			 '"' .mysql_real_excape_string($first_name, $db) .'", ' . 
			 '"' .mysql_real_excape_string($last_name, $db) .'", '.
			 '"' .mysql_real_excape_string($email, $db) . '", '.
			 '"' .mysql_real_excape_string($phone, $db) . '", '.
			 '"' .mysql_real_excape_string($city, $db) . '", '.
			 '"' .mysql_real_excape_string(join(', ', $state),$db) . '", '.
			 '"' .mysql_real_excape_string($zip, $db) . '")';
	$result = mysql_query($query, $db) or die(mysql_error());

	$_SESSION['logged'] = 1;
	$_SESSION['username'] = $username;

	header('Refresh: 5; URL=main.php');
}
}
?>
<html>
<head>
	<title>Register</title>
	<style type="text/css">
	td{ vertical-align: top;}
	</style>
</head>
<body>
	<form action="register.php" method="post">
		<table>
			<tr>
				<td><label for="username">Username:</label></td>
				<td><input type="text" name="username" id="username" size="20" maxlength="20" value="<?php echo $username; ?>"/></td>
			</tr><tr>
				<td><label for="password">Password:</label></td>
				<td><input type="password" name="password" id="password" size="20" maxlength="20" value="<?php echo $password; ?>"/></td>
			</tr><tr>
				<td><label for="email">Email:</label></td>
				<td><input type="text" name="email" id="email" size="20" maxlength="50" value="<?php echo $email; ?>"/></td>
			</tr><tr>
				<td><label for="first_name">First name:</label></td>
				<td><input type="text" name="first_name" id="first_name" size="20" maxlength="20" value="<?php echo $first_name ?>"/></td>
			</tr><tr>
				<td><label for="last_name">Last name:</label></td>
				<td><input type="text" name="last_name" id="last_name" size="20" maxlength="20" value="<?php echo $last_name ?>"/></td>
			</tr><tr>
				<td><label for="city">City:</label></td>
				<td><input type="text" name="city" id="city" size="20" maxlength="20" value="<?php echo $city ?>"/></td>
			</tr><tr>
				<td><label for="state">State</label></td>
				<td><select name="state[]" id="state" multiple="multiple">
			<?php
			foreach ($state_list as $states){
				if(in_array($states, $state)) {
					echo '<option value="'.$states.'" selected="selected">' .$states . '</option>';
					} else {
						echo '<option value="'.$states.'">'.$states.'</option>';
					}
			}
			?>
				</select></td>
			</tr><tr>
				<td></td>
				<td><input type="submit" name="submit" value="Register"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

 

You were just missing a couple closing braces before you started the HTML section.

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.