Page 1 of 1

Cannot get sessions working for login

Posted: Thu Jan 29, 2009 1:45 pm
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?

Re: Cannot get sessions working for login

Posted: Thu Jan 29, 2009 2:10 pm
by Zehna
Could you also post the session code you are using with this? It would be easier to help you.

Re: Cannot get sessions working for login

Posted: Thu Jan 29, 2009 2:55 pm
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.

Re: Cannot get sessions working for login

Posted: Fri Jan 30, 2009 4:58 am
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....

Re: Cannot get sessions working for login

Posted: Fri Jan 30, 2009 5:59 am
by mavieng
session_start() should be very first line of php file. There should not be even one blank space before <?php tag.

Re: Cannot get sessions working for login

Posted: Fri Jan 30, 2009 6:09 am
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