Jump to content

cookies i dont understand 100%

- - - - -

  • Please log in to reply
5 replies to this topic

#1
alrazy1

alrazy1

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
what i dont understand is , when using cookies in a php script , that script is executed in the server side and the cookie itself is in the client browser memory,
how the server know his own cookies ?
when the user/browser request a php page from the server the browser send first all cookies that is correspond to this domain to the server "we dont see that " ,
for example : browser >> GO to www.somewebsite.com/dir1/dir2/foo.php
the browser duty is first to find if it has any cookies related to this domain and if it has it send it together with the request and it is stored by php in $_COOKIE global variable, then the script in foo.php process this cookie further more like changing or deleting it.... am i right ?
setcookie() work the opposite direction , it set the new created cookie and send it to the page in its header and the browser save it locally for local use like in javascript...am i right ?
please tell me if i understand ? thanks.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The server will request the cookie, which the browser provides. It's handled fairly transparently.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
tayolad

tayolad

    Newbie

  • Members
  • PipPip
  • 27 posts
read this if it helps: Cookies | Practical PHP Programming | TuxRadar Linux

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Client:
The browser will match domain, path and connection type (http or ssl) and if matches will send the cookie along with the request, an example HTTP request from the client browser to the server:
GET /index.html HTTP/1.1
Host: www.example.com
Cookie: name=value
Accept: */*
Note that Apache will populate PHP's $_COOKIE superglobal array with the content of the cookies sent, in this case $_COOKIE['name'] = 'value'.

Server:
When setcookie() is called, it will send an HTTP request named Set-Cookie, an example of setting the cookie and sending it to the client is as follows:
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=value
Then it is the client browsers responsibility to handle the cookie and store it. The request is dependent on parameters, for example if you use a more complex setcookie call like this:
setcookie("name", "value", time() + 3600, "/", ".example.com");
It can populate this response:
Set-Cookie: name=value; expires=Fri, 31-Jan-2011 10:00:59 GMT; path=/; domain=.example.com

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
alrazy1

alrazy1

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
hi guys thank you for the help
but alexander you say that apache will populate $_COOKIE superglobal ,
but i think apache is just a web server that transform php to html and serve the pages to the client ,
i am not sure if you are right because i think its php parser duty to populate the $__COOKIE superglobal variable..
please correct me if i am wrong...
thank you all again

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Apache will collect the cookie information sent from the client in a environment variable and forward it to PHP which populates the $_COOKIE variable based upon the forwarded information. So apache do indirect populates the $_COOKIE variable.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users