Professional Documents
Culture Documents
And after
having learned how to make a simple login, I will try to write my own tutorial, for you
the tutorial
Step 1:
The first step in designing a member system is to plan out exactly what you need. A common
impulse among programmers is to jump right in and start coding. I'll be honest and admit that I'm
guilty of this more so than anyone. However, since I'm in control of this conversation (yes!),
you'll have it all planned out by reading through this before you even see any code.
Second, at least for our purposes, you need a database. Preferably MySQL. PHP and MySQL go
hand-in-hand, so a lot of servers tend to match the two up. Thus, since we're talking PHP, we
may as well talk MySQL.
Third, you will need 4 blank PHP web pages entitled: register.php, login.php, members.php, and
logout.php. After you have these pages created and open, we're ready to start.
Step 2: Database
If we want to design a members system, we'll need a database. So all we need to do in this step is
to create the table we will use to manage the user's login information. Note that the schema we
use here is quite simple, and is only simplified to help you see how it works.
Name the table "dbUsers." It will need 4 fields:
Once you've made the database table, you're ready to design and code the registration page.
Create a new file and name it dbConfig.php. This file will contain the PHP code that will connect
to the MySQL database, and select the correct database. Make sure you have added users to your
MySQL database with read/write or admin access, then place this type of code into the
dbConfig.php file:
<?
// Replace the variable values below
// with your specific database information.
$host = "localhost";
$user = "UserName";
$pass = "Password";
$db = "dbName";
Step 3: Register
register.php
On your registration page, you need to create a web form that will allow the user to plugin a
username, password, and their e-mail address. Then, also on your page, add code that runs only
when information has been passed via the form. Finally, display a "Registration Successful!"
message to the user.
<?php
login.php
Now in PHP, first we need to check the username and password against the information stored in
the database. Since when the user registered, we encrypted their password using the MySQL
PASSWORD() function, we re-encrypt the password the user supplied in the login form and
cross-check this with the existing value in the dBase. If login information is O.K., then we need
to use sessions to store the user's ID so they can access member-only content.
<?php
session_start();
// dBase file
include "dbConfig.php";
if ($_GET["op"] == "login")
{
if (!$_POST["username"] || !$_POST["password"])
{
die("You need to provide a username and password.");
}
// Create query
$q = "SELECT * FROM `dbUsers` "
."WHERE `username`='".$_POST["username"]."' "
."AND `password`=PASSWORD('".$_POST["password"]."') "
."LIMIT 1";
// Run query
$r = mysql_query($q);
if ( $obj = @mysql_fetch_object($r) )
{
// Login good, create session variables
$_SESSION["valid_id"] = $obj->id;
$_SESSION["valid_user"] = $_POST["username"];
$_SESSION["valid_time"] = time();
members.php
Now that the user has logged in successfully, and has his id, username, and login stored in
session variables, we can start working with member-only content. A major thing to remember is
that any page you want to carry session data over to you must declare a session_start(); at the top
of your code.
<?php
session_start();
if (!$_SESSION["valid_user"])
{
// User not logged in, redirect to login page
Header("Location: login.php");
}
Step 6: Logout
logout.php
Ah, although it would be nice if our user's never left our web sites, we should give them to
opportunity to log out and destroy the session variables if they so choose. It's quite easy to do,
and you can just copy and paste this one.
<?php
session_start();
session_unset();
session_destroy();
// Logged out, return home.
Header("Location: index.php");
?>
That's about it!. I used many simple examples hoping that you will learn how the internal
systems work so you can expand on them and design a system that's just right for your needs.
Have fun!