Martin Laing - Multimedia Developer | Fife, Scotland

How to create simple Member Login for your websites. Part 1 - Member Login

How to create simple Member Login for your websites. Part 1 - Member Login

This tutorial aims to show you how to build a simple Member Login for a members area on your website.

OK what do you need to build a member system.

You need all of the following:

- Sessions
- Database
- Cookies(Optional)


First of we start by defining a Session so the browser knows the user can potentially be logged in at any minute.

We do that by inserting the following code at the top of our login page.

<?php
session_start();


We then want to go on and check and see if the user is already logged in or not:

// is the one accessing this page logged in or not?
if (!isset($_SESSION['admin_loggedin'])
|| $_SESSION['admin_loggedin'] !== true) {

// not logged in, move to login page
$errorMessage = '';
if (isset($_POST['adminusername']) && isset($_POST['adminpassword'])) {





Note that i include functions.php and call a function called dbconnect(); all this does is includes the file that contains the database information and the function to connect to the database, you can replace this part with your own coding for this.

include('functions.php');
dbconnect();




Now we get the form variables from the login form.

$adminuser = $_POST['adminusername'];
$adminpass = $_POST['adminpassword'];

// check if the user id and password combination exist in database
$sql = "SELECT adminusername
FROM admin_users
WHERE adminusername = '$adminusername'
AND adminpassword = ('$adminpassword')";

$result = mysql_query($sql)
or die('Query failed. ' . mysql_error());

if (mysql_num_rows($result) == 1) {
// the user id and password match,
// set the session
$_SESSION['admin_loggedin'] = true;

// after login we move to the main page
header('Location: admin.php');
exit;
} else {
$errorMessage = 'Sorry, wrong user id / password';
}

}
} else {
header('Location: admin.php');
}

?>




And that concludes the php scripting for the login page. all you need now is to include a login form on the same page this could be as follows:

<form action="index.php" method="post" enctype="multipart/form-data" name="login" id="login">
<table width="200" border="1">
<tr>
<td colspan="2" class="sideheader"><div align="center"><strong>Admin Login </strong></div></td>
</tr>
<tr>
<td class="sidecontent"><strong>Username:</strong></td>
<td class="sidecontent"><input name="adminuser" type="text" id="adminuser" /></td>
</tr>
<tr>
<td class="sidecontent"><strong>Password: </strong></td>
<td class="sidecontent"><input name="adminpass" type="password" id="adminpass" /></td>
</tr>
<tr>
<td colspan="2" class="sidecontent"><div align="center">
<input name="login" type="submit" id="login" value="Login" />
</div></td>
</tr>
<tr>
<td colspan="2" class="sidecontent"><div align="center">
<h3>
<?php
if ($errorMessage != '') {
?>
</h3>
<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
</div></td>
</tr>
</table>
</form>




Next Page - Session Management
Part 3 - Logging Out

Comments

Comments will be displayed here

Back to Tutorial Home

Latest Projects

Fallen Angels Website Development Bucketball Candy Drop Fallen Angels Bebo Application Hector the Inspector

View Full Portfolio


Contact Me

If you would like to contact me, the easiest and quickest way to do so is by filling out the contact form on this website.

Send Me a Message

If you prefer you can contact me on any of these Websites:


Bebo facebook Myspace Twitter

Tutorials


How to send data from Flash to PHP How to create a Contact Form using PHP How to create simple Member Login for your websites. Part 1 - Member Login How to create simple Member Login for your websites. Part 2 - Session Management How to create simple Member Login for your websites. Part 3 - Logging Out Users