Technical Training
PHP TutorialHow to Implement User Registration
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:
- Username: <input name="username" type="text" />
- Password: <input type="password" name="password" />
- Email: <input name="email" type="text" />
- <input type="submit" value="Submit" />
- </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.
- <?php
- include("db.php");
- {
- //Prevent SQL injections
- //Get MD5 hash of password
- //Check to see if username exists
- {
- }
- mysql_query("INSERT INTO usersystem (username, password, email) VALUES ( '$username', '$password', '$email')") or die (mysql_error()); echo "Account created.";)
- }
- ?>
- <html></html>
- <form action="register.php" method="post">
- Username: <input name="username" type="text" />
- Password: <input type="password" name="password" />
- Email: <input name="email" type="text" />
- <input type="submit" value="Submit" />
- </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
- <form action="login.php" method="post">
- Username: <input name="username" type="text" />
- Password: <input type="password" name="password" />
- <input type="submit" value="Submit" />
- </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:
- <?php
- include("db.php");
- if (isset($_POST['username'] && isset($_POST['password']))
- {
- user_login($_POST['username'], $_POST['password']);
- }
- ?>
- <form action="login.php" method="post">
- Username: <input name="username" type="text" />
- Password: <input type="password" name="password" />
- </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:
- <?php include "db.php";?>
- You can then show their username like so:
That completes the demonstration on how to implement a user login/membership system in your website.
PHP Tutorial
- WAMP Server
- PHP and MySQL User Registration
- PHP Strings
- How to Use Cookies in PHP
- PHP Tutorial : PHP & MySQL
- PHP Tutorials – Forms
- PHP Tutorials - File manipulation (Part-2)
- PHP Tutorials – File manipulation (Part 1)
- PHP Tutorials - Arrays (Part 2)
- PHP Tutorials : Arrays
- PHp Tutorials : Regular expressions
- PHP Tutorials : Strings (Part 2)
- PHP Tutorials - Strings (Part I)
- PHP Tutorials – Functions (Part-2)
- PHP Tutorials - Functions (Part I)
- PHP Tutorials - Loops
- PHP Tutorials - Conditional Statements
- PHP Tutorials - Operators
- PHP Tutorials - Operators (Part I)
- PHP Tutorials - Variables
- PHP Tutorials - Data Types
- PHP Tutorial - Syntax
- PHP Tutorial - Installation
- PHP Tutorial - Introduction







