rnintulsa Posted July 28, 2008 Share Posted July 28, 2008 Hi all, My login script works good, except for one thing...If I try to login when I test the errors with a name that is not in the system, I get the "You must be registered before you may log in.", but it is showing up at the top of the body instead of in the content area. I know it is because that is where I am telling it to do so, but when I move that php code to the content area I get other problems and errors. I have tried many things, but I am going to give you the original code. I am new to php and programming and am so excited with all I am learning. I would appreciate detailed explanations to the correct way to solve this problem. Thanks ahead! This is my php: <?php error_reporting(E_ALL); session_start( ); // if username and password are set and not empty then proceed with the rest of the process if( isset( $_POST[ 'username' ] ) && isset( $_POST[ 'password' ] ) && $_POST[ 'username' ] != '' && $_POST[ 'password' ] != '' ) { $link = mysql_connect( 'host', 'username', 'password' ); $db_selected = mysql_select_db('uname', $link); $username = mysql_real_escape_string($_POST['username'], $link); $password = mysql_real_escape_string($_POST['password'], $link); if (!$db_selected) { echo"Connection to the database failed. Please try again later." ; exit; } //checks for username and password in db table. $results = mysql_query("select * from users where username='" . $username . "' and password = '" . $password . "'" ,$link ) or die(mysql_error()); $num_rows = mysql_num_rows($results); //greater than zero if( $num_rows > 0 ) { $_SESSION['username'] = $username; //redirect header('Location:orion.php'); } else { echo 'You must be registered before you may log in.'; } } ?> <html> <?php include( 'sessions.php' ); show_statement( ); if (isset($_SESSION['username'])) { echo '<br />'; echo 'Logged in as '.$_SESSION['username'].''; echo '<br /><a href="logout.php">Log out</a><br />'; } else { echo 'Please login to view your company files.<br />'; } ?> </html Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/ Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 With that code, not sure if anyone knows what you mean by "content area". You also should start your HTML header before any output. Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601831 Share on other sites More sharing options...
rnintulsa Posted July 28, 2008 Author Share Posted July 28, 2008 Thank you revraz, So, I should start my page with html, and not the php? I was not clear about the content area. I thought that would be understood. The content area is what I have shown in the html tags. I am new to forums and php. I will be more specific next time. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601845 Share on other sites More sharing options...
rnintulsa Posted July 28, 2008 Author Share Posted July 28, 2008 I tried putting my html tag at the top of the page, but I get this error: Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /nfs/cust/4/45/65/556544/web/login3.php:5) in /nfs/cust/4/45/65/556544/web/login3.php on line 9 So I moved it to after my session_start(): and still get the same error. Can someone explain to me what revraz meant by: You should start your HTML header before any output. I really want to learn. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601889 Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 Are you familiar with HTML ? Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601902 Share on other sites More sharing options...
rnintulsa Posted July 28, 2008 Author Share Posted July 28, 2008 yes Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601909 Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 Try something like this. You didn't close your HTML tag at the end either in the code you posted. <?php error_reporting(E_ALL); session_start( ); ?> <html> <body> <?php // if username and password are set and not empty then proceed with the rest of the process if( isset( $_POST[ 'username' ] ) && isset( $_POST[ 'password' ] ) && $_POST[ 'username' ] != '' && $_POST[ 'password' ] != '' ) { $link = mysql_connect( 'host', 'username', 'password' ); $db_selected = mysql_select_db('uname', $link); $username = mysql_real_escape_string($_POST['username'], $link); $password = mysql_real_escape_string($_POST['password'], $link); if (!$db_selected) { echo"Connection to the database failed. Please try again later." ; exit; } //checks for username and password in db table. $results = mysql_query("select * from users where username='" . $username . "' and password = '" . $password . "'" ,$link ) or die(mysql_error()); $num_rows = mysql_num_rows($results); //greater than zero if( $num_rows > 0 ) { $_SESSION['username'] = $username; //redirect header('Location:orion.php'); } else { echo 'You must be registered before you may log in.'; } } include( 'sessions.php' ); show_statement( ); if (isset($_SESSION['username'])) { echo '<br />'; echo 'Logged in as '.$_SESSION['username'].''; echo '<br /><a href="logout.php">Log out</a><br />'; } else { echo 'Please login to view your company files.<br />'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601913 Share on other sites More sharing options...
DarkWater Posted July 28, 2008 Share Posted July 28, 2008 He'll get a "headers already sent" message because of the header('Location:') call. Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601915 Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 Ah true, so he can either restructure the logic to put the header before the HTML or change it to a HTML/Javascript redirect. Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601924 Share on other sites More sharing options...
rnintulsa Posted July 28, 2008 Author Share Posted July 28, 2008 You guys are right. I got that message: And I tried this same thing earlier, with the same results. Warning: Cannot modify header information - headers already sent by (output started at /nfs/cust/4/45/65/556544/web/login2.php:17) in /nfs/cust/4/45/65/556544/web/login2.php on line 46 But the error message that I was trying to print in to the page is still up on top of the page. I hope I am making sense. Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601934 Share on other sites More sharing options...
Nhoj Posted July 28, 2008 Share Posted July 28, 2008 ?> <html> <body> <?php That part would be your problem more than likely. You cannot output ANYTHING to the browser if you are going to use any of the header() function commands after it. Try the following: <?php error_reporting(E_ALL); session_start( ); // if username and password are set and not empty then proceed with the rest of the process if( isset( $_POST[ 'username' ] ) && isset( $_POST[ 'password' ] ) && $_POST[ 'username' ] != '' && $_POST[ 'password' ] != '' ) { $link = mysql_connect( 'host', 'username', 'password' ); $db_selected = mysql_select_db('uname', $link); $username = mysql_real_escape_string($_POST['username'], $link); $password = mysql_real_escape_string($_POST['password'], $link); if (!$db_selected) { echo"Connection to the database failed. Please try again later." ; exit; } //checks for username and password in db table. $results = mysql_query("select * from users where username='" . $username . "' and password = '" . $password . "'" ,$link ) or die(mysql_error()); $num_rows = mysql_num_rows($results); //greater than zero if( $num_rows > 0 ) { $_SESSION['username'] = $username; //redirect header('Location:orion.php'); } else { echo 'You must be registered before you may log in.'; } } include( 'sessions.php' ); show_statement( ); if (isset($_SESSION['username'])) { echo '<br />'; echo 'Logged in as '.$_SESSION['username'].''; echo '<br /><a href="logout.php">Log out</a><br />'; } else { echo 'Please login to view your company files.<br />'; } ?> </body> </html> Also, what is in sessions.php? If you echo ANYTHING and then try to use header() it will give an error. Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601990 Share on other sites More sharing options...
discomatt Posted July 28, 2008 Share Posted July 28, 2008 Output buffering will help fix that.. but it's really no replacement for good logic, IMO http://php.net/manual/en/ref.outcontrol.php Quote Link to comment https://forums.phpfreaks.com/topic/117013-solved-login-error-showing-in-wrong-place/#findComment-601995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.