SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    UK
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP cookie parse error

    Hi all,
    Im trying to implement a simple cookie into my site, similar to that off kevins book, but i get the parse error. the ordering i believe is correct, what is wrong?

    <?php
    setCookie("visited","1",time()+3600*24*365);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    ....
    ...
    <body>
    <table>
    <?
    if (!isset($visited)) {
    echo (" information information ");
    }
    ?>

  2. #2
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No HTML ouput is allowed before a setCookie() call. Make sure there are no spaces before or after <?php and ?>



    Cheers.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    UK
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The <? setcookie(.........); ?> is the first thing in the index.php file...nothing before it.... No spaces...

    thanks

    John

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by filmfoto
    No HTML ouput is allowed before a setCookie() call. Make sure there are no spaces before or after <?php and ?>
    that wouldn't cause a parse error, just a warning.

    johntaba, i see nothing wrong. i just copied and ran your exact code with no problems.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    UK
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, i worked out whats wrong. The set cookie function works fine....its just that the actual printing of the text doesnt as i havent printed out <tr> and <td>, ill add that in now!!

    thanks for help,

    sorry for wasting your time


    John

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    UK
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    New problem,
    THe cookie sets itself fine, but then, I want to determine if its the users first visit to the site or not. If it is, i want to add an extra few lines at the beginning of a table....introductory paragraph as such. If its not the users first time, hence a cookie has been set, I want nothing to be added....so the page continues as it should.... Why does the following piece of code not work then?

    <?
    if (!isset($visited)) {

    echo ("<tr>");
    echo ("<td bgcolor=\"#ffffff\" valign=\"center\">");
    echo (" Street-Drugs.com offers accurate, honest up to date information on street
    drugs such as marijuana, cocaine, heroin, acid and much more. Read the
    <a href=\"introduction.php\">introduction</a> section for a more detailed
    overview.");
    echo ("</tr>");
    echo ("</td>");

    }

    ?>




    Well, it works ok, but if the cookie is set it still shows this, whats wrong?

    thanks

    John

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dunno, it looks OK as long as you haven't created a $visited variable in your script. why don't you try

    if (!isset($HTTP_COOKIE_VARS['visted']))

    which is really how you should be accessing variables anyway.

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    UK
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the help guys, but it still isnt working. Here is the complete scripts. At the top of the page i have
    <?
    setCookie("visited","1",time()+3600*24*365);
    ?>

    Then later down the page....I want first time visitors ONLY to see the following introductory paragraph.

    <?
    if (!isset($HTTP_COOKIE_VARS['visted'])) {

    echo ("<tr>");
    echo ("<td bgcolor=\"#ffffff\" valign=\"center\">");
    echo (" Street-Drugs.com offers accurate, honest up to date information on street
    drugs such as marijuana, cocaine, heroin, acid and much more. Read the
    <a href=\"introduction.php\">introduction</a> section for a more detailed
    overview.");
    echo ("</tr>");
    echo ("</td>");

    }

    ?>

    And if they have already visited the page...then the above script should be ignored. What is wrong?

    Thanks

    John

  9. #9
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by johntaba
    PHP Code:
    <?
    setCookie
    ("visited","1",time()+3600*24*365);
    ?>

    <?
    if (!isset($HTTP_COOKIE_VARS['visted']))   {

    You have a "typo". Check your visited variables.

    Should be:
    PHP Code:
    $HTTP_COOKIE_VARS['visited'




    Cheers.

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    UK
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    unfortunately that makes no difference....I thought that was the problem but no. It still doesnt work. I think the logic behind it must be wrong. Am i supposed to send the cookie first at the top of the page? It still shows the damn introdoctory paragraph even when ive visited loads more than once and the cookie is registered in my temp files.

    Thanks

    John

  11. #11
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry I didn't catch this before. You should put the logic first.
    PHP Code:
    if (!isset($HTTP_COOKIE_VARS['visited'])) { 
        
    setCookie("visited","1",time()+3600*24*365);
        
    /* introduction code */
    ...} 
    Because your new setCookie() is first available on your next page refresh.

    Let's put it this way: if a new user arrives at your site, set the cookie visited, then echo the introduction to the screen. If a returning user has arrived at your site, the cookie is checked and no introduction is echoed and no cookie is set.


    Understand the difference to your code?




    Cheers.

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    UK
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help filmfoto, but, i believe you cant set a cookie after HTML has been outputted. I tried the code anyway and i got a header error message. if you look at my main page www.street-drugs.com you will see that the introductory paragraph comes after a fair bit of HTML.

    Or did you mean that I use the previous code at the top of my page and assign the text to a variable and print that later on where the introductory paragraph should be?

    Thanks once again

    John

  13. #13
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is correct. A cookie can only be set before HTML output. That means all logic with cookies involved should be done before HTML output. When I code a script I do all logic first, assign all ouput to variables, and use a templating system to output what is neccessary.


    Hope this helps.




    Cheers.


Bookmarks

Posting Permissions

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