Thread: Cookies in PHP3

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Bucharest, Romania
    Posts
    4
    Rep Power
    0
    Hello,
    I tried to set up a temporary cookie, using this code:

    setcookie("CookieID","",time()+3600,"/");

    but i'm permanently receiving the same error:

    Warning: Oops, php3_SetCookie called after header has been sent in /usr/local/apache/htdocs/rex/admin/login.php3 on line 22

    Can anyone help me ? The lines before are only for checking a username and password from a mysql database.

    Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Englewood, CO
    Posts
    30
    Rep Power
    15
    My guess is that you have your cookie-setting PHP code somewhere after the header has already been sent to the client. Setting cookie(s) is the first thing your PHP code should do, before <HTML> or anything else.

    -TM
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    durham, nc, usa
    Posts
    14
    Rep Power
    0
    I did this too. What a headache.

    Here's the deal. Apparently ANYTHING you put in your php3 file ahead of the first "<?" tag causes your server to send a header and this information. If you are writing code that sets a cookie, you have to ensure that there is no whitespace (or text) ahead of your php tag. You can execute plenty of code between the tag and the set_cookie function (as long as you don't printf or echo), including require other files (just don't have any whitespace in them either).

    My guess is you have an html comment and perhaps some indentation between the start of file and the php tag containing your setcookie.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Posts
    9
    Rep Power
    0
    Hi there
    I've got an other problem with cookies. When I try to set a cookie with the following command
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    setcookie("RechtsGebruiker", $RechtsGebruiker, time()+31536000, "/rechts/scripts/", "cc5336-a.ensch1.ov.nl.home.com", 0);
    [/code]
    It won't work... neither with Netscape nor with IE. I don't have the problem with "Oops, php3_setCookie called after..."
    It simply doesn't set the cookie but it doesn't generate an error ....
    Could anyone please help as soon as possible? I really need this....

    I suddenly realise the following: the client doesn't "GET" the page, but retrieves it after a "POST". In other words, there's a form that has its action-tag set to this page. Does this matter?

    Marc

    P.S. Here's the script source:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <?PHP
    // bevat functie voor controle username & password
    include "security.php3";

    // Als de eerste parameter een 1 is, dient een fout gemeld te worden! Uitleg:
    // vanuit het hoofdframe (../index.html) wordt dit script aangeroepen,
    // maar dan zonder deze parameter. Als de gebruiker geen cookie geset
    // heeft, zal het inloggen in dit script dus altijd foutgaan, maar
    // dit hoeft dan niet gemeld te worden. Wanneer dit script vanuit het
    // inlog scherm (inloggen.html) wordt aangeroepen, moet bij een fout
    // de fout wel genoemd worden! Daarom wordt vanuit inloggen.html dus
    // wel een 1 meegegeven als parameter.
    //
    // auto_inlog geeft aan of er sprake is van een "automatische inlog" of via het form
    $auto_inlog = ($argv[0] == 0);

    // als het via het form gegaan is, moeten 2 vars gezet worden naar de waarden uit het form
    if (!($auto_inlog)) {
    $RechtsGebruiker = $naam;
    $RechtsPassword = $wachtwoord;
    }

    $succesvol_ingelogd = 0;
    if (($RechtsGebruiker != "") && ($RechtsPassword != "")) {
    $succesvol_ingelogd = security_controleer_up($RechtsGebruiker, $RechtsPassword);
    }

    if ($succesvol_ingelogd == 1) {
    // zet cookie als de gebruiker dat wil
    if (!($auto_inlog)) {
    if ($zetcookieaan) {
    setcookie("RechtsGebruiker", $RechtsGebruiker, time()+31536000, "/rechts/scripts/", "cc5336-a.ensch1.ov.nl.home.com", 0);
    setcookie("RechtsPassword", $RechtsPassword, time()+31536000, "/rechts/scripts/", "cc5336-a.ensch1.ov.nl.home.com", 0);
    echo "Cookie WEL gezet<BR>";

    } else {
    echo "Cookie NIET gezet<BR>";
    }
    }

    // toon aanwezige links en e-mail
    echo "Inloggen succesvol! Welkom $RechtsGebruiker";
    } else {
    // inloggen niet goed gegaan.
    if ($argv[0]) {
    echo "<FONT COLOR="red"><B><CENTER>Onjuiste gebruikersnaam en wachtwoord, probeer opnieuw!</B></CENTER><BR><BR>";
    }
    include "inloggen.html";
    }
    ?>[/code]

    It DOES show "Cookie WEL gezet", so it should have parsed the setcookie command!!

    ----------------------------------------
    ceterum censeo linguam c++ esse delendam

    [This message has been edited by Foddex (edited March 06, 2000).]
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    Dallas,TX, USA
    Posts
    11
    Rep Power
    0
    Ive had this problem before also however what I found was that the cookie was being set however cookies will not become visible until the next loading of a page that the cookie should be visible for. If your testing on the same page that you set it on then you wouldnt get a error, or a cookie value.

    ------------------
    DbAbility.com


    [This message has been edited by ki (edited March 07, 2000).]

    [This message has been edited by ki (edited March 07, 2000).]
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    1
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by cciipp:
    Hello,
    I tried to set up a temporary cookie, using this code:

    setcookie("CookieID","",time()+3600,"/");

    but i'm permanently receiving the same error:

    Warning: Oops, php3_SetCookie called after header has been sent in /usr/local/apache/htdocs/rex/admin/login.php3 on line 22

    Can anyone help me ? The lines before are only for checking a username and password from a mysql database.

    Thank you.
    [/quote]

    You should check if there are any line breaks not within <?php and ?>. Empty lines before the <?php tag are considered as start of an output and no header lines can be sent after that.
    This also includes all #include("")'ed files (if any). Check if there any empty lines at the begining or (especialy )at the end of them, not within <?php ?>.

    I hope this'll solve your problem.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    1
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by cciipp:
    Hello,
    I tried to set up a temporary cookie, using this code:

    setcookie("CookieID","",time()+3600,"/");

    but i'm permanently receiving the same error:

    Warning: Oops, php3_SetCookie called after header has been sent in /usr/local/apache/htdocs/rex/admin/login.php3 on line 22

    Can anyone help me ? The lines before are only for checking a username and password from a mysql database.

    Thank you.
    [/quote]

    So, I have the same problem. I did this to be sure that there was nothing else send to the browser before the cookie:

    $mysql_link = @mysql_connect("server_name", "user", "pass");
    @mysql_select_db("peter.kostov", $mysql_link) or Die ("Table connection error ...");
    $query = "some_query";
    $mysql_result = @mysql_query($query);

    The @ sign disable the error or any other messages that the MySql server could sent to your script.




    ------------------
    Petre KOSTOV
    peter_kostov@writeme.com
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    43
    Rep Power
    15
    Cookie Error Problem:
    I had this same problem with cookies. The fix is that you can not even have a space or line before any php tags i.e. <? ?>. If you do it will be output to the browser and that is considered the first header.

    For Foddex's problem. The line:
    $succesvol_ingelogd = 0;
    will always be zero so how can you get to the code that sets the cookies. It will never equal 1 will it?
    Maybe you meant to place this statement elswhere but it looks like it will always equal 0.


    ------------------
    webpatron.com
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    5
    Rep Power
    0
    Hi !

    This some very small problem.

    your setcookie command is after your <HTML> tag. So that the error will there.

    becos when the <HTML> tag exection your entire header information is passed to browser from the web server. The header have the cookie value and form hidden values and query string, URL etc.

    If you are user the setcooike function you don't use the echo or print command before <html> tag . you can use after <html> tag.

    I think you can understand this answer.

    If any further problem or other problem let me know the details

    please drop some line at "mariyasikumar@yahoo.com"

    Thanks and Regards
    Mariya.k
    Chennai - India


    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by cciipp:
    Hello,
    I tried to set up a temporary cookie, using this code:

    setcookie("CookieID","",time()+3600,"/");

    but i'm permanently receiving the same error:

    Warning: Oops, php3_SetCookie called after header has been sent in /usr/local/apache/htdocs/rex/admin/login.php3 on line 22

    Can anyone help me ? The lines before are only for checking a username and password from a mysql database.

    Thank you.
    [/quote]



    ------------------
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Posts
    9
    Rep Power
    0
    lhallusa> You're wrong. This code:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    if (($RechtsGebruiker != "") && ($RechtsPassword != "")) {
    $succesvol_ingelogd = security_controleer_up($RechtsGebruiker, $RechtsPassword);
    }
    [/code]
    can set $succesvol_ingelogd to 1!

Similar Threads

  1. cant delete cookies random errors
    By toxical in forum PHP Development
    Replies: 2
    Last Post: November 26th, 2003, 05:16 AM
  2. Clearing all cookies php script - HowTo?
    By fordfasterr in forum PHP Development
    Replies: 8
    Last Post: October 24th, 2003, 09:40 AM
  3. Problems setting cookies with a path and domain
    By IBBoard in forum PHP Development
    Replies: 4
    Last Post: July 2nd, 2003, 03:03 PM
  4. Is it better to use sessions or cookies?
    By m0nk3y in forum PHP Development
    Replies: 7
    Last Post: March 4th, 2002, 02:35 AM
  5. AOL cookies?
    By cfthong in forum PHP Development
    Replies: 0
    Last Post: August 18th, 2000, 10:59 AM

IMN logo majestic logo threadwatch logo seochat tools logo