Exforsys

Home arrow Technical Training arrow PHP Tutorial

How to Implement User Registration

Page 2 of 2
Author: Exforsys Inc.     Published on: 31st Oct 2008    |   Last Updated on: 17th Mar 2011

PHP and MySQL User Registration

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:

Ads

 

Sample Code
  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>
  7.  
Copyright exforsys.com


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.

Sample Code
  1. <?php
  2. include("db.php");
  3. if (isset($_POST['username']) && isset($_POST['password']) && 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.  
  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 ("Username taken.");
  19. }
  20.  
  21.  
  22. mysql_query("INSERT INTO usersystem (username, password, email) VALUES ( '$username', '$password', '$email')") or die (mysql_error()); echo "Account created.";)
  23.  
  24. }
  25. ?>
  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>
  34.  
Copyright exforsys.com


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

Sample Code
  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>
  7.  
Copyright exforsys.com


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:

Sample Code
  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>
Copyright exforsys.com


Ads

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:

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



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

Read Next: PHP Strings


 
This tutorial is part of a PHP Tutorial tutorial series. Read it from the beginning and learn yourself.

PHP Tutorial

 

Comments