Main Content

Using cookies and sessions to connect different URLs - PHP

Archive - Originally posted on "The Horse's Mouth" - 2008-05-18 21:42:02 - Graham Ellis

One of the great beauties of cookies (and derived from that, of PHP sessions) is that they can be used across a series of pages on the same server - indeed we use cookies on this site to record our visitor's preferences for colour, font size, country and open source langauge(s) via our user preference page, and we then use those inputs to apply to all pages and styles on the site.

We're a simple session demonstration showing this in a module from our new PHP Techniques Workshop that we ran for the first time last week - the source code of the first page is here and the source of a second page is here.

Run the first page here and each time you do so you'll step through a limeric. Run the other page hereto report on the status of the first page from a different URL.

Practical uses of this? Anything from two applications which need to share data but are maintained by different teams to applications with multiple top level pages - and even captcha technology, where the session is used to link the graphics to the page. More about that latter here and try in here