Set cookies in php cgi environment without using php api

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

-1


source to share


1 answer


Ok, do you want to set up cookies with php in a CGI environment?

Copy the code and name it what.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 required. Or you run into trouble :) Now let's set the cookie:

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

      



Sets the Cookie named foo to a string of values. It will expire in 60 seconds.

You can now 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 then set max-age to zero

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

      

0


source







All Articles