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
<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>
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
<?php
include("db.php");
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']))
{
//Prevent SQL injections
//Get MD5 hash of password
$password = md5($_POST['password']);
//Check to see if username exists
$sql = mysql_query("SELECT username FROM usersystem WHERE username = 'username'");
{
}
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>
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
<form action="login.php" method="post">
Username:
<input name="username" type="text" />
Password:
<input type="password" name="password" />
<input type="submit" value="Submit" />
</form>
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
<?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>
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
<?php include "db.php";?>
You can then show their username like so:
<?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