cookies

Discussion in 'PHP' started by killaklown, Jul 3, 2006.

  1. #1
    Im new to php and i have a question with setting cookies, right now i have:

    
    <?
    if($_POST["uname"]=="" & $_POST["uname"]==""){
    }
    else{
    if($_POST["uname"]=="$username" & $_POST["uname"]=="$password"){
    echo'Login is successful!';
    $username = $_POST['uname'];
    setcookie("phpli", $username, time()+3200);
    }
    else{
    echo'Username and/or Password is incorrect!';
    }
    }
    ?>
    
    Code (markup):
    but i get this error when putting in the correct username/password:

     
    killaklown, Jul 3, 2006 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    Put the "echo" line after the "setcookie" line.

    I won't get into the PHP code itself (heh), but that should fix the error for you.
     
    digitalpoint, Jul 3, 2006 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    still shows that error, but above 'Login is succesful!'


    heres the code above it:
    
    <html>
    <head>
    <title>test</title>
    </head>
    <body>
    <? include('../config.php'); ?>
    <center>
    <table width="375" border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000">
    <tr><td align="middle" bgcolor="#589f32">
    <b>Login to the administrator panel!</b><br>
    </td></tr><tr><td>
    
    Code (markup):
     
    killaklown, Jul 3, 2006 IP
  4. chandubhai

    chandubhai Banned

    Messages:
    556
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can try getting around using ob_start() and ob_end_flush().
     
    chandubhai, Jul 3, 2006 IP
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    I got it now, read the php manual and i was suppose to put the code before the <html> and <head> tags.
     
    killaklown, Jul 3, 2006 IP
  6. weppos

    weppos Well-Known Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    125
    #6
    You need to put it before any page code or print, including any tag. :)
     
    weppos, Jul 4, 2006 IP
  7. ahref

    ahref Well-Known Member

    Messages:
    1,123
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    170
    #7
    There should not be any ouput before setcookie function.
     
    ahref, Jul 4, 2006 IP