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.
We're almost done with the user authentication portion of our CMS. We now have a login page. We're processing login attempts. We're marking admins as being logged in and we're confirming that admins have that mark stored in the session before we give them back protected pages. There's just one last thing that we need to take care of which is allowing admins to log out. To do that we're going to create a new page. Instead of login.php it's going to be logout.php. I'm just going to go to Text Mate and open a brand new page with nothing in it.
Let's save that in Sites, in Widget Corp, in Public and we're going to save it as logout.php. Now to begin with, I'm going to go ahead and add the session.php at the top and functions.php so that we can work with our session. Cause that's what we're going to need to do to logout is change those marks that are stored in the session. And then function is because I'm going to use redirect and that redirect is something that's stored in the function. So the steps that we're going to want to follow for this. I'm going to show you two versions. The first one I'm going to call v1, simple logout. And most times this is probably what you're going to want to do. You'll want to make sure that you have your session started. We've already doing that up above.
But I'm just going to put a little note here, just to remind us that we need to have our session started that takes place here. And then, once we do that, then session and admin_id is where we store that. That was the mark, we want to wash it away. Right? We want to take away their hand stamp. And we can do that by setting it to null. That's it. That's also at the same time, let's go ahead and set their username that we stored. We don't want to keep that around, either. We'll set that to null, too. So now, both of those are being set to null.
And the only thing left to do is redirect to login.php. So that's it. That's all the page does. It comes here. It pulls up the session. Erases their hand stamp. And then sends them back to the login page. You could also have to send them to the homepage or something else, whatever suits your needs. The login page is often the place that it redirects to. And I'm going to show you another version of this as well and I"m going to call this the version two. This one assumes that there's nothing else in the session that needs to be kept, and it's a little bit more heavy handed.
because we're going to completely destroy the session. So we're still going to have this session start at the top. Then we're going to tell the session that it should be set to an empty array that removes anything that was in it. So that essentially clears all values. Then we're going to check to see if the cookie for that session name is there. If they have that cookie then we're going to set the cookie with that same session name, equal to nothing and give it a time in the past so that it should be expired. Now remember that cookie is the cookie that's actually here in Firefox.
Show cookies under localhost. There it is, PHP session ID. That's the link between the browser and the session file that sits on the server. So we're telling it to get rid of that cookie. Then right after that we also call session_destroy which destroys the session file on the server. So this is a two part process. We're getting rid of the cookie, and we're getting rid of the session file, and then redirecting to login.php. Now, again, like I said, this is pretty heavy-handed. I'm going to comment it all out, so that it's not actually taking place. I'm going to do the simple version and use that instead.
I just wanted you to see both. If you want to completely eradicate the session, this is how you do it. Alright, so let's save it. And then let's try that out. So I'm going to come over here. I'm on admin.php, make sure you're logged in if you aren't already. And I have logout here, which I've already had made as a link pointing to logout.php. I did that a long time ago. So let's click Logout and lookie there. I'm on the login page. Let's try and just go to admin.php and make sure we're not logged in. Yep. It sent me back to login page. Now we'll go back and log back in, Kevin Skogland, Secret And now I'm logged back in.
I can surf around, I can see different pages, go back to the main menu, manage admin users. Oops, I don't have a back to menu page. Here, let's just do that real quick. Manage Admins and let me open up Manage Content. Here's my Main Menu link, right after the navigation starts Let's add that. I don't need my   anymore. So that's going to be Manage Admins now. There it is, Main menu. And then I can edit, cancel, move around but Main menu will always be on this page to take me back.
And then when I'm finally done, logout, and then I don't have access to those pages anymore. manage_admins.php, nope, won't let me have access. So now that we have our logout page working that completes our user authentication. And now that our user authentication is complete, our content management is complete as well. If we go back to our original blueprint and we look, We have our public area done. People can go to index.php and they can see the navigation, they can browse the page content. They're seeing only the things that we want them to see, the things that have been made visible.
And it's read only. And then on the admin side, you can log in with the username and password. And once you do, you have access to the admin menu, which allows you to manage content, manage admins and then to logout. And we can do all of those: create, read, update, and delete for the content in the admins, in our admins area. Now admittedly, this is still a very simple content management system. But it does contain some fundamental principles that you can continue to build on and add features on your own.
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.