Jump to content

[SOLVED] Login error showing in wrong place.


rnintulsa

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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. 

 

 

 

Link to comment
Share on other sites

?>

<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.

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.