• PHP Video Tutorials

PHP - session_set_cookie_params() Function



Definition and Usage

Sessions or session handling is a way to make the data available across various pages of a web application. The session_set_cookie_params() is used to set the session cookie parameters defined in the php.ini file

Syntax

session_set_cookie_params([$array]);

Parameters

Sr.No Parameter & Description
1

array(Optional)

This an associative array which holds the values of the cookie parameters (lifetime, path, domain, secure, httponly and samesite).

Return Values

This function returns a boolean value which is TRUE on success and FALSE on failure.

PHP Version

This function was first introduced in PHP Version 4 and works in all the later versions.

Example 1

Following example demonstrates the usage of the session_set_cookie_params() function.

<html>   
   <head>
      <title>Setting up a PHP session</title>
   </head>   
   <body>
      <?php  	
         //Setting the cookie parameters
         session_set_cookie_params(30 * 60, "/", "test", );
         //Retrieving the cookie parameters
         $res = session_get_cookie_params();
         //Starting the session
         session_start();
         print_r($res);	  
      ?>
   </body>   
</html> 

One executing the above html file it will display the following message −

Array ( [lifetime] => 1800 [path] => /test [domain] => test.com [secure] => [httponly] => [samesite] => )

Example 2

This is another example of this function.

<html>   
   <head>
      <title>Setting up a PHP session</title>
   </head>   
   <body>
      <?php  
         //Retrieving the cookie parameters
         $currentCookieParams = session_get_cookie_params();
         
         //Setting the cookie parameters
         $domain = '.test.com';
         session_set_cookie_params(
            $currentCookieParams["lifetime"],
            $currentCookieParams["path"],
            $domain,
            $currentCookieParams["secure"],
            $currentCookieParams["httponly"]
         );
         //Starting the session
         session_start();
      ?>
   </body>   
</html>
php_function_reference.htm
Advertisements