PHP and HTTP cookies

Jussi Pohjolainen

Tampere Polytechnic University of Applied Sciences » ICT

HTTP Cookie [source: wikipedia]

Purpose of cookies

In technical perspective

Interaction

Picture

HTTP Request

Browser support

Cookie drawbacks

Setting cookie from PHP

Examples of setting cookie with PHP

Reading and destroying the cookie

Examples

Exercises

  1. Make a simple "hello world" php - code and use setcookie-function after printing some (x)html-code. Want kind of error message do you get? Why?
  2. Move the setcookie - function to top of your php-code. Run your code and find the cookie from your browser (in Firefox: Cookie Manager)
  3. Modify this game so that the player can play the game using money. When the game starts, user has five euros. Each round costs one euro and when the the fruits are same, player wins three euros. Game notifies all the time how much money is left. When user has no money left, game asks from the user if he/she wants to start a new game. Use cookies to implement the functionalities.