From the course: Learning PHP

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Maintaining state with cookies

Maintaining state with cookies - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

Maintaining state with cookies

- [Instructor] Cookies are a great way for PHP to remember the state of a user's browser. This can be anything for any information like their name or if they're logged in. See by default, PHP doesn't maintain state. so if you move from page to page, unless you do something to explicitly remember what a user did on the last page, PHP will not do that automatically for you and cookies persist as long as the browser stays open. If you've ever had a website remember some setting, like being logged in, that website was likely using a cookie. To set a cookie in PHP, you would use the function set cookie. This accepts a name, a value and an optional expiration. So let's create a cookie that tells us if the user has visited the site before. We'll set this to true and we won't set an expiration date. So we'll just let it stay as long as the browser stays open. To check that cookie, we would use the cookie super global array. So…

Contents