Goals The ultimate goal is to create a PHP login script with the capability of remembering logged-in users. I also hope this tutorial will serve as a way to introduce people to user sessions and cookies in PHP. Notes Although this tutorial uses a MySQL database for storing user information, it has been written so that the data accessing code is separated from the main code through specific functions, so it would be easy to instead use a flat file database system, simply by changing the code in those specific functions, without messing with the rest.
This tutorial uses the latest and greatest of PHP 4, which means super globals are used, such as $_POST, $_SESSION, etc.. This tutorial will aim to teach you about sessions and cookies through example, however if you need to know more information, go to the official website . Database For those of you planning on using a flat file system, you can skip this section. For the rest of us, we want to create a MySQL database table that holds user information, here it is: CREATE TABLE users ( sername varchar(30), password varchar(32)); Of course this table can be modified according to your needs, however the password field must not be less than 32 because it has to store the md5 encrypted versions of passwords which are 32 bytes. database. php This file will contain the code that connects you to your MySQL database and the functions that access user information, you need to modify this to show your MySQL username, password and database. Allow Users to Sign-Up
Before we can login users, we need users. Here we will focus on the code that allows users to sign-up, registering their username and password. register. php Registered! Thank you , your information has been added to the database, you may now log in. Registration Failed Were sorry, but an error has occurred and your registration for the username , could not be completed. Please try again at a later time. Registration Page Registration Page Register Login Username: Password: Remember me next time Join