Сессии в PHP. Что такое сессия PHP

This article was written in 2009 and remains one of our most popular posts. If you’re keen to learn more about PHP and MySQL, you may find this of great interest.

NOTE: This article has been newly updated to work on PHP 4.2 or later!

Recently, I had occasion to work on a small project with a group of people. We had determined early on that email alone wasn’t going to be enough to keep everyone in the loop, so I was tasked with building a small Web site for the project. It would contain a simple message board, a place where we could upload documents and other files for the rest of the team to use, and contact information for the various team members.

For many of these features to work, I knew that I’d need users to log in before accessing the relevant parts of the site. What I needed was a system that would let users register for a user ID to access to the site, then immediately use that ID without any intervention on my part.

In this article, I shall provide an overview of the system I developed, beginning in the first half with the user signup process. In the second half, I’ll focus on the site itself, how it requires users to log in and then maintains that logged-in status throughout their visit. I’ll be paying special attention to the use of the session management features in PHP. By the end, you should have all the information you need to implement a similar system of your own.

Throughout this article, I’ll be assuming that you have a basic familiarity with the PHP language, the use of forms to submit information to a PHP script, and how PHP may be used to interact with a MySQL database. If any of these are foreign concepts to you, you should begin by reading my previous article, .

Part One: The Signup Process The Signup Form

A natural place to start building a site that will require users to register for access is the registration process itself. As one would expect, a simple Web-based form will do the trick. Here’s what it will look like:

And here’s the code for this form:




New User Registration



New User Registration Form

* indicates a required field


With the objective now clear, I’ll walk you through the code for accesscontrol.php . Begin by including your two handy include files: