Jump to content

issues with start_session() before header()


erickreno

Recommended Posts

I am having an issue I cannot figure out. Been looking at it for 3 days now and I don't see anything wrong, maybe you can help. When I run the script, the form comes up and works well, when I submit it I get the following PHP error message:

Quote

Warning: session_start(): Cannot start session when headers already sent in /home/smchigho/public_html/eweek/admin/index.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /home/smchigho/public_html/eweek/admin/index.php:1) in /home/smchigho/public_html/eweek/admin/index.php on line 32

header() comes after the session_start() but it's still telling me there is an issue. The include files all work and have no spaces or echo commands, they are used in another part of the site and work fine. Here is the code:

<?php include_once('../includes/connectvars.php');?>
<?php include_once('../functions/globalFunctions.php');?>

<?php
$webID = '';
$password = '';
$sendForm = '';

// * Form Processing  *
if(isset($_POST['username'], $_POST['password'])){
	$webID = $_POST['username'];
	$password = $_POST['password'];

// * Validate login  *
if(validateLoginAdmin($webID, $password, $conn) == false){
		$sendForm = false;
	}else{$sendForm = true;}

// * Good to go? Start Session  *
if($sendForm == true){
	//include_once('startSessionAdmin.php');
	 session_start();
	$query = "SELECT * FROM adminlogin WHERE adminuser = '$webID' limit 1";
	$result = mysqli_query($conn, $query);
	$row = $result->fetch_assoc();
    	$_SESSION['firstName'] = $row['firstName'];
    	$_SESSION['lastName'] = $row['lastName'];
    	$_SESSION['login'] = 1;
    	$_SESSION['admin'] = 2;
    	$_SESSION['username'] = $webID;
	header('Location:admin_landing.php');
	exit();
}}
?>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Login</title>
		<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
		<link rel="stylesheet" href="../css/login.css">
	</head>
	<body>
	<div class="login">
	  <h1>Enrichment Week Admin Login</h1>
			<form action="index.php" method="post">
				<label for="username">
					<i class="fas fa-user"></i>
				</label>
				<input type="text" name="username" placeholder="Username" id="username" required>
				<label for="password">
					<i class="fas fa-lock"></i>
				</label>
				<input type="password" name="password" placeholder="Password" id="password" required>
				<input type="submit" value="Login">
			</form>
		</div>
	</body>
</html>

What am I missing?

Link to comment
Share on other sites

First thing I thought about, even recreated the file manually to make sure I wasn't missing any errant characters. The fact that the header() is after the session_start() is the part that is strange.

Also checked all include files to make sure no errant characters. I use the same script for student's login with no issues.

Link to comment
Share on other sites

This more like it should look:

<?php 
session_start();
include_once '../includes/connectvars.php';

include_once '../functions/globalFunctions.php';
$webID = '';
$password = '';
$sendForm = '';

// * Form Processing  *
if(isset($_POST['username'], $_POST['password']))
{
	$webID = $_POST['username'];
	$password = $_POST['password'];
	// * Validate login  *
	if(validateLoginAdmin($webID, $password, $conn) == false)
	{
		$sendForm = false;
	}
	else{$sendForm = true;}

	// * Good to go? Start Session  *
	if($sendForm == true)
	{
		//include_once('startSessionAdmin.php');
		$query = "SELECT * FROM adminlogin WHERE adminuser = '$webID' limit 1";
		$result = mysqli_query($conn, $query);
		$row = $result->fetch_assoc();
		$_SESSION['firstName'] = $row['firstName'];
		$_SESSION['lastName'] = $row['lastName'];
		$_SESSION['login'] = 1;
		$_SESSION['admin'] = 2;
		$_SESSION['username'] = $webID;
		header('Location:admin_landing.php');
		exit();
	}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
<link rel="stylesheet" href="../css/login.css">
</head>
<body>
<div class="login">
<h1>Enrichment Week Admin Login</h1>
<form action="index.php" method="post">
<label for="username">
<i class="fas fa-user"></i>
</label>
<input type="text" name="username" placeholder="Username" id="username" required>
<label for="password">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="password" placeholder="Password" id="password" required>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>

 

PS

Include/requires do not need parens.

 

Link to comment
Share on other sites

To clarify the error message points you at the header line but that is not the cause.  It is probably that blank line up at the top.

When in php mode nothing goes to the client by accident.  But when NOT in php mode any blank chars or lines do go to the client and that's the message you are getting.  Actually line 1 seems to be your problem.

Link to comment
Share on other sites

Take a long look at how I coded up your page.  Only one php start tag and one end.  Pretty simple. You don't have to use a pair of tags for a single line of php code.  But - all you had to do was move your first php start tag to the very first line in column 1 and that would have cured you if you got rid of the end tags too.

HTH

Edited by ginerjm
Link to comment
Share on other sites

I would urge you to adopt most if not all the coding standards in PSR-12 from the PHP Framework Interop group, which has published many useful PHP standards.  In particular as mentioned, look at 2.2 which tells you to omit a php end tag.  Do this whenever and wherever you can.  In conclusion, your php files should never end with a closing tag, and would only need a closing tag if there is an intermixing of php and html blocks.  Even if you do, as ginerjm helpfully demonstrated, you won't ever have a closing tag at the end of the file, regardless of whether the file ended with an html block or a php block.

Link to comment
Share on other sites

  • gizmola changed the title to issues with start_session() before header()
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.