Results 1 to 7 of 7

Thread: PHP - Cookies (Yumm!)

  1. #1

    Thread Starter
    Hyperactive Member flame_211's Avatar
    Join Date
    Jun 2000
    Location
    I dont really think I know where I am???
    Posts
    393

    Post PHP - Cookies (Yumm!)

    The basic idea behind cookies is very simple. The site stores a little bit of info on the users computer, which can be accessed at a later point in time. The info stored can be anything, but usually consists of the users username, password, etc. for that particular site.

    PHP Code:
    // basic syntax
    setcookie("stuff to store"$variable"expiration date");

    //example, expires 1 year after the cookie is stored
    setcookie("stuff"$var1time()+31104000);

    //to retrieve cookie
    $var1 $_COOKIE['var1']; 
    Any questions/comments, please feel free to contact me.
    Reach me at:
    AIM: FlameWide
    AIM: Itz deep6
    MSN/Email: flame_gandalf@hotmail.com

    Check out these sites:
    My Blog

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

    Re: PHP - Cookies (Yumm!)

    Its worth noting that the cookie is not available in the $_COOKIE array until the next request. Also that this cookie will be available to all PHP scripts within the domain and the URI passed as the path parameter to the setcookie() function.

    See here for more information.
    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.

  3. #3
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: PHP - Cookies (Yumm!)

    Why is it that JavaScript can set cookies when ever it wants to, but in PHP you must set cookies before sending any of the content?

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: PHP - Cookies (Yumm!)

    Quote Originally Posted by Slyke
    Why is it that JavaScript can set cookies when ever it wants to, but in PHP you must set cookies before sending any of the content?
    JavaScript is a client-side technology. PHP is a server-side technology. Cookies are a client-side object therefore JavaScript can access, add and manipulate cookies in real time because it's happening in real time. PHP, on the other hand, renders, requests and shows nothing to the browser until it sends its response. Therefore you have to setup your cookies before you send them otherwise how could they ever get set?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  5. #5
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: PHP - Cookies (Yumm!)

    Mmm, that is true, but I'm sure it's possible for PHP to make it so that it could send new cookies to the browser in real time (IE Real time means as the page is being sent, not 20 seconds after it's completed). I know that the HTTP protocol would have to be restructured and all that for this to happen, but I'm just a little unsure of why they wouldn't of designed it like that in the first place.

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: PHP - Cookies (Yumm!)

    Quote Originally Posted by Slyke
    Mmm, that is true, but I'm sure it's possible for PHP to make it so that it could send new cookies to the browser in real time (IE Real time means as the page is being sent, not 20 seconds after it's completed). I know that the HTTP protocol would have to be restructured and all that for this to happen, but I'm just a little unsure of why they wouldn't of designed it like that in the first place.
    When you say real time and when the page is being sent... well that's kind of what happens today. The cookies are sent with the page. If by "real time" you mean whenever the server decides to send cookies... then that's a bad idea. If that's the case not only are you opening a second connection to the user (otherwise you'd throw "real time" out the window and you'd have today's implementation) but the browser wouldn't have any guarantee that the cookies it's receiving came from the same site it requested data from. If you sent a cookie to the browser in "real time", what happens if the browsers requests a page slightly after your cookie? Your server won't realize the browser even has the cookie you sent so you better hope it's not important.

    Today everything is sent at once. The browsers knows for sure that the cookies were set by the website it's loading. The server knows that the cookie will exist on the next request and everyone is happy.

    I don't see why the way it's done today would be a problem anyway with any server-side of client-side technology. Cookies work fine in PHP; just don't set them after you start sending data back.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: PHP - Cookies (Yumm!)

    Cookies have to be send before any output is rendered because they are part of the page header information that needs to be send to the client browser. Once you start sending the actual page data & displaying data, you can't re-modify the header info. Therefore, the cookies must be sent ahead of the page data.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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