Jump to content

Login form - if error, re-display form + errors (atm i have just errors)


Phear46

Recommended Posts

Login.php

<?php

if (!$_SESSION['auth'] == 1) {

	if (!isset($_POST['login_submit'])) {
	//If login has NOT been submitted then {
	?>
	<form name="login" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<div id="username" style="width:500px;">
		<div name="box1" style="text-align:right;height:27px;width:250px;float:left;">
			Username:
		</div>
		<div name="box2" style="text-align:center;height:27px;width:250px;float:right;">
			<input type="text" name="userlogin">
		</div>
	</div>
	<div id="password" style="width:500px;">
		<div name="box1" style="text-align:right;height:27px;width:250px;float:left;">
			Password:
		</div>
		<div name="box2" style="text-align:center;height:27px;width:250px;float:right;">
			<input type="password" name="userpass">
		</div>
	</div>
	<div id="submit" style="width:500px;">
		<div name="box1" style="text-align:center;height:27px;width:250px;float:left;">
		</div>
		<div name="box2" style="text-align:center;height:27px;width:250px;float:right;">
			<input type="submit" name="login_submit" value="Log In">
		</div>
	</div>
	</form>
	<?php

	} else {
		if (empty($_POST['userlogin'])) {
		die ("ERROR: Please enter username!");
		}

		if (empty($_POST['userpass'])) {
		die ("ERROR: Please enter password!");
		}


	// set server access variables
		$host = "***";
		$user = "***";
		$pass = "***";
		$db = "***";
		$table = "***";

	// open connection
		$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

	// select database
		mysql_select_db($db) or die ("Unable to select database!");

	// create query
		$query = "SELECT * FROM $table WHERE username = '" . $_POST['userlogin'] . "' AND password = '" . $_POST['userpass'] . "'";

	// execute query
		$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

	// see if any rows were returned
		if (mysql_num_rows($result) == 1) {
			// if a row was returned
			// authentication was successful
			// create session and set cookie with username

			session_start();
			$_SESSION['auth'] = 1;
			setcookie("username", $_POST['userlogin'], time()+(84600*30));
			echo "Access granted!";

		} else {
		// no result
		// authentication failed
		echo "ERROR: Incorrect username or password!";
		}

	}

} else {
	echo "You are logged in! <a href=include/logout.php>Logout</a>";
}
?>

This is a php file that is included inside a <div> on the actual webpage. It works fine but if i have an error id like to display the form with the error message below. Right now the form dissapears and leaves me with just an error message.

 

Any ideas? I was thinking about making the 'display form' part of the code a function and then calling it + my errors when an error is returned. Not sure if this is the best way to do it or not.

 

Thanks for reading, any relpies are much appreciated!

Link to comment
Share on other sites

At the top of your php file, use your if statement to check if the form has been submitted. If yes then do error checking.

In the error checking, instead of calling die() (which kills php processing at that point), set a variable to the error message you would like to display, and you could set a flag to say that there is an error.

 

Then have a separate if (so not an else of your initial if, just a completely separate if), that will check if a form hasn't been submitted or if there are errors. Echo the error next to the field that you want it to display next to, or just at the top of the form. The else for the second if can be the processing for actually submitting the form (as there would be no errors), and then forwarding to the results/confirmation page.

 

That's a pretty basic way to get what you want, and it's not code, but you should be able to figure out how to implement it :).

 

Denno

Link to comment
Share on other sites

It works, perfectly actually! Thank you very much!

 

One more question, i know this is off topic and also HIGHLY likely to be answered already in this forum ...

 

Is there a good FREE program to help identify spelling mistakes and missed brackets etc? My programs are starting to get a little bigger and re-reading line after line after line to find one missing ) is getting quite frustrating! I'm currently using gEdit on my Ubuntu laptop.

 

I believe there is some built in function of PHP (i could be wrong) to help with this stuff but I'm fairly new to this and have not yet gotten round to finding out how to make it work for me.

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.