Jump to content

[SOLVED] Having three header errors which I cant seem to solve


farban

Recommended Posts

Hello there I am making a login for my social network site as a uni project. I have followed a tutorial for the login but when i login at the login.php page it shows up with three header errors stating

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\testsite\community\login.php:2) in C:\xampp\htdocs\testsite\community\login.php on line 17

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\testsite\community\login.php:2) in C:\xampp\htdocs\testsite\community\login.php on line 17

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\testsite\community\login.php:2) in C:\xampp\htdocs\testsite\community\login.php on line 23

 

Here is all the source code for the files that are related to the login of the site

 

login.php

 

<?php


if(isset($_POST['submitted']))   {

	require_once ('includes/loginfunctions.php');
	require_once ("includes/_connect.inc.php");


	list ($check, $data) = check_login ($dbc, $_POST['user_username'],
	$_POST['user_password']);

	if ($check) {


		session_start();
		$_SESSION['user_id'] = $data
		['user_id'];
		$_SESSION['user_first_name'] = $data
		['user_first_name'];
		$url = absolute_url ('loggedin.php');
		header("Location: $url");
		exit();
	}

	else {

		$errors = $data;


	}

	mysqli_close($dbc);


}


include ("includes/loginpage.php");





?>

 

loginfunctions.php

 

<?php

function absolute_url ($page = 'index.php')  {

	$url = 'http://'.$_SERVER['HTTP_HOST'].
	dirname($_SERVER['PHP_SELF']);


$url = rtrim ($url, '/\\');

$url .= '/'. $page;

return $url;

}

function check_login ($dbc, $user_username ='', $user_password ='') {

	$errors = array();


	if (empty($user_username))  {

		$errors[] ='you forgot to enter your username';

	}

	else	{

		$u = mysqli_real_escape_string($dbc, trim($user_username));

	}


	if (empty($user_password))  {

		$errors[] ='you forgot to enter your password';

	}

	else	{

		$p = mysqli_real_escape_string($dbc, trim($user_password));

	}

	if (empty($errors)) {

	$q = "SELECT user.user_id,
		    user.user_first_name
		    
	       FROM user 
	       
	       WHERE user.user_username ='$u' AND user.user_password =SHA1('$p')";
	       
	       
	       
	 $r = @mysqli_query($dbc, $q);



	 if (mysqli_num_rows($r) == 1) {


		$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);


	return array (true, $row);

	 } else { 

		 $errors[] = 'the username and password do not match';

	 }


}
	return array(false, $errors);


	}

		    






?>

 

loginpage.php

 

<?php
include("includes/start.php");

if (!empty($errors)) {

echo '<h1>Error</h1>
	<p>the following error occured:<br />';

	foreach ($errors as $msg) {

		echo " - $msg<br />\n";

	}

	echo '</p><p>Please try agian</p>';

}


?>

<h1>Login</h1>

<form action="login.php" method="post">
<p>Username : <input type="text" name="user_username" size="20" maxlengh="80"/>
</p>
<p>Password : <input type="password" name="user_password" size="20" maxlengh="80"/>
</p>
<p><input type="submit" name="submit" value="Login"/>
</p>
<input type="hidden" name="submitted" value="TRUE"/>

</form>

<?php
include("includes/footer.php");

?>

 

loggedin.php

 

<?php


session_start();

if (!isset($_SESSION['user_id'])) {


	$url = absolute_url();
	header("Location: $url");

	exit();



}

include("includes/start.php");


echo "<h1>You are Logged in</h1> <p>You are now logged in, {$_SESSION ['user_first_name']}!</p>

<p><a href=\"logout.php\">Logout</a></p>";

include("includes/footer.php");



















?>

 

logout.php

 

<?php 


session_start();

if (!isset($_SESSION['user_id']))  {


	require_once ('includes/loginfunctions.php');

	$url = absolute_url();

	header("Location: $url");

	exit();

}

else {

$_SESSION = array(); 

session_destroy();
setcookie ('PHPSESSID', '', time()-3600, '/', '',0,0);
}

include ("includes/start.php");

echo "<h1>logged out</h1>
<p>you are now logged out</p>";


include ("includes/footer.php");



?>

 

start.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head>

<link href="core/general.css" rel="stylesheet" type="text/css" /> <meta
http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta
http-equiv="Content-Language" content="en-us" /> 
<title>SCO SITE</title>






</head> 

<body>


<div class="container">


<div class="header"> 
<h6>HEADER</h6>


</div>



<div class="menu"> 
<li><a href="index.php">Home</a></li>


<li><?php

if ((isset($_SESSION ['user_id'])) && (!strpos($_SERVER['PHP_SELF'], 'logout.php'))) {

	echo '<a href="logout.php">Logout</a>';

}

else {
	echo '<a href="login.php">Login</a>';

}

?></li>



</div>

<div class="content"> 










 

Can anyone point out the problem ? im really pulling my hair out over this and want my login to work :(

 

Much thanks

 

Link to comment
Share on other sites

I read the sticky and sorry im just not very good at understanding php and the header problems.

 

Same number of header problems still :(

 

Here is what the login.php looks like now

 

login.php

 

<?php if(isset($_POST['submitted']))   {

	require_once ('includes/loginfunctions.php');
	require_once ("includes/_connect.inc.php");


	list ($check, $data) = check_login ($dbc, $_POST['user_username'],
	$_POST['user_password']);

	if ($check) {


		session_start();
		$_SESSION['user_id'] = $data
		['user_id'];
		$_SESSION['user_first_name'] = $data
		['user_first_name'];
		$url = absolute_url ('loggedin.php');
		header("Location: $url");
		exit();
	}

	else {

		$errors = $data;


	}

	mysqli_close($dbc);


}


require_once ("includes/loginpage.php");?>

Link to comment
Share on other sites

well i be dammed, on my book it says, if you use require_once() from a page with header's

you wont get the error message as it get the page once.

 

 

This has some sense in case a file with session_start() is included several times. Note however, that the error here occurs in main file, and before any include() is called.

And by giving an advice like you did, without any explanation you're just introducing confusion. Be careful about it.

Link to comment
Share on other sites

If the current error message is exactly the same as in the first post, you have two blank lines or one blank line and BOM characters before the <?php tag. If the error message is different, post it to get the quickest solution.

 

did u consider this?

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.