Jump to content


Photo

Error with headings and cookies


  • Please log in to reply
6 replies to this topic

#1 jacer17

jacer17
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 June 2006 - 10:45 PM

The orginal script
<?

    $uname = $_POST['uname'];
    $password = $_POST['password'];

    if( (!$uname) or (!$password) )
    { header("Location:login.php"); exit(); }

    $conn = mysql_connect('localhost', 'username', 'password')
    or die('wrong connection info');

    mysql_select_db('fcp_fcodes') 
    or die('No database named that');

    $sql="select * from fcdatabase where uname='$uname' and password = password('$password')";

    $rs = mysql_query($sql)
    or die("could not execute query");
 
    $row = mysql_fetch_object($rs);
    $id = $row->id;
    $firstname = $row->fname;
    $num = mysql_numrows($rs);

    if($num !=0 )
    {
        setcookie("id", $id, time()+36000);
        setcookie("firstname", $firstname, time()+36000);
        header( "Location:settings.php?id=$id");
        exit();

    }
    else
    {
    header( "Location:login.php" ); 
    exit(); 
    }


?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form action="auth.php" method="post">
<table>
<tr>
<td><b>Username:</b></td>
<td><input type="text" name="uname" size="25" /></td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input type="password" name="password" size="25" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Log In" /></td>
</tr>
</table>

</form>



</body>
</html>

The error
Warning: Cannot modify header information - headers already sent by (output started at /home/fcp/public_html/friendcode/login.php:2) in /home/fcp/public_html/friendcode/login.php on line 8

I would like to know why it is doing this and what are some ways to correct the script.


#2 homchz

homchz
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationLee, New Hampshire

Posted 17 June 2006 - 10:47 PM

Which one is line 8??

#3 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 17 June 2006 - 10:48 PM

Looks like the script above the second Location header is outputting something (maybe an error message you've set up?) and that will cause the redir to fail.
[table]



Don't worry, the printer fairies will sort it out.

#4 jacer17

jacer17
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 19 June 2006 - 11:37 PM

I tryed modifying the script.
<?
if(!$_POST['submit'])
{
        $uname = $_POST['uname'];
        $password = $_POST['password'];
        
    
    
        if( (!$uname) or (!$password) )
            { 
                header(" Location: login.php "); exit(); 
            }
            
        $conn = mysql_connect('localhost', 'connect', 'connect')
        or die('wrong connection info');
    
        mysql_select_db('fcp_fcodes') 
        or die('No database named that');
    
        $sql="select * from fcdatabase where uname='$uname' and password = password('$password')";
    
        $results = mysql_query($sql)
        or die("could not execute query");
     
        $row = mysql_fetch_object($results);
        $id = $row->id;
        $firstname = $row->fname;
        $num = mysql_numrows($results);
    
        if($num !=0 )
            {
                setcookie("id", $id, time()+36000);
                setcookie("firstname", $firstname, time()+36000);
                header( " Location: settings.php?id=$id ");
                exit();
    
            }
    
    
        else
            {
            header( " Location: login.php " ); 
            exit(); 
            }
}
else
{
?>
<html>
<head>
</head>

<body>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<table>
<tr>
<td><b>Username:</b></td>
<td><input type="text" name="uname" size="25" /></td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input type="password" name="password" size="25" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Log In" /></td>
</tr>
</table>

</form>



</body>
</html>
<?
}
?>

This time I get only a blank page

#5 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 20 June 2006 - 02:42 AM

The header function will fail if there is so much as a single invisible space before your first php start tag.
If you are going to use the header() or cookies or session variables, put the php start tag on the first line of the source code without so much as a singe space before it.

<?php //must be on the very first line, no spaces before it


#6 jacer17

jacer17
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 June 2006 - 04:40 AM

The php tag is the first line, except the pasted code.
There is no spaces before it and it is top line.

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 June 2006 - 10:54 AM

This:
<?
if(!$_POST['submit'])
{
should be:
<?
if(isset($_POST['submit']))
{

ALso I highly suggest you chnage this:
$uname = $_POST['uname'];
$password = $_POST['password'];
to the following:
$uname = mysql_real_escape_string(trim($_POST['uname']));
$password = mysql_real_escape_string(trim($_POST['password']));





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users