Cookies in PHP CPTE 212 4/7/2015 John Beckett
Two Types of Cookies A cookie is data saved on the client computer Temporary – saved in RAM in the workstation –Vanish when the browser is exited Persistent – saved on the hard drive of the workstation –Made persistent by giving them a date in the future –Deleted by giving them a date in the past
Names and Values Names may include [], which means you can have an array of arrays (2-dimensional array) in PHP but work with the single-dimensional array structure of cookies Cookies may not include certain characters such as a semicolon, but PHP auto-translates so you don’t see this limitation
Creating Cookies setcookie(name,value,expires,path,domain,secure) setcookie() must appear before anything else in your PHP code including spaces. Name and Value are required Other parameters are optional –Expires makes a cookie persistent –Path allows access by scripts in other directories –Domain allows other hosts to access –Secure requires SSL/https
$_COOKIE[] autoglobal Fetches a copy of all available cookies before any are set If you programmatically put a value into this array, it doesn’t go into the actual cookie but only into the autoglobal