PHP Tutorials
Tutorials
PHPPHP and MySQL User Registration
PHP and MySQL User Registration
This tutorial will demonstrate how to implement a user login/membership system in your website. Though implementing login/membership system appears to be a herculean task, it is actually lot more simpler than you think. In fact, all you need is a single table in your database, and a PHP-supported Server. To begin with, here is an outline of what this tutorial will show you how to do:
- User registration
- Gather username, password, email, and store in table
- User login
Before we can begin, we need to do the backend work with MySQL. First you can create a database, and in this case, let us call it "myDB" and this name will be used in example queries and code.
First, we need to create the table. Within the table, we have a structure similar to this:
- userid (auto increment)
- username password (MD5 hash)
- email address
Now that we have the basic structure of our table, we can go ahead and run a query through either your MySQL console, or through PHPMyAdmin. The query for creating myDB table is given below:
- CREATE TABLE `myDB`.`usersystem` (
- `userid` INT NOT NULL AUTO_INCREMENT ,
- `username` VARCHAR( 50 ) NOT NULL ,
- `password` VARCHAR( 32 ) NOT NULL ,
- `email` VARCHAR( 50 ) NOT NULL ,
- PRIMARY KEY ( `userid` )
- )
After running the above query you will have your database and table all ready to go.
The Structure File (db.php)
The first PHP file will be the "structure" file. This is the file that will have our functions and database connectivity in it. This file will later be included at the top of every single PHP file on your website.
First we place session_start() on the first line. This function allow us to maintain our sessions with the login fields later. your PHP file will look like this:
The next thing we want to do is to make a connection to the database. We use the mysql_connect() and mysql_select_db() functions. After adding these two lines, the PHP script will look like this:
- <?php
- ?>
Now we create our user_login() function so we can use it every time the users enter their login information. We create the function name and the parameters (username and password) as shown below:
- function user_login($username, $password)
- {
- }
Let us now write the code that is actually needed to log in. We need to take the hash of the password and check it against the username. As long as there are more than 0 entries in the database for that username/password combination, we will allow the user to log in.
- <?php
- function user_login ($username, $password)
- {
- //take the username and prevent SQL injections
- //begin the query
- $sql = mysql_query("SELECT * FROM usersystem WHERE username = 'username' AND password = 'password' LIMIT 1");
- //check to see how many rows were returned
- if ($rows<=0 )
- {
- }
- else
- {
- //have them logged in
- $_SESSION['sername'] = $username;
- }
- }
- ?>
Now that we have the db.php file, let us make the user registration part.
Next Page: How to Implement User Registration
Comments
Weekly Offers
Sponsored Links
