Login problem??

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
yoursanjay
Forum Newbie
Posts: 17
Joined: Sat Feb 23, 2008 12:21 pm

Login problem??

Post by yoursanjay »

:?:
I have some problem with my login script. 2 warnings are showing..
Cannot send session cookie - headers already sent by
Cannot send session cache limiter - headers already sent

login.php ..........................................................

Code: Select all

 
<?php
session_start();
if (isset($_POST['username']) && $_POST['password']) 
{
    $username=$_POST['username'];
    $password=$_POST['password'];
    
    $database="bluebell";
 
    $connect=mysql_connect("localhost","root","") or die("Couldn't connect to the database" . mysql_error());
    mysql_select_db("$database",$connect) or die("Couldn't select database" . mysql_error());
    
    $query="select * from user where user_name='$username' and password='$password'";
    $result= mysql_query($query,$connect) or die("Unable to verify user" .mysql_error());
    $info=mysql_fetch_assoc($result);
    $id=$info['user_Id'];
    $exp=60*60*24;
    if (mysql_num_rows($result)==1)
    {
        setcookie('u',$id,$exp);
        $_SESSION=true;
        header("Location:profile.php?id='.$id.'");
    }
    else 
    {
        $error="Incorrect Username or Password";
    }   
}
?>
 
<html>
<head>
<title></title>
</head>
<body>
 <form action="" method="POST">
 <p><input type="text" name="username">User Name:</p>
 <p><input type="password" name="password">Password:</p>
 
 <p><input type="submit" value="Login"></p>
 <?php echo "$error";?>
 </form>
</body>
</html>
 
and the profile.php ......................

Code: Select all

 
<?php
session_start();
if(isset($_COOKIE['u']))
{
    header("Location:login.php");
}
$id=$_COOKIE['u'];
if(!isset($_SESSION[$id]) || $_SESSION[$id]!== true)
{
    header("Location:login.php");
    exit();
}
echo "You are currently logged in.";
 
?>
 
plz help me.. :?:
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Login problem??

Post by Reviresco »

You have to set a cookie or header before ANY output is sent to the page -- this includes even a single space or line break. Check to make sure nothing is being sent before the cookie and/or header, and put the scripts before any html output.
Post Reply