Cannot get sessions working for login

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Cannot get sessions working for login

Post by tenacious-dee »

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\anml\EN\Admin\adminHome.php:2) in C:\xampp\htdocs\anml\EN\Admin\adminHome.php on line 2

I get the above error when trying to use a session

the only line above this is the doctype for the html document... could this have anything to do with it?
Zehna
Forum Newbie
Posts: 5
Joined: Thu Jan 29, 2009 12:48 pm
Location: Winter Park, FL

Re: Cannot get sessions working for login

Post by Zehna »

Could you also post the session code you are using with this? It would be easier to help you.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cannot get sessions working for login

Post by VladSun »

tenacious-dee wrote:Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\anml\EN\Admin\adminHome.php:2) in C:\xampp\htdocs\anml\EN\Admin\adminHome.php on line 2

I get the above error when trying to use a session

the only line above this is the doctype for the html document... could this have anything to do with it?
You should read your PHP error messages. PHP says:
"You have some data sent in response on line 2 in C:\xampp\htdocs\anml\EN\Admin\adminHome.php, so I can't send any more headers.
There are 10 types of people in this world, those who understand binary and those who don't
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Re: Cannot get sessions working for login

Post by tenacious-dee »

Code: Select all

<?php
 
$user = ($_POST["username"]);
$pass = ($_POST["password"]); 
 
//connect to server and select database
$mysqli = mysqli_connect("localhost", "root", "Dee1987!", "anml");
 
//create the issue query
$sql = "SELECT username, password FROM tbladmin WHERE username = '".$_POST["username"]."' AND password = '".$_POST["password"]."'";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
 
//get the number of rows in the result set; should be one if a match is found
if (mysqli_num_rows($result) == 1)
    {   
        //setcookie("auth", "0", time()+1);
        session_start();
        $_SESSION['login'] = "1";
 
        header("Location: ../adminHome.php");
        
    }
    else
    {
        //redirect back to the login form if not authorised
        session_start();
        $_SESSION['login'] = '';
        header("Location: adminReLogin.html");
        exit;
    }
?>
and then the code in the page throwing the error is:

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
echo "You are not authorised to view this page";
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Áras na Mac Léinn | Student Centre UCC</title>
<link href="../style.css" rel="stylesheet" type="text/css"/>
</head>


I understand what the error is telling me but no matter where i put the session_start() in the page - even in line one where there is nothing being sent to the browser before it, i still get the error....
mavieng
Forum Newbie
Posts: 10
Joined: Thu Jan 29, 2009 12:34 pm

Re: Cannot get sessions working for login

Post by mavieng »

session_start() should be very first line of php file. There should not be even one blank space before <?php tag.
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Re: Cannot get sessions working for login

Post by tenacious-dee »

i fixed it there

it was a combination of things

- code needed to be on the first line
- session_start() didn't need to be called in the first file

so now it looks something like this.. just incase anybody else is experiencing an error:

Code: Select all

<?php
 
$user = ($_POST["username"]);
$pass = ($_POST["password"]); 
 
//connect to server and select database
$mysqli = mysqli_connect("localhost", "root", "***", "anml");
 
//create the issue query
$sql = "SELECT username, password FROM tbladmin WHERE username = '".$_POST["username"]."' AND password = '".$_POST["password"]."'";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
 
//get the number of rows in the result set; should be one if a match is found
if (mysqli_num_rows($result) == 1)
    {   
 
 
       header("Location: ../adminHome.php");
       session_start();
       $_SESSION['login'] = "1";        
    }
    else
    {
        //redirect back to the login form if not authorised
        header("Location: adminReLogin.html");
        exit;
    }
?>
 
and in the next page

Code: Select all

<?php
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: Login/notAuth.html");}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Áras na Mac Léinn | Student Centre UCC</title>
<link href="../style.css" rel="stylesheet" type="text/css"/>
</head>
 
cheers for all the help guys
Post Reply