PDF='MAIN' { ATTRIBUTES='*' { PLATFORM='php.txt'; REPTYPE='1'; ATTACHED='0'; PROJECT='36'; OBJECTNAME='cookies.php'; LIBLOBJ='*FILES'; TITLE='Example -Using cookies PHP'; WEBPATH='http://esdi.excelsystems.com/webtest/'; TARGETPATH='/www/zendcore/htdocs/examples/'; FTPSITE='examples'; SET='149'; VERSION='4.00'; CRTBNUM='Revision 1811'; CRTTEMPLT='C:\\Program Files\\ESDI\\WebSmart\\Temp\\PHP-A Simple Page.tpl'; SAVBNUM='Revision 1811'; LASTLOC='1'; VHTML='N'; LASTSAVE='12/21/2007 14:40:59'; OBJLIBRARY='WEBTEST'; SOURCELIBR='WEBTEST'; SOURCEFILE='QRPGLESRC'; } FTPREFINFO='*' { URL='192.168.0.100'; INITIALDIR='/www/zendcore/htdocs/examples/'; FTPPORT='21'; PASSIVEMODE='1'; WEBPATH='http://esdi.excelsystems.com/wsexmpphp/'; } PANELS='*' { PANEL='MainSeg' { DESC='Main Segment'; DETAILS=' Example 144: Using cookies (PHP)

Example 144: Using cookies (PHP)

Description:   This example demonstrates how to set, retrieve and destroy a cookie using WebSmart PHP



Enter a value for the cookie

Retrieve the value of the cookie       Click here       $cookie_msg
      
Clear the value of the cookie       Click here





  1. Start with a new Simple page template from the PHP templates




  2. Create some global variables at the top of the PHP source code
    global $cookie1, $cookie_msg;                      
                              
  3. Replace:
        // Retrieve the task (default to "default")
    if ($pf_task == \'default\')
    	generic();
                              
    with the following:
    // run the specified task
    switch($pf_task)
    {
    	case \'default\':
    	generic();
    	break;
    	
    	case \'cookie_value\':
    	cookie_value();
    	break;
    	
    	case \'retrieve\':
    	retrieve();
    	break;    
    	
    	case \'clear\':
    	clear();
    	break;
    }
                              
  4. Copy the cookie_value function and paste it below the generic function
    function cookie_value()
    {
    	// Make all global variables available here
    	foreach($GLOBALS as $arraykey=>$arrayvalue) 
    	{
    		if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\')
    			global $$arraykey;
    	}
    	//get value from the form
    	$cookie1 = $_POST["cookie1"];
    	//set the cookie to value from the form. This cookie will expire in one hour.
    	setcookie("cookie1",$cookie1, time()+3600);
    	//write the MainSeg segment
    	wrtseg(\'MainSeg\');
    }
                              
  5. Copy the retrieve function and paste it below the generic function.
    function retrieve()
    {
    	// Make all global variables available here
    	foreach($GLOBALS as $arraykey=>$arrayvalue) 
    	{
    		if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\')
    			global $$arraykey;
    	}
    	//if we have a cookie set with a value display a message
    	
    	
    	if(isset($_COOKIE[\'cookie1\']))
    	{ 
    	
    			$msg = "You set\\"<strong>";
    			$msg2 = "</strong>\\" as the cookie contents";
    			$cookie_msg = $msg . $_COOKIE["cookie1"] . $msg2;
    	
    	}
    	else
    	{
    		$msg = "";
    		$msg2 = "";
    		$cookie_msg = "<strong><span style=\\"color:red;\\">
    		               Please set the cookie with a value</span></strong>";
    	}
    	
    	wrtseg(\'MainSeg\');
    	
    }
                              
  6. Now add the clear function right after the retrieve function.
    function clear()
    {
    	// Make all global variables available here
    	foreach($GLOBALS as $arraykey=>$arrayvalue) 
    	{
    		if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\')
    			global $$arraykey;
    	}
           //this will set the cookie to a time in the past and it will be destroyed
    	setcookie("cookie1","",time() - 3600);
    	
    	wrtseg(\'MainSeg\');
    	
    }
                              
  7. In the HTML MainSeg segment create a table and form to set and retrieve the cookie value
    <table>
       <tr>
         <td><strong>Enter a value for the cookie</strong></td>
       </tr>
       <tr>
        <td>
          <form action="cookies.php" method="POST">
          <input type="hidden" name="task" value="cookie_value">
          <input type="text" size="30" name="cookie1" value="$cookie1">
          <input type="submit" value="Set Cookie">
          </form>
        </td>
       </tr>
    </table>
       <br>
    <table>
       <tr>
          <td><strong>Retrieve the value of the cookie</strong</td>
          <td><a href="$pf_scriptname?task=retrieve">Click here</a></td>
          <td>$cookie_msg</td>
          </tr>
        <tr><td colspan="3"><td> </tr>
        <tr>
          <td><strong>Clear the value of the cookie</strong></td>
          <td><a href="$pf_scriptname?task=clear">Click here</a></td>
          <td></td>
        </tr>
    </table>
                               
  8. Compile and run your program

Program Definition:   cookies.phw

Rate This Example

Did this knowledge base article help you to achieve your goal?  Yes  No  Don\'t Know

Enter additional comments below.   If you want to hear back from us, include your contact information.

'; } } PREVIEWS='*' { DEFAULT='MainSeg'; PREVIEW='MainSeg' { SEG='MainSeg'; } } ACTIONS=' // Program Name: cookies.php // Program Title: Example -Using cookies PHP // Created by: SPOT // Template family: Idaho // Template name: Simple Page // Purpose: // Program Modifications: require(\'/esdi/websmart/v6.6/include/xl_functions001.php\'); global $wsnum, $cookie1, $cookie_msg; // This code is for the Rate this page section if (isset($_REQUEST[\'wsnum\'])) { $wsnum = $_REQUEST[\'wsnum\']; } // Check the cookie if(isset($_COOKIE[\'wsnumbc\'])) { $wsnum = $_COOKIE[\'wsnumbc\']; } else { setcookie(\'wsnumbc\', $wsnum); } //cookie for the example $cookie1 = ""; // run the specified task switch($pf_task) { case \'default\': generic(); break; case \'cookie_value\': cookie_value(); break; case \'retrieve\': retrieve(); break; case \'clear\': clear(); break; } function generic() { wrtseg(\'MainSeg\'); } function cookie_value() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\') global $$arraykey; } //get value from the form $cookie1 = $_POST["cookie1"]; //set the cookie to value from the form setcookie("cookie1",$cookie1, time()+3600); //write the MainSeg segment wrtseg(\'MainSeg\'); } function retrieve() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\') global $$arraykey; } //test to see if we have a cookie set with a value if(isset($_COOKIE[\'cookie1\'])) { //set a message with the value of our cookie $msg = "You set    \\""; $msg2 = "\\"    as the cookie contents"; $cookie_msg = $msg . $_COOKIE["cookie1"] . $msg2; } //if we don\'t have a value for our cookie else { $msg = ""; $msg2 = ""; $cookie_msg = "Please set the cookie with a value"; } wrtseg(\'MainSeg\'); } function clear() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\') global $$arraykey; } //this will set the cookie to a time in the past and it will be destroyed setcookie("cookie1","",time() - 3600); wrtseg(\'MainSeg\'); }'; }