Exforsys.com
 
Home Tutorials PHP
 

PHP and MySQL User Registration

 
Category: PHP
Comments (0)

How to Implement User Registration

Page 2 of 2


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:



 

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($s?>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



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.




First Page: PHP and MySQL 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 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape