Exforsys.com
 
Home Tutorials PHP
 

PHP and MySQL User Registration

 
Category: PHP
Comments (0)

PHP and MySQL User Registration

Page 1 of 2

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:


Sample Code
  1. CREATE TABLE `myDB`.`usersystem` (
  2. `userid` INT NOT NULL AUTO_INCREMENT ,
  3. `username` VARCHAR( 50 ) NOT NULL ,
  4. `password` VARCHAR( 32 ) NOT NULL ,
  5. `email` VARCHAR( 50 ) NOT NULL ,
  6. PRIMARY KEY ( `userid` )
  7. )
Copyright exforsys.com


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:


Sample Code
  1. <?php
  2. ?>
Copyright exforsys.com


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:


Sample Code
  1. <?php
  2. mysql_connect("localhost", "username of your database", "password of database");
  3. ?>
Copyright exforsys.com


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:


Sample Code
  1. function user_login($username, $password)
  2. {
  3. }
Copyright exforsys.com



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.


Sample Code
  1. <?php
  2. mysql_connect("localhost", "username of your database", "password of database");
  3. mysql_select_db("myDB");
  4. function user_login ($username, $password)
  5. {
  6. //take the username and prevent SQL injections
  7. $username = mysql_real_escape_string($username);
  8. //begin the query
  9. $sql = mysql_query("SELECT * FROM usersystem WHERE username = 'username' AND password = 'password' LIMIT 1");
  10. //check to see how many rows were returned
  11. $rows = mysql_num_rows($sql);
  12. if ($rows<=0 )
  13. {
  14. echo "Incorrect username/password";
  15. }
  16. else
  17. {
  18. //have them logged in
  19. $_SESSION['sername'] = $username;
  20. }
  21. }
  22. ?>
  23.  
Copyright exforsys.com


Now that we have the db.php file, let us make the user registration part.


Next Page: How to Implement User Registration


Read Next: PHP Strings



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape