Jump to content

[SOLVED] Exit PHP and read HTML?


LooieENG

Recommended Posts

<?php

if ( $something != $something2 )
{
// stop reading the PHP code
}
else
{
// do lots of stuff here
}

?>

<html>
<head>
<title>Title</title>
</head>

<body>
Stuff
</body>
</html>

 

How to I stop PHP, but continue to read the HTML?

 

Thanks.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

the HTML is technically part of the php, so exiting exits everything. also, this won't output anything:

 

exit('Already logged in.');

 

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

 

but you already header()'ed away so that won't do anything either.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
		}
	}
}
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.