Jump to content

session_start() [function.session-start]: Cannot send session cookie - headers a


ra_ie_darkness

Recommended Posts

I am trying to create an index page which contains registration and login field

the problem that i get is on successful login a warning is displayed

session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\Eventz.com\index.php:116) in C:\xampp\htdocs\Eventz.com\index.php on line 235

 

This is the login part of my index.php

this tag is inside an html table below the login form

I also have a registration form and its php code above the login form

 

 

<?php
if (isset($_REQUEST['pass']))
{
    $id=$_POST['id'];
    $pass=$_POST['pass'];

    $conn	=mysql_connect("localhost","root","");
    if (!$conn)
    {
   die('Could not connect: ' . mysql_error());
    }
/*
checking connection....success!
*/
    $e=mysql_select_db('test', $conn);
    if(!$e)
    {
   die(''.mysql_error());
    }
    else
    {
   echo 'database selected successfully';
    }

    if (isset($_REQUEST['id'])  || (isset($_REQUEST['pass'])))
    {
        if($_REQUEST['id'] == "" || $_REQUEST['pass']=="")
        {
       echo "login fields cannot be empty";
        }
        else
        {
            $sql=mysql_query("Select email,password from login where email='$id' AND password='$pass'");
            $count=mysql_num_rows($sql);
            if($count==1) 

    /* $count checks if username and password are in same row */
            {   
                session_start();
                $_SESSION['id']=$id;
                echo "</br>Login Successful</br>";
            }
            else
            {
           echo "</br>invalid</br>";
           echo "please try to login again</br>";

            }
        
        }   
    }
}
?>

 

 

Any help or suggestion would be appreciated

this tag is inside an html table below the login form

 

I take it that means there is HTML above this block of PHP code?  You can't call session_start after you have sent output (which includes anything, even blank space) which is outside of <?php ?> tags.  Reason is session-start() has to send headers for the cookie, and sending output causes the headers to be sent which means you can't send any new headers after that point.

 

You need to move your processing code to be at the top of the script, before any other output.

 

see also, the sticky on header errors

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.