Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Cookies

  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cookies

    Hi. My name is Zack, and I'm having some serious problems with PHP. I have a simple login script, but it does not work. It finds the user in the MySQL database just fine, but it won't set the cookie.

    I'm having literally the same problem as this guy: "Headers already sent" Cookie Problem . I did everything he did to get it working, but no luck.

    PHP Code:
        <?php
        $result 
    "";
        if (
    $_POST["action"] == "login")
        {    
        
    $link mysql_connect('localhost''root''*****');
        if (!
    $link) {
            die(
    'A MySQL error occured. You could not be logged in.');
        }
        
    mysql_select_db("magellis") or die( "A MySQL error occured! You could not be logged in.");
        
    $sql "SELECT * FROM `users` WHERE `username` = '".$_POST["username"]."'";
        
    $query mysql_query($sql);
        while (
    $row mysql_fetch_array($query)) { 
        if (
    $row["username"] == $_POST["username"])
        {
             if (
    $row["password"] == md5($_POST['password'])) {
             
    //valid
             
    setcookie("magloggedin"$row["username"], 2592000);
             
    $result "Welcome, ".$row["username"];
             }else{
             
    $result "Invalid Password.";
             }
        }
        else {
        
    $result "Invalid Username";
            }     
        }
        
    mysql_close($link);
        }
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Magellis Networks</title>
    </head>
        <style type="text/css">
        .minititle 
        {
            font-size: medium;
        }
        a:link, a:visited
        {
            color: Black;
            text-decoration: none;
        }
        a:hover, a:active
        {
            color: Black;
            text-decoration: underline;
        }
        </style>
    <body style="background-image: url(bggradient.png); font-family: 'Segoe UI'; font-size:small;">
    <center>
    <br /><br /><br />
    <?php echo $result?>

    </center>

    </body>

    </html>
    When I use the right credentials, it gives me this:
    Code:
    Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\magellis.net\control.php:1) in C:\xampp\htdocs\magellis.net\control.php  on line 16
    
    
    
    Welcome, zDawg32
    I checked my cookies, none from localhost.

    Oh, and please don't tell me about how everything inside my while statement is done wrongly, I know.

    EDIT: Line 16 is the setcookie
    Last edited by zDawg32; 06-18-2010 at 01:53 PM. Reason: RESOLVED!

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    From what I see you aren't outputting anything before the cookie header is sent. Any chance it's the same problem as this:

    http://www.phpbuilder.com/board/arch...-10330991.html

    Check for white space before the <?php tag. If not that, then I don't think you need ANSI encoding, but you might need UTF-8 without BOM or something. I can't think of anything else that should create that problem.
    Last edited by Rowsdower!; 06-18-2010 at 01:24 PM. Reason: Forgot to mention white space...
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, ANSI made the error go away, but now it doesn't set the cookie. - scratch that. It was because I used _GET instead of _POST.
    This is my PHP so far:
    PHP Code:
    <?php
    if ($_POST['action'] == 'login')
    {    
    $link mysql_connect('localhost''root', ***********);
    if (!
    $link) {
        die(
    'A MySQL error occured. You could not be logged in.');
    }
    mysql_select_db('magellis') or die('A MySQL error occured! You could not be logged in.');
    $sql 'SELECT * FROM `users` WHERE `username` = \''.$_POST["username"].'\'';
    $query mysql_query($sql);
    $user false;
    while (
    $row mysql_fetch_array($query)) { 
    $user true;
         if (
    $row['password'] == md5($_POST['password'])) {
             
    //valid
            
    setcookie('magloggedin'$_POST['username']);
            
    $result "Welcome, ".$_POST["username"];
         }else{
            
    $result "Invalid PASSWORD";
         }
    }
    if (
    $user==false) {
    $result "Invalid USERNAME";
    }
    mysql_close($link);
    }
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Magellis Networks</title>
    </head>
        <style type="text/css">
        .minititle 
        {
            font-size: medium;
        }
        a:link, a:visited
        {
            color: Black;
            text-decoration: none;
        }
        a:hover, a:active
        {
            color: Black;
            text-decoration: underline;
        }
        </style>
    <body style="background-image: url(bggradient.png); font-family: 'Segoe UI'; font-size:small;">
    <center>
    <br /><br /><br />
    <?php echo $result?>

    </center>

    </body>

    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •