1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to drop a cookie using php ?

Discussion in 'PHP & Perl' started by jake3340, Nov 4, 2009.

  1. jake3340

    jake3340 Supreme Member

    Joined:
    Nov 20, 2008
    Messages:
    1,396
    Likes Received:
    418
    Location:
    Pluto
    I created a test.html file and put this in it.

    Code:
    <?php
    setcookie("test", "test1", time()+3600);
    ?>
    <html>
    <body>
    Test
    </body>
    </html>
    
    Its not working... shouldn't that set the cookie ?

    Could someone try it and tell me if it works for them ?
     
  2. PostWhore

    PostWhore Registered Member

    Joined:
    Jul 22, 2008
    Messages:
    51
    Likes Received:
    2
    Try renaming your test.html to test.php
     
  3. jake3340

    jake3340 Supreme Member

    Joined:
    Nov 20, 2008
    Messages:
    1,396
    Likes Received:
    418
    Location:
    Pluto
    tried it no luck...
     
  4. PostWhore

    PostWhore Registered Member

    Joined:
    Jul 22, 2008
    Messages:
    51
    Likes Received:
    2
    Can you provide a link to the file that supposed to drop a cookie?
     
  5. jake3340

    jake3340 Supreme Member

    Joined:
    Nov 20, 2008
    Messages:
    1,396
    Likes Received:
    418
    Location:
    Pluto
    Put that code in an html file running php on the server. Thats the whole file.
     
  6. PostWhore

    PostWhore Registered Member

    Joined:
    Jul 22, 2008
    Messages:
    51
    Likes Received:
    2
    It has to be a php file.
     
  7. blacknight9

    blacknight9 Newbie

    Joined:
    Jan 1, 2009
    Messages:
    44
    Likes Received:
    37
    Home Page:
    Since you put php code in an html file, I assume you're not familiar with web programming :eek:

    First you will need a web server to run php file. You'll need this to save you a lot of pain

    http://sourceforge.net/projects/xampp/

    Download and setup your web server, then put your code in an php file and put that file in your webroot, the default path is

    Code:
    yourxamppdirectory/htdocs/
    Code:
    <?php
    setcookie("test", "test1", time()+3600);
    ?>
    <html>
    <body>
    Cookie value: <?php echo $_COOKIE["test"]; ?>
    </body>
    </html>
    
    You can access the php file from the default location

    Code:
    http://localhost/yourphpfile.php
    The out put should be
    Code:
    Cookie value: test
    Again, these steps are very basic but since you're putting php code in an html, I feel it is necessary to go into details.
     
  8. showboytridin

    showboytridin Regular Member

    Joined:
    Sep 5, 2009
    Messages:
    348
    Likes Received:
    716
    Location:
    127.0.0.1
    It can be whatever you want. You can configure your server to run .html files like .php files.
     
  9. jake3340

    jake3340 Supreme Member

    Joined:
    Nov 20, 2008
    Messages:
    1,396
    Likes Received:
    418
    Location:
    Pluto
    It doesn't need to be a php file, and yes it is running on a server that allows PHP. Try that code guys its good but its not working.
     
  10. showboytridin

    showboytridin Regular Member

    Joined:
    Sep 5, 2009
    Messages:
    348
    Likes Received:
    716
    Location:
    127.0.0.1
    Just tried. It 's working!
     
  11. rodmansmith32

    rodmansmith32 Newbie

    Joined:
    Sep 28, 2008
    Messages:
    12
    Likes Received:
    4
    You can change parameters on server to able php in html files.

    BTW Im tried and working for me :)
     
  12. blacknight9

    blacknight9 Newbie

    Joined:
    Jan 1, 2009
    Messages:
    44
    Likes Received:
    37
    Home Page:
    Is your browser accepting cookies?
     
  13. jake3340

    jake3340 Supreme Member

    Joined:
    Nov 20, 2008
    Messages:
    1,396
    Likes Received:
    418
    Location:
    Pluto
    My server allows php in html files, echo tags and other scripts work perfect. Its just that this script is not dropping any cookies, I tried my hosting and wamp still nothing. And IE is not showing any cookies even tho i set it to accept all cookies.
     
  14. showboytridin

    showboytridin Regular Member

    Joined:
    Sep 5, 2009
    Messages:
    348
    Likes Received:
    716
    Location:
    127.0.0.1
    Do you have the lnk? Try with Firefox.
     
  15. jake3340

    jake3340 Supreme Member

    Joined:
    Nov 20, 2008
    Messages:
    1,396
    Likes Received:
    418
    Location:
    Pluto
    Oh damn, it seems to be working now... strange, thanks anyway guys for your help I appreciate it :)