Viewers: in countries Watching now:
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
In this chapter, we will learn to work with cookies and sessions. From the two previous chapters, we learned to create basic web pages and web forms. Otherwise known as GET and POST requests. But, there's a third way that we can get data from our users, and that's from their browser cookies. You're probably already familiar with browser cookies. They're small bits of data that a website asks your browswer to keep around. Cookies are important because they give us, as web developers, the ability to store user's state. To remember who the user is and what they were doing. Without cookies, web servers don't recognize when multiple page requests come from the same user.
You may be thinking well, what about their IP address, isn't that the same? Well, sure, each request probably does come from the same IP. But more than one person can share an IP address. In fact, it's very common on wireless networks. And it's also just as possible that a single user can change IP addresses between requests. Imagine that they're on a mobile phone traveling between cell phone towers. Browser cookies provide a point of consistency so that servers can look at the cookie and recognize that. That browser that's making this current request is the same one that made the three previous requests. The process starts after a user sends a request to a web server. This is an example request.
Web servers can't set or read cookies until a request comes in. And then, if the server wants to send back a cookie to the browser, it does it by sending a Set-Cookie command. And the data that it wants to save along with the rest of the page response. In fact, it sends it in the Page Header, and we talked about Headers back in Chapter 10. You can see here where Set-Cookie is just another line in the header that's being sent back with the response. At that point, the browser stores the cookie on the local computer. It keeps it. And then, whenever a browser makes another request to the website in the future.
Well, it sends all of the cookies that it had stored for that website. And it sends those in the request header, which we haven't actually talked about. It works pretty much the same way as the response header does. It's just incoming instead of outgoing. So, cookies are always going to piggyback on the regular request response cycle. That's an important point. You can't deal with cookies, you can't set a cookie or get a cookie if there's not a request coming in or response going back. Because they go in the headers of those requests and responses. There's no way to set or receive a cookie without doing one of those two things.
Now, with get and post requests, we saw that PHP automatically took all the query parameters. And put them in associative arrays assigned to the superglobals get and post. As you'd expect, PHP takes the cookie values and also puts them in a superglobal called cookie. Note that it's singular and not plural. And we can retrieve cookie value exactly like we did with our other super globals. Remember, it's just an associative array. So, we'll find the things inside the cookie by asking for the key that we want. We'll see how to retrieve values from the cookie in just a minute.
First, I think it's best if we learned how to set values so that we'll have something to read. And we'll do that in the next movie.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.