16-cookies/index.php

Go to the documentation of this file.
00001 <?php
00011 define('COOKIE_LIFE', 3600);
00012 
00014 $submit1=get_post('submit1');
00015 
00017 $submit2=get_post('submit2');
00018 
00020 $cookie_name=get_post('cookie_name');
00021 
00023 $cookie_value=get_post('cookie_value');
00024 
00026 $cookie_list=get_post('cookie_list');
00027 
00028 
00038 function get_post($var1)
00039 {
00040     if(isset($_POST[$var1]))
00041         return $_POST[$var1];
00042     else
00043         return "";
00044 }
00045 
00046 
00055 function show_cookies_page($message)
00056 {
00057     global $submit1;
00058     global $cookie_name;
00059     global $cookie_value;
00060     ?>
00061         <html>
00062             <head>
00063                 <title>Cookies example</title>
00064             </head>
00065             <body>
00066                 <?php echo htmlspecialchars($message); ?>
00067                 <form action="index.php" method="POST">
00068                     Add cookie: <br/>
00069                     Name : <input type="text" name="cookie_name" /> <br/>
00070                     Value : <input type="text" name="cookie_value" /> <br/>
00071                     <input type="submit" value="Add" name="submit1" /> <br/>
00072                     <br/>
00073                     
00074                     Following cookies are already stored: <br/>
00075                     <select name="cookie_list[]" multiple="yes">
00076                         <?php
00077                             foreach($_COOKIE as $key1 => $value1)
00078                             {
00079                                 echo "<option value='$key1'>$key1 -> $value1</option>\n";
00080                             }
00081                             if($submit1=='Add' && $cookie_name!="" && $cookie_value!="")
00082                                 echo "<option value='$cookie_name'>$cookie_name -> $cookie_value </option>\n";
00083                                 
00084                         ?>
00085                     </select> <br/>
00086                     <input type="submit" value="Delete" name="submit2" /> <br/>
00087                 </form>
00088             </body>
00089         </html>
00090     <?php
00091 }
00092 
00093 
00100 function main()
00101 {
00102     global $submit1;
00103     global $submit2;
00104     global $cookie_name;
00105     global $cookie_value;
00106     global $cookie_list;
00107     $message='';
00108 
00109     if($submit1=='Add')
00110     {
00111         //if 'Add' button was pressed to submit page.
00112         if($cookie_name!="" and $cookie_value!="")
00113             setcookie($cookie_name, $cookie_value, time()+COOKIE_LIFE);
00114         $message="Cookie $cookie_name added successfully.<br/>";
00115     }
00116     else if($submit2=='Delete')
00117     {
00118         if($cookie_list!="")
00119         {
00120             //if 'Delete button was pressed to submit page
00121             foreach($cookie_list as $key1 => $value1)
00122                 setcookie($value1, "", time() - COOKIE_LIFE); //Any negative value
00123                                                               //woud do
00124         }
00125     }
00126 
00127     show_cookies_page($message);
00128 }
00129 
00130 main();
00131 ?>

Generated on Fri Nov 4 14:16:54 2011 for PHP example documentation by  doxygen 1.4.7