Example 144: Using cookies (PHP)

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

Cookies are text files that a Web server can store on a user's hard disk. Cookies allow a Web site to store information (sites visited or credentials for accessing the site) on a user's machine and later retrieve it. The pieces of information are stored as name-value pairs. Cookies are designed to be readable only by the Web site that created them.



Enter a value for the cookie

      
      





  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.