Jump to content

Archived

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

jacer17

Error with headings and cookies

Recommended Posts

The orginal script
[code]
<?

    $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>
[/code]

The error
[code]
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
[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I tryed modifying the script.
[code]
<?
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>
<?
}
?>
[/code]

This time I get only a blank page

Share this post


Link to post
Share on other sites
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.

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

Share this post


Link to post
Share on other sites
The php tag is the first line, except the pasted code.
There is no spaces before it and it is top line.

Share this post


Link to post
Share on other sites
This:
[code]<?
if(!$_POST['submit'])
{[/code]
should be:
[code]<?
if(isset($_POST['submit']))
{[/code]

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

Share this post


Link to post
Share on other sites

×

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.