<?php use Comodojo\Cookies\Cookie; use Comodojo\Cookies\SecureCookie; use Comodojo\Cookies\CookieManager; use Comodojo\Exception\CookieException; require __DIR__ . '/../../vendor/autoload.php'; ob_start(); $manager = new CookieManager(); $manager->register(Cookie::create('cookie_1', array('value' => 'cookie-1'))); $manager->register(SecureCookie::create('cookie_2', "thisismyverycomplexpassword", array('value' => 'cookie-2'))); $result = $manager->save(); ob_end_clean(); echo json_encode(array("manager" => $result));
/** * @expectedException Comodojo\Exception\CookieException */ public function testRetrieve() { $cookie = \Comodojo\Cookies\SecureCookie::retrieve('test_cookie', 'test_key'); }
<?php use Comodojo\Cookies\SecureCookie; use Comodojo\Exception\CookieException; require __DIR__ . '/../../vendor/autoload.php'; ob_start(); // create cookie with class constructor $cookies = array(); $cookie_1 = new SecureCookie('cookie_1', "thisismyverycomplexpassword"); $cookies['cookie_1'] = $cookie_1->load()->getValue(); // create cookie with static method create $cookies['cookie_2'] = SecureCookie::retrieve('cookie_2', "thisismyverycomplexpassword")->getValue(); try { $cookies['cookie_3'] = SecureCookie::retrieve('cookie_3', "thisismyverycomplexpassword")->getValue(); } catch (CookieException $ce) { $cookies['cookie_3'] = $ce->getMessage(); } $cookies['cookie_4'] = SecureCookie::retrieve('cookie_4', "thisismyverycomplexpassword")->getValue(); try { $cookies['cookie_5'] = SecureCookie::retrieve('cookie_5', "thisismyverycomplexpassword")->getValue(); } catch (CookieException $ce) { $cookies['cookie_5'] = $ce->getMessage(); } ob_end_clean(); echo json_encode($cookies);
/** * Static method to get a cookie quickly * * @param string $name The cookie name * * @param string $key * * @return \Comodojo\Cookies\SecureCookie * * @throws \Comodojo\Exception\CookieException */ public static function retrieve($name, $key) { try { $cookie = new SecureCookie($name, $key); $return = $cookie->load(); } catch (CookieException $ce) { throw $ce; } return $return; }
<?php use Comodojo\Cookies\Cookie; use Comodojo\Cookies\SecureCookie; use Comodojo\Cookies\CookieManager; use Comodojo\Exception\CookieException; require __DIR__ . '/../../vendor/autoload.php'; ob_start(); $manager = new CookieManager(); $manager->register(Cookie::create('cookie_1')); $manager->register(SecureCookie::create('cookie_2', "thisismyverycomplexpassword")); $result = $manager->load()->getValues(); ob_end_clean(); echo json_encode($result);
<?php use Comodojo\Cookies\SecureCookie; require __DIR__ . '/../../vendor/autoload.php'; ob_start(); // create cookie with class constructor $cookies = array(); $cookie_1 = new SecureCookie('cookie_1', "thisismyverycomplexpassword"); $cookies['cookie_1'] = $cookie_1->setValue("cookie-1")->save(); // create cookie with static method create $cookies['cookie_2'] = SecureCookie::create('cookie_2', "thisismyverycomplexpassword", array('value' => 'cookie-2'))->save(); // delete a cookie (should return a CookieException when retrieved) $cookie_3 = new SecureCookie('cookie_3', "thisismyverycomplexpassword"); $cookies['cookie_3'] = $cookie_3->setValue('bla')->delete(); // set a cookie expiration timestamp $cookie_4 = new SecureCookie('cookie_4', "thisismyverycomplexpassword"); $time = time(); $cookies['cookie_4'] = $cookie_4->setValue("expiry time: " . $time)->setExpire($time + 3600)->save(); // set a cookie for another domain (should return a CookieException when retrieved) $cookie_5 = new SecureCookie('cookie_5', "thisismyverycomplexpassword"); $time = time(); $cookies['cookie_5'] = $cookie_5->setValue("cookie for www.example.com")->setDomain("www.example.com")->save(); ob_end_clean(); echo json_encode($cookies);