Title: Python and PHP cookies
Last Modified: 06.12.2008
I use some python cgi's and I use PHP for templating quite a bit. This is a way to make them communicate together. The main trick here is to set your path.
PHP files are run from a sub directory of my site, while my cgi files are run from the cgi-bin directory. If you just set cookies, without stating the path they will be set to the path that the file is being run from.
<?PHP
setcookie("free","bird",0,"/");
?>
Thats it make sure it is early/first, because it modifies the response headers.
Python is similar but it takes a little more setup, since python is not designed to support cookies.
import cgi,Cookie cooked = Cookie.SimpleCookie() cooked['free']='bird' cooked['free']['path']='/' print cooked
Both of these programs will print out response headers to set a cookie with variable name free and value bird. The both specifiy the root path so they refer to the same cookie.
Something I found a little bit cryptic, a python Cookie when you set a value it has the following keys.
['comment', 'domain', 'secure', 'expires', 'max-age', 'version', 'path']
Those keys are how you change the various parameters. When you these parameters you need to follow the correct format1
If you want to change the mode to secure:
cooked['free']['secure']=True
And that will append "secure" to your comment.
I found some good information http://doc.astro-wise.org/Cookie.html
I wil conclude this with two programs that play 'cookieball' with eachother. I assume this would be something similar to how you want python and php to share cookies. You can try it at
As in they pass a cookie back and forth like a ball.
<?PHP
if($_POST['username']){
setcookie('username',$_POST['username'],0,'/');
setcookie('password',$_POST['password'],0,'/');
}
echo "<h2>Your old username: ".$_COOKIE['username']."</h2>";
echo "<h2>Your old password: ".$_COOKIE['password']."</h2>";
echo "<h3>Your new username and password will become</h3>";
echo "<h2>new username: ".$_POST['username']."</h2>";
echo "<h2>new password: ".$_POST['password']."</h2>";
?>
<html>
</body>
<form method="post" action = "/cgi-bin/cookieball.cgi" name='myform'>
change username: <input name='username'/><br/>
change password: <input name='password'/><br/>
then we we'll set a cookie and everybody is happy.<br/>
<input type='submit'/>
</form>
</body>
</html>
And the corresponding python program.
#!/usr/bin/env python
"""
Passing cookies between a .cgi and php program
"""
import cgi,Cookie
import os
cookme = Cookie.SimpleCookie()
cookme.load(os.environ.get('HTTP_COOKIE',''))
print os.environ
FormData = cgi.FieldStorage()
entered_name=FormData.getvalue('username')
test = 0
#to set a cookie this has to be your first output.
if FormData.has_key('password') and FormData.has_key('username'):
cooked = Cookie.SimpleCookie()
cooked['username']=FormData.getvalue('username')
cooked['username']['path']='/'
cooked['password']=FormData.getvalue('password')
cooked['password']['path']='/'
test = 1
print cooked
#And this line is very nescessary.
print "Content-Type: text/html\n\n"
print "<h3>lets get you authenticated so we can proceed?</h3>"
if cookme.has_key('username') and cookme.has_key('password'):
print "<h2>Current username:"
print cookme.get('username').value
print "</h2>"
print "<h2>current password:"
print cookme.get('password').value
print "</h2>"
else:
print "<h3>no username/password as of yet.</h3>"
if test==1:
print "<h3>your cookies have been changed just now.</h3><br/>"
print "<h2>new username = "
print FormData.getvalue('username')
print "</h2><h2>new password = "
print FormData.getvalue('password')
print "</h2><br/>"
else:
print "no username/password were specified so it is not being changed"
print "<form method=\"post\" action = \"/cookieball/index.php\" name='myform'>"
print "username: <input name='username'/><br/>"
print "password: <input name='password'/><br/>"
print "then we we'll set a cookie and everybody is happy.<br/>"
print "<input type='submit'/>"
print "</form>"
In closing there are some subtle differences in the way the cookies are set. Python uses quotes and PHP changes spaces to +'s so So preparcing my need to take place to make sure they get change accordingly.

