tags:

views:

357

answers:

1

How can I use cookies in a php CGI Enviroment without using any api functions from PHP?

A: 

Ok you want to setup cookies with php in a CGI Enviroment?

Copy the code and name it to whatever.cgi and make it executable

#!/usr/bin/php
function set_cookie($cookiename,$cookievalue,$cookietime){
   echo 'set-cookie: '.$cookiename.'="'.$cookievalue.'"; max-age="'.$cookietime.'";'."\n";  
}

The \n at the end of the line is a must. Or you will run into trouble :) Now let's set a cookie:

set_cookie("foo","bar",60)

Sets the Cookie with the Name foo to the Value bar. Expires in 60 seconds.

Now you can start with the HTML Header.

echo "Content-Type: text/html\n\n";
echo "<html>\n";
echo "<head>\n";
echo "<title>whatever</title>\n";
echo "</head>\n";
echo "<body>\n";

If you want to delete the cookie, set the max-age to zero

function set_cookie($cookiename){
   echo 'set-cookie: '.$cookiename.'="0"; max-age="0";'."\n";  
}
evildead