Jump to content

[SOLVED] Exit PHP and read HTML?


LooieENG

Recommended Posts

Here's my code, I thought exit would stop the PHP only, but it stops the whole page

 

<?php

// Starts session
session_start();

// Checks if user is already logged in
if ( $_SESSION['username'] )
{
// User is already logged in, so redirecting to the Admin CP
header('location: admin.php');
exit('Already logged in.');
}

// Checks if form has been submitted
if ( $_POST['login'] )
{
// Checks if username contains alphanumeric characters only
if ( !ctype_alnum($_POST['username']) )
	{
	// Sets error and exits
	$ue = '(Alphanumeric characters only.)';
	exit;
	}

// Sets variables
$username = $_POST['username'];
$password = md5('salt' . $_POST['password']);

// Opens database connection and selects database
$con = mysql_connect('localhost', 'xxxxx', 'xxxxx');
mysql_select_db('xxxxx_blog', $con);

// Gets data for username from database
$result = mysql_query("SELECT password FROM users WHERE username = '$username'");

// Checks if user exists
if ( mysql_num_rows($result) < 1 )
	{
	// Sets error and exits
	$ue = '(Username not found.)';
	exit;
	}

// Gets rows from database query
$row = mysql_fetch_array($result);

// Checks if password matches
if ( $password != $row['password'] )
	{
	// Sets error and exits
	$pe = '(The password you entered is incorrect.)';
	exit;
	}

// Sets session data and redirects to Admin CP
$_SESSION['username'] = $username;
header('location: admin.php');

// Exits
exit;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="login">
Username: <input name="username" type="text" size="30" /> <?php echo $ue ?><br />
Password: <input name="username" type="password" size="30" /> <?php echo $pe ?><br />
<input name="login" type="submit" value="Log in" />
</form>
</body>
</html>

Yes, exiting the script will stop all following output.

 

Futhermore, if you're changing location then there wont be any output anyway!

 

maybe you mean die('Already logged in.');

 

Die and exit are equivalent - it makes no odds which one you use.

if you want to do it the way you're doing it, you'll need to add some else's:

 

<?php

// Starts session
session_start();

// Checks if user is already logged in
if ( $_SESSION['username'] ) {
// User is already logged in, so redirecting to the Admin CP
header('location: admin.php');
exit;
}

// Checks if form has been submitted
if ( $_POST['login'] ) {
// Checks if username contains alphanumeric characters only
if ( !ctype_alnum($_POST['username']) ) {
	// Sets error and exits
	$ue = '(Alphanumeric characters only.)';
} else {

	// Sets variables
	$username = $_POST['username'];
	$password = md5('salt' . $_POST['password']);

	// Opens database connection and selects database
	$con = mysql_connect('localhost', 'xxxxx', 'xxxxx');
	mysql_select_db('xxxxx_blog', $con);

	// Gets data for username from database
	$result = mysql_query("SELECT password FROM users WHERE username = '$username'");

	// Checks if user exists
	if ( mysql_num_rows($result) < 1 ) {
		// Sets error and exits
		$ue = '(Username not found.)';
	} else {

		// Gets rows from database query
		$row = mysql_fetch_array($result);

		// Checks if password matches
		if ( $password != $row['password'] ) {
			// Sets error and exits
			$pe = '(The password you entered is incorrect.)';
		} else {

			// Sets session data and redirects to Admin CP
			$_SESSION['username'] = $username;
			header('location: admin.php');

			// Exits
			exit;
		}
	}
}
}

?>

Yeah, but a bit ago someone said it's best to use exit after incase header() fails

 

And exit('stuff') does output the text

 

wow, it does output the text. a new one for me. i usually use die(). yes, you should use exit after header(), but nothing will output after header(), exit or not.

I see, and using else works, but every time I enter something, it says username not found, but I checked the database and the name does exist.

 

<?php

// Starts session
session_start();

// Checks if user is already logged in
if ( $_SESSION['username'] )
{
// User is already logged in, so redirecting to the Admin CP
header('location: admin.php');
exit;
}

// Checks if form has been submitted
if ( $_POST['login'] )
{
// Checks if username contains alphanumeric characters only
if ( !ctype_alnum($_POST['username']) )
	{
	// Sets error and exits
	$ue = '(Alphanumeric characters only.)';
	}
else
	{
	// Sets variables
	$username = $_POST['username'];
	$password = md5('salt' . $_POST['password']);

	// Opens database connection and selects database
	$con = mysql_connect('localhost', 'xxxxx, 'xxxxx');
	mysql_select_db('xxxxx_blog', $con);

	// Gets data for username from database
	$result = mysql_query("SELECT password FROM users WHERE username = '$username'");
	// Checks if user exists
	if ( mysql_num_rows($result) < 1 )
		{
		// Sets error
		$ue = '(Username not found.)';
		}
	else
		{
		// Gets rows from database query
		$row = mysql_fetch_array($result);

		// Checks if password matches
		if ( $password != $row['password'] )
			{
			// Sets error
			$pe = '(The password you entered is incorrect.)';
			}
		else
			{
			// Sets session data and redirects to Admin CP
			$_SESSION['username'] = $username;
			header('location: admin.php');

			// Exits
			exit;
			}
		}
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="login">
Username: <input name="username" type="text" size="30" /> <?php echo $ue ?><br />
Password: <input name="username" type="password" size="30" /> <?php echo $pe ?><br />
<input name="login" type="submit" value="Log in" />
</form>
</body>
</html>

 

Thanks

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.