P2P | IT Discussions Forum

Full Version: PHP/MySQL Login System
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In this tutorial I will explain how you can make your own PHP Login Script using a MySQL database. This includes a register, login, and member page.

1. First you need to create the database and to store your users information. If you have already done so then you should skip to step two. To create the database you may have to use a MySQL admin administrater because some host's don't always support it being done from a script. So create a database if you haven't already done so..

2. Now that we have a database or if we already had one then we need to setup the table and columns to store our users information. So you can use this code to setup the columns and you can fine tune it to customize your site.
Lets break this code down.
CREATE TABLE users - makes a table called users in the database.
id Int NOT NULL Auto_INCREMENT - Makes an int column called "id" that will increase by 1 for each entry.
PRIMARY KEY(id) - Makes "id" primary, so it is used as an index.
username VARCHAR(30) - Makes a column called "username" that is VARCHAR which can hold almost any character. The (30) means it can't be any larger than 30.
password VARCHAR(20) - Makes a column called "password" that is also VARCHAR and its maximum length is 20.
email VARCHAR(40)- Makes a column called "email" which is also VARCHAR but its maximum length is 40.
mysql_query($query) - Run the MySQL query to create the table.
echo "Table Created!"; - Print "Table Created!" on the screen.

3. Now we have our database setup, on to the next part, register,login, and index. I will start with a simple register page for the users to insert their username, password, and email.
Heres register.php,
Lets review this code.
$username = $_POST["username"]; - Get the POST variables and register them with local variables. The two variables below it do the same.
if($username==NULL|$password==NULL|$cpassword==NUL L|$email==NULL) - Check if any of the fields were left blank. This is the beginning of our chain of "if" statements.
if($password!=$cpassword) { - If the password is the same as confirm password.
mysql_query("SELECT username FROM users WHERE username='$username'"); - Check if the username is already in use, it also does the same for the email.
mysql_num_rows($checkuser) - Turns the result into a number which means how many rows it selected, $checkemail also does this.
if ($email_exist>0|$username_exist>0) { Check if the email or username is in use.
The rest is inserting the values and the HTML design.

4. Now that we have our register page and the database set up we can test out our register page. If you have a different name for the columns then what I had at the top then you will have to go through the scripts changing the MySQL queries. Otherwise when you register it should return 'The user $username has been successfully registered.'

Source : http://www.AquireKnowledge.com
hhmmm...nice info mateSmile
Reference URL's