common\Cookie::set PHP Method

set() public static method

Stores a value in a cookie, by default the cookie will expire in one day.
public static set ( string $key, mixed $value, integer $time = 2592000, string $path = '/', string $domain = null, boolean $secure = null, boolean $httpOnly = true ) : boolean
$key string A name for the cookie.
$value mixed The value to be stored. Keep in mind that they will be serialized.
$time integer The number of seconds that this cookie will be available, 30 days is the default.
$path string The path on the server in which the cookie will be available. Use / for the entire domain, /foo if you just want it to be available in /foo.
$domain string The domain that the cookie is available on. Use .example.com to make it available on all subdomains of example.com.
$secure boolean Should the cookie be transmitted over a HTTPS-connection? If true, make sure you use a secure connection, otherwise the cookie won't be set.
$httpOnly boolean Should the cookie only be available through HTTP-protocol? If true, the cookie can't be accessed by Javascript, ...
return boolean If set with success, returns true otherwise false.
    public static function set($key, $value, $time = 2592000, $path = '/', $domain = null, $secure = null, $httpOnly = true)
    {
        // redefine
        $key = (string) $key;
        $value = serialize($value);
        $time = time() + (int) $time;
        $path = (string) $path;
        $httpOnly = (bool) $httpOnly;
        // when the domain isn't passed and the url-object is available we can set the cookies for all subdomains
        if ($domain === null && FrontendModel::getContainer()->has('request')) {
            $domain = '.' . FrontendModel::getContainer()->get('request')->getHost();
        }
        // when the secure-parameter isn't set
        if ($secure === null) {
            /*
            detect if we are using HTTPS, this wil only work in Apache, if you are using nginx you should add the
            code below into your config:
                ssl on;
               fastcgi_param HTTPS on;
            
            for lighttpd you should add:
                setenv.add-environment = ("HTTPS" => "on")
            */
            $secure = isset($_SERVER['HTTPS']) && mb_strtolower($_SERVER['HTTPS']) == 'on';
        }
        // set cookie
        $cookie = setcookie($key, $value, $time, $path, $domain, $secure, $httpOnly);
        // problem occurred
        return $cookie === false ? false : true;
    }

Usage Example

 /**
  * Execute the order save
  */
 public function execute()
 {
     parent::execute();
     // get order values
     $this->orderValues['product_id'] = \SpoonFilter::getPostValue('productId', null, '');
     $this->orderValues['amount'] = \SpoonFilter::getPostValue('productAmount', null, '');
     $action = \SpoonFilter::getPostValue('action', null, '');
     // get cookie
     $cookieOrderId = Cookie::get('order_id');
     // check if cookies are enabled
     $cookiesEnabled = Cookie::set('enabled', 'true');
     $cookieExists = Cookie::exists('enabled');
     // check if cookies are set, when true update the order
     if (isset($cookieOrderId) && FrontendCatalogModel::existsOrder($cookieOrderId) == true) {
         $this->orderValues['order_id'] = $cookieOrderId;
         // action add or update
         if ($action == 'add-update') {
             if (FrontendCatalogModel::existsOrderValue($this->orderValues['product_id'], $this->orderValues['order_id']) == true) {
                 // update the order values
                 FrontendCatalogModel::updateOrderValue($this->orderValues, $this->orderValues['order_id'], $this->orderValues['product_id']);
                 $this->output(self::OK, null, 'Order values updated.');
             } else {
                 // insert order values
                 FrontendCatalogModel::insertOrderValue($this->orderValues);
                 $this->output(self::OK, null, 'Order values inserted.');
             }
         } elseif ($action == 'delete') {
             if (FrontendCatalogModel::existsOrderValue($this->orderValues['product_id'], $this->orderValues['order_id']) == true) {
                 // delete the order values
                 FrontendCatalogModel::deleteOrderValue($this->orderValues['order_id'], $this->orderValues['product_id']);
                 $this->output(self::OK, null, 'Order values deleted.');
             }
         }
     } else {
         // when no cookies are set, create new cookie and insert order
         $orderId = FrontendCatalogModel::insertOrder();
         if ($orderId != '') {
             // set order id
             $this->orderValues['order_id'] = $orderId;
             // set cookie
             Cookie::set('order_id', $orderId);
             // insert order values
             FrontendCatalogModel::insertOrderValue($this->orderValues);
             $this->output(self::OK, null, 'Order imported.');
         }
     }
 }
All Usage Examples Of common\Cookie::set