Jump to content

Error with headings and cookies


jacer17

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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