Cookie Management
Required Namespace
  • Ness
    • Tool
      • Cookie

You can include this library to your code file by typing the below code;

use Ness\Tool\Cookie


About Cookie Class #top

Simple cookie management class for Ness PHP Framework with cookie encryption.


Create a Cookie #top

You can create a Cookie with just a single line of code. Crate a new Cookie class object for defining a new cookie.

                                $ck = new Ness\Tool\Cookie($name = "test_cookie", $value = "test cookie value", $expire = 3600, $path = "", $domain = "", $secure = FALSE, $httpOnly = FALSE);
Initialize a cookie
Return String; Stored cookie data
Parameters $name String; Cookie Name
$value String; Cookie Value
$expire Integer; Cookie expire as seconds
$path String; Set active path for cookie
$domain String; Set active domain for cookie
$secure Bool; True for Allow Only HTTPS
$httpsOnly Bool; When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript



Destroy a Cookie #top

Destroy a cookie if its available.

                                
    $ck = new Ness\Tool\Cookie($name = "test_cookie", $value = "test cookie value");
    $ck->Destroy();
Destroy
Return Bool
Parameters None



Get value of Cookie #top

Get's the cookie value. Returns false if cookie is not available

                                
    $ck = new Ness\Tool\Cookie($name = "test_cookie", $value = "test cookie value");
    echo $ck->Revert(); // Print Cookie value
Get Value
Return String
Parameters None



Other Methods of Cookie Class #top

setName

$ck = new Ness\Tool\Cookie();
$ck->setName($param = "cookie_name")->Store();
                        
Set a name for your cookie.
Return $this
Parameters $prm String; Cookie name



setValue

$ck = new Ness\Tool\Cookie();
$ck->setName("cookie_name")
    ->setValue($param ="test")
    ->Store();
                        
Set a value for your cookie.
Return $this
Parameters $prm String; Cookie value



setExpire

$ck = new Ness\Tool\Cookie();
$ck->setName("cookie_name")
    ->setValue($param ="test")
    ->setExpire($param = 1000); //1000 seconds
    ->Store();
                          
Set a expire as seconds for your cookie.
Return $this
Parameters $prm Integer; Cookie expire time as seconds



setPath

$ck = new Ness\Tool\Cookie();
$ck->setName("cookie_name")
    ->setValue($param ="test")
    ->setExpire($param = 1000); //1000 seconds
    ->setPath($param = "/")
    ->Store();
                           
Set active path for Cookie
Return $this
Parameters $prm String; Cookie active path



setDomain

$ck = new Ness\Tool\Cookie();
$ck->setName("cookie_name")
    ->setValue($param ="test")
    ->setExpire($param = 1000); //1000 seconds
    ->setPath($param = "/")
    ->setDomain($param = "www.yourdomain.com")
    ->Store();
                             
Set active domain for Cookie
Return $this
Parameters $prm String; Cookie active domain



setHttpsOnly

$ck = new Ness\Tool\Cookie();
$ck->setName("cookie_name")
    ->setValue($param ="test")
    ->setExpire($param = 1000); //1000 seconds
    ->setPath($param = "/")
    ->setDomain($param = "www.yourdomain.com")
    ->setHttpsOnly($param = TRUE)
    ->Store();
                              
Allow only https requests
Return $this
Parameters $prm Bool; True | False



setHttpOnly

$ck = new Ness\Tool\Cookie();
$ck->setName("cookie_name")
    ->setValue($param ="test")
    ->setExpire($param = 1000); //1000 seconds
    ->setPath($param = "/")
    ->setDomain($param = "www.yourdomain.com")
    ->setHttpsOnly($param = TRUE)
    ->setHttpOnly($param = FALSE)
    ->Store();
                              
When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript
Return $this
Parameters $prm Bool; True | False



setEncryption

$ck = new Ness\Tool\Cookie();
$ck->setName("cookie_name")
    ->setValue($param ="test")
    ->setExpire($param = 1000); //1000 seconds
    ->setPath($param = "/")
    ->setDomain($param = "www.yourdomain.com")
    ->setEncryption($isset = TRUE, $key="1425")
    ->Store();
                              
Enable or Disable cookie encryption
Return $this
Parameters $prm Bool; True | False for enabled/disabled
$key String; Encription key



Store

$ck = new Ness\Tool\Cookie();
$ck->setName("cookie_name")
    ->setValue($param ="test")
    ->setExpire($param = 1000); //1000 seconds
    ->setPath($param = "/")
    ->setDomain($param = "www.yourdomain.com")
    ->setHttpsOnly($param = TRUE)
    ->setHttpOnly($param = FALSE)
    ->Store();
                              
Store a configured Cookie
Return Void
Parameters None



DestroyAll

Cookie::DestroyAll();
                               
Destroy All Cookies
Return Bool
Parameters None



isEnabled

Cookie::isEnabled();
                                  
Returns true if cookies are enabled.
Return Bool
Parameters None