reading cookies from PHP

Joshua Kugler jkugler at bigfoot.com
Mon Dec 10 19:22:38 EST 2007


Jack Holt wrote:
> Hello there.
> I'm a PHP fan but a Python newbie. I wrote anapplication in Python
> that needs to read a cookie setup from a PHP page. Is itpossible to do it?
> 
> If not, what if I create a TXT file - as well as a cookie - thatcontains
> the cookie's data? Will python be able to open the file and readthe
> data? The TXT file can contain:
> 
> Username = dude
> Password = python

Cookies are sent browser by the web server, and sent back to the web server
by the browser.  So, yes, you can set the cookie with the PHP script and
read it with the python program.

I do it like this:

import Cookie
myCookies = Cookie.SimpleCookie()

if 'HTTP_COOKIE' in os.environ:
    myCookies.load(os.environ['HTTP_COOKIE'])

After that, the cookies will be available via a dictionary style lookup:

user_name = myCookies['user_name']

j






More information about the Python-list mailing list