Results 1 to 12 of 12

Thread: PHP Cookie Help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    PHP Cookie Help

    hello got this php script i wrote to diplay stuff depending on the cookie value but getting errors on the setcookie anyone know why or can improve on the code please doo

    PHP Code:
    <?php
    if($_GET['CIEC'] == 3)
    {
        
    $TWO "2";
        
    setcookie("ShowOrHide",$TWO);
    }
    else
    {
     if (isset(
    $_COOKIE["ShowOrHide"]) AND $_COOKIE["ShowOrHide"] == "1")
     {
         echo 
    "<a href=\"index.php?CIEC=3\">Close</a>";
     }
     else if(isset(
    $_COOKIE["ShowOrHide"]) AND $_COOKIE["ShowOrHide"] == "2")
     {
         
    //Do Nothing
     
    }
     else
     {
         
    $ONE "1";
        
    setcookie("ShowOrHide"$ONE);
        echo 
    "<a href=\"index.php?CIEC=3\">Close</a>";
        echo 
    "<a id=\"tooCool\" href=\"http://www.w3.com/\"></a>";
     }
    }
    ?>
    Last edited by 308holes; Dec 6th, 2005 at 12:12 PM. Reason: code fix

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: PHP Cookie Help

    What are the messages you are receiving?

  3. #3
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: PHP Cookie Help

    When you set a cookie, it has to be set before any data is acctually sent.
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    Re: PHP Cookie Help

    im getting


    Warning: Cannot modify header information - headers already sent by (output started at htdocs\index.php:9) in htdocs\index.php on line 143

  5. #5
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: PHP Cookie Help

    Exacactlly what k1ll3rdr4g0n said

    You can't write anything to your webpage b4 you set your cookie.

    PHP Code:
    //Will work
    setcookie("ShowOrHide"$ONE); 
    echo 
    "Cookie set";

    //Won't work
    echo "About to set cookie";
    setcookie("ShowOrHide"$ONE); 

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    Re: PHP Cookie Help

    ok I think I see what your saying but i am not writing anything untill the php checks to see if the cookie is there and what value it has could it be because the css info for that tag is being processed before the cookie if soo how do i get around that ?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    Re: PHP Cookie Help

    ok im lost

    html code:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <link href="main.css" rel="stylesheet" type="text/css" media="all" />
    </head>
    <body>
    <?php
    if($_GET['CIEC'] == 3)
    {
        
    $TWO "2";
        
    setcookie("ShowOrHide",$TWOtime()+604800);
    }
    else
    {
     if (isset(
    $_COOKIE["ShowOrHide"]) AND $_COOKIE["ShowOrHide"] == "1")
     {
         echo 
    "<a href=\"index.php?CIEC=3\">Close Cool IE</a>";
        echo 
    "<a id=\"tooCool\" href=\"http://www.w3junkies.com/toocool/\"></a>";
     }
     else if(isset(
    $_COOKIE["ShowOrHide"]) AND $_COOKIE["ShowOrHide"] == "2")
     {
         
    //Do Nothing
     
    }
     else
     {
         
    $ONE "1";
        
    setcookie("ShowOrHide"$ONE);
        echo 
    "<a href=\"index.php?CIEC=3\">Close Cool IE</a>";
            echo 
    "<a id=\"tooCool\" href=\"http://www.w3junkies.com/toocool/\"></a>";
     }
    }
    ?>
    </body>
    </html>
    CSS:
    PHP Code:
    a#tooCool {
    positionfixed;
    right0;
    bottom0;
    displayblock;
    height80px;
    width80px;
    backgroundurl(imgs/too_cool_corner.pngbottom right no-repeat;
    text-indent: -999em;
    text-decorationnone;


  8. #8
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: PHP Cookie Help

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <link href="main.css" rel="stylesheet" type="text/css" media="all" />
    </head>
    <body>
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    Re: PHP Cookie Help

    Ok think i got it now
    Last edited by 308holes; Dec 7th, 2005 at 08:17 AM.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    Re: PHP Cookie Help

    ok go the code working now works good just for g wiz info there is no ways to set the cookie from inside the html code like above code ?

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP Cookie Help

    A cookie is sent to the client via the HTTP header in the HTTP response. A typical HTTP response may look like this:
    Code:
    HTTP/1.1 200 OK
    Date: Wed, 07 Dec 2005 15:38:00 GMT
    Server: Apache
    Set-Cookie: RMID=50b16804439701d0; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.vbforums.com bblastvisit=1133969880; expires=Thu, 07-Dec-06 15:38:00 GMT; path=/; domain=.vbforums.com bblastactivity=1133969868; expires=Thu, 07-Dec-06 15:38:00 GMT; path=/; domain=.vbforums.com
    Expires: 0
    Cache-Control: private, post-check=0, pre-check=0, max-age=0
    Pragma: no-cache
    Content-Length: 112655
    Content-Type: text/html; charset=ISO-8859-1
    Proxy-Connection: close
    
    <html>
        <head>
        </head>
        <body>
        <!-- page content here -->
        </body>
    </html>
    This is the response you get from vbforums when you first go to the site. The set cookie string is highlighted. As you can see it is a header and appears before the main body of the page.

    In PHP the set_cookie function simply adds one of these header. However, the second you produce any output the headers are flushed through and the body of the HTTP response is started.

    You can get around this by using output buffers, which collect all the data in a buffer before sending it to the client. However, it is bad practice to do this. A well designed script will keep all output separate from the scripts processes.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    Re: PHP Cookie Help

    Cool thanks for the example :-)

Posting Permissions

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



Click Here to Expand Forum to Full Width