hi everyone,

i am new here and i also quite new to php. i know this issue is quite common, been spending hours try to troubleshoot for this issue earlier. i have to give up and decide to seek advice from you guys, thanks for your time.

The problem actually is because of this line below.
header('Location:welcome.php');

This is the error after login:

An error occurred in script 'D:\xampp\htdocs\book\includes\login.inc.php' on line 33:
Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\book\includes\header.html:264)

For login.inc.php code:

<?php 

// This is the login page for the site.
// It's included by index.php, which receives the login form data.
// This script is created in Chapter 4.

// Array for recording errors:
$login_errors = array();

// Validate the email address:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$e = mysqli_real_escape_string ($dbc, $_POST['email']);
} else {
	$login_errors['email'] = 'Please enter a valid email address!';
}

// Validate the password:
if (!empty($_POST['pass'])) {
	$p = mysqli_real_escape_string ($dbc, $_POST['pass']);
} else {
	$login_errors['pass'] = 'Please enter your password!';
}
	
if (empty($login_errors)) { // OK to proceed!

	// Query the database:
	$q = "SELECT email, password FROM members1 WHERE (email='$e' AND password='$p')";		
	$r = mysqli_query ($dbc, $q);
	
	if (mysqli_num_rows($r) == 1) { // A match was made.
		
		// Get the data:
			header('Location:welcome.php');
		$row = mysqli_fetch_array ($r, MYSQLI_NUM); 	
		$_SESSION['email'] = $_POST['email'];
 // Jump to secured page
//  		
		// If the user is an administrator, create a new session ID to be safe:
		// This code is created at the end of Chapter 4:
			
	} else { // No match was made.
		$login_errors['login'] = 'The email address and password do not match those on file.';
	}
	
} // End of $login_errors IF.

// Omit the closing PHP tag to avoid 'headers already sent' errors!
Member Avatar for diafol

You've got html output in the page already - I assume that your index page has a number of include files. You can't use header() after any html output. You'll have to place the include file before any other stuff.

You've got html output in the page already - I assume that your index page has a number of include files. You can't use header() after any html output. You'll have to place the include file before any other stuff.

thanks for your reply.

which page u referring to? the login.php or the welcome.php (after login success, will redirect to this page)?

Member Avatar for diafol

the login.php contains the header() statement.

the index.php includes login.php as well as other stuff.

so your index.php file needs to look something like this:

<?php
include('login.php');
... other stuff...
?>

<DOCTYPE...
<html>
...
</html>

Hi,

Try this, modify your codes by adding the php closing tag just below line 47, and then in your php codes find

// Get the data:
header('Location:welcome.php');

change it to this

// Get the data:
//header('Location:welcome.php');
$welcomepage = "welcome.php";

echo '<script language="javascript" type="text/javascript">
window.location.href="'.$welcomepage.'";
</script>';

As long as it is within the if condition, it should work I think..

the login.php contains the header() statement.

the index.php includes login.php as well as other stuff.

so your index.php file needs to look something like this:

<?php
include('login.php');
... other stuff...
?>

<DOCTYPE...
<html>
...
</html>

thanks for your time and help!

Hi,

Try this, modify your codes by adding the php closing tag just below line 47, and then in your php codes find

// Get the data:
header('Location:welcome.php');

change it to this

// Get the data:
//header('Location:welcome.php');
$welcomepage = "welcome.php";

echo '<script language="javascript" type="text/javascript">
window.location.href="'.$welcomepage.'";
</script>';

As long as it is within the if condition, it should work I think..

thanks it works! :)

if i do in this way, am i able to display example, welcome $email etc in my welcome.asp ? so in welcome, can i put a session_start() ? thanks again :)

Member Avatar for diafol

You need to put session_start() at the top of every 'main' file or at least in a common include file that you place at the top of each 'main' file (like index.php, welcome.php etc).

WRT using js to redirect - just be aware that users without js turned on (not many these days, but still a significant amount), will not be redirected and may be 'stuck' on the original page. I'd always advise using a server-side redirect, if that is an essential part of the system (login).

Ardav is right, if javascript is off then user is trapped..

you can still use the same codes as you were, but you will have to rotate your codes.. like this

Remove the php closing tag below line 47, and then rearrange your codes as shown below.

// Get the data:

$row = mysqli_fetch_array ($r, MYSQLI_NUM);
$_SESSION['email'] = $_POST['email'];
//wait at least 1 second and then fire the header location
sleep(1); 
header('Location:welcome.php');

The problem with the above is that if 1 second is not enough time to wait for the $row result to comeback, then it has to be increased to at least 2. Users are impatient to wait even a fraction of a second in front of a blank page. While the javascript, allows the browser to take over after the $row is returned by the mysql server. Once the browser is on the welcome.php, the server takes over again to parse the content.

This is common problem if you are hosted by c__tex or some other hosting servers having the same setting as C__tex or h__tV where the mysql server is slow to respond, but the server where the PHP codes resides already parse your header location command. Often times, in a hosting like this you would get something like this "mysql server has gone away", or something in this regard. Believe me, I spent a good months exchanging nasty messages with c__tex support, but to no avail. So, I came out with the javascript solution just to give my clients the piece of mind, because they are hooked into a year plan with their hosting company.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.