LooieENG Posted May 17, 2008 Share Posted May 17, 2008 <?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 https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/ Share on other sites More sharing options...
BlueSkyIS Posted May 17, 2008 Share Posted May 17, 2008 remove the if option where you do nothing. <?php if ( $something == $something2 ) { // do lots of stuff here } ?> <html> <head> <title>Title</title> </head> <body> Stuff </body> </html> Link to comment https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543763 Share on other sites More sharing options...
LooieENG Posted May 17, 2008 Author Share Posted May 17, 2008 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 https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543769 Share on other sites More sharing options...
BlueSkyIS Posted May 17, 2008 Share Posted May 17, 2008 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 https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543771 Share on other sites More sharing options...
GingerRobot Posted May 17, 2008 Share Posted May 17, 2008 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 https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543772 Share on other sites More sharing options...
BlueSkyIS Posted May 17, 2008 Share Posted May 17, 2008 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 https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543774 Share on other sites More sharing options...
LooieENG Posted May 17, 2008 Author Share Posted May 17, 2008 Yeah, but a bit ago someone said it's best to use exit after incase header() fails And exit('stuff') does output the text Edit: Thanks, I'll try that Link to comment https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543775 Share on other sites More sharing options...
BlueSkyIS Posted May 17, 2008 Share Posted May 17, 2008 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 https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543777 Share on other sites More sharing options...
LooieENG Posted May 17, 2008 Author Share Posted May 17, 2008 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 https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543834 Share on other sites More sharing options...
LooieENG Posted May 18, 2008 Author Share Posted May 18, 2008 Can't modify my last post, but it's working now. Link to comment https://forums.phpfreaks.com/topic/106094-solved-exit-php-and-read-html/#findComment-543903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.