Setting a Cookie with PHP

You can set a cookie in a PHP script in two ways. You can use the header() function to set the Set-Cookie header. You encountered the header() function in Hour 10, “Working with Forms.” header() requires a string that is included in the header section of the server response. Because headers are sent automatically for you, header() must be called before any output is sent to the browser:

header ("Set-Cookie: vegetable=artichoke; expires=Wed, 25-Aug-04 14:39:58 GMT; path=/; ");

Although not difficult, this method of setting a cookie requires you to build a function to construct the header string. ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.