Giter Club home page Giter Club logo

cookie's Introduction

Object oriented cookie handler.

This library is a simple object oriented alternative to the $_COOKIE superglobal that can be read using the same associative array style code. The Cookie class represents cookie data in immutable objects, meaning that the state of the request/response cookies cannot be accidentally changed by undisclosed areas of code.


Build status Code quality Code coverage Current version PHP.Gt/Cookie documentation

Example usage

// Create a replacement for $_COOKIE.
$cookie = new Gt\Cookie\CookieHandler($_COOKIE);

// Access values as normal.
$value = $cookie["firstVisit"];

if(isset($cookie["firstVisit"])) {
// Cookie "firstVisit" exists.
}

if($cookie->has("firstVisit")) {
// Cookie "firstVisit" exists.
}
else {
// Create a new cookie that expires in ten days.
	$now = new DateTime();
	$expire = new DateTime("+10 days");
	$cookie->set("firstVisit", $now, $expire);
}

// Now you can unset the superglobal!

What's not covered?

This library does not touch on encrypting cookies. To store sensitive information across HTTP requests, use a session variable. To ensure cookies can't be read by JavaScript, use a secure HTTP-only cookie.

cookie's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar g105b avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

isabella232

cookie's Issues

CookieList class

CookieList constructed with multiple Cookie objects.

  • with
  • without
  • has
  • get
  • getAll

Array cookies

Values of get methods are set to return strings. That's absolutely expected.

However, PHP implements the ability to store arrays as cookies like this:

setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
        $name = htmlspecialchars($name);
        $value = htmlspecialchars($value);
        echo "$name : $value <br />\n";
    }
}

This isn't currently possible using this library, but it should be compatible, possibly utilising a getArray method.

I personally don't like the idea of cookies being set using the array syntax because it means that browsers will send each element of the array as a separate cookie which just seems like a massive waste and massive hack.

Can't pass cookie value as string

There is no toString implementation, because the cookie model does not match the other objects of PHP.Gt - for example, I think the CookieStore idea in #39 will fix this.

Static classes for dealing with PSR-7

With the basics taken care of in Cookie and CookieList, use static classes for reading/writing cookies to/from PSR-7 Request/Response objects.

With/without style methods.

Function to clear all/specific cookies

Very useful for logging out.

$cookie->clear() kills everything

$cookie->clear("user", "tracking"); kills only the two specified cookies, leaving others set.

Cookie class

Cookie represents a single cookie header with name, value and optional other fields that setcookie() takes. __toString outputs the cookie header line.

Optional fields set in PSR style withX, withoutX:

  • expiry (DateTime)
  • path (string absolute path)
  • domain (string)
  • secure (bool)
  • httpOnly (bool)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.