Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

PHP and MySQL User Registration

By Exforsys | on October 31, 2008 |
PHP Tutorial

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:

  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. )

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:

  1. <?php
  2. session_start(); 
  3. ?>

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:

  1. <?php
  2. session_start(); 
  3. mysql_connect("localhost", "username of your database", "password of database"); 
  4. mysql_select_db("myDB");
  5. ?>

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:

  1. function user_login($username, $password) 
  2. {
  3. }

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.

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

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

User Registration (register.php)

Now we can have a page where the user can register their account. It will be part HTML and part PHP. It can look as follows:

 
  1. <html></html><form action="register.php" method="post">
  2. Username: <input name="username" type="text" />
  3. Password: <input type="password" name="password" />
  4. Email: <input name="email" type="text" />
  5. <input type="submit" value="Submit" />
  6. </form>

The basic HTML structure of the registration form is shown above.  We can add some PHP code to handle the actual registration part. The form will send the information through POST, and we can handle it with PHP. We will take the username, make sure it does not exist, then create the account using mysql_query() to place the values within the database.

  1. <?php 
  2. include("db.php"); 
  3. if (isset($_POST['username']) &amp;&amp; isset($_POST['password']) &amp;&amp; isset($_POST['email']))
  4.  
  5. { 
  6. //Prevent SQL injections 
  7. $username = mysql_real_escape_string($_POST['username']); 
  8. $email = mysql_real_escape_string($_POST['email']); 
  9.  
  10.  
  11. //Get MD5 hash of password 
  12. $password = md5($_POST['password']); 
  13. &nbsp;
  14. //Check to see if username exists 
  15. $sql = mysql_query("SELECT username FROM usersystem WHERE username = '".$username."'");
  16. if (mysql_num_rows($sql)>0) 
  17. { 
  18. die (&quot;Username taken.&quot;); 
  19. } 
  20.  
  21.  
  22. mysql_query(&quot;INSERT INTO usersystem (username, password, email) VALUES ( '$username', '$password', '$email')&quot;) or die (mysql_error()); echo &quot;Account created.&quot;;) 
  23.  
  24. } 
  25. ?&gt;
  26.  
  27. <html></html>
  28. <form action="register.php" method="post">
  29. Username: <input name="username" type="text" />
  30. Password: <input type="password" name="password" />
  31. Email: <input name="email" type="text" />
  32. <input type="submit" value="Submit" />
  33. </form>

The login page (login.php)

Final let us create the login page. For login.php, we can have a form for the username and password, then we pass those values to the user_login() function we made earlier in the db.php script.
We can once again have a basic HTML structure for login.php

  1. <html></html>
  2. <form action="login.php" method="post">
  3. Username: <input name="username" type="text" />
  4. Password: <input type="password" name="password" />
  5. <input type="submit" value="Submit" />
  6. </form>

Now, we can add the PHP code before the HTML again to process the login, just like with the registration.

We do not really need to add much code, because most of it was done in the db.php page. Here is what you need:

  1. <?php
  2. include("db.php"); 
  3. if (isset($_POST['username'] && isset($_POST['password'])) 
  4. {     
  5. user_login($_POST['username'], $_POST['password']); 
  6. } 
  7. ?>
  8. <html></html>
  9. <form action="login.php" method="post">
  10. Username: <input name="username" type="text" />
  11. Password: <input type="password" name="password" />
  12. </form>

You now have everything you need for your user system. For all of the pages on your website, you must place this line at the very first line for this to work:

  1. <?php include "db.php";?>
  2. You can then show their username like so:
  3. <?php echo "Logged in as: " . $_SESSION['username'];?>


That completes the demonstration on how to implement a user login/membership system in your website.

« « WAMP Server
Life Skill Mastery » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • PHP Tutorials : Strings (Part 2)

    October 20, 2006 - 0 Comment
  • PHP Strings

    March 18, 2009 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP Tutorial : PHP & MySQL

    January 16, 2007 - 0 Comment
  • PHP Tutorial – Introduction

    April 29, 2006 - 0 Comment
  • PHP Tutorials – Variables

    September 22, 2006 - 0 Comment
  • PHP Tutorial – Installation

    May 17, 2006 - 0 Comment
  • PHP Tutorials – Operators (Part I)

    September 18, 2006 - 0 Comment
  • PHP Strings

    March 18, 2009 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorial : PHP & MySQL

    January 16, 2007 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Arrays (Part 2)

    November 15, 2006 - 0 Comment
  • PHP Tutorials : Arrays

    November 5, 2006 - 0 Comment
  • PHp Tutorials : Regular expressions

    November 2, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • PHP Strings
  • How to Use Cookies in PHP
  • WAMP Server
  • PHP Tutorial : PHP & MySQL
  • PHP Tutorials – Forms

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT