Local Tech Repair: Creating a Login System Part 1, Website Series Part 1

Tuesday, January 4, 2011

Creating a Login System Part 1, Website Series Part 1

This is a first part of a series of releases on building a website using PHP and MySQL. I will be going through simple ways coding what can be done. i will try to explain everything that is going on with a script so that you can learn it and understand each part of it. This is for beginners to intermediate level programmers. If you are an expert please post in comments with corrections and other helpful hints and please use sources or code examples.

first we will need to create a database and table for member IDs, their passwords, and for their password salt, and for their privilege level. I will explain the later 2 later in this tutorial.
i will be using PHPmyadmin to create the database and table
so
steps

naming the database

Naming the table and how many rows. we need 5 rows
ID is just a number we associate them in the database. also ID is A_I which means auto increment and it is also our primary key.

We now have our database so we can now start working on the pages. 
lets first make sure we can create a new member.
this is a simple form to add a member to the table
-----------addmember.php---------
<?php
$title = "Add Member";
include('header.php');
?>

<td class="left_content">
<?include "leftside.php";?>
</td>


<td class="body_content"><div align="center"><strong>Add a new member for login</strong></div> <br />
<br />


<form method="post" action="checkaddmember.php">
<table>
<tr><td align="left">Username</td>
<td><input type="text" id="username" name="username" size="20"></td>
</tr>
<tr><td align="left">Password</td>
<td><input type="PASSWORD" id="password" name="password" size="20"></td>
</tr>
<tr>
<td align="left">
Email:
</td>
<td>
<input name="email" size="25">
</td>
</tr>
<tr><td align="left">Type</td>
<td><select name="privlege">
<option value="user">User</option>
<option value="Admin">Admin</option>
<option value="Moderator">Moderator</option>
</select></td>
</tr>
<tr><td colspan="2">
<p align="center">
<input type="submit" name="Submit" />
</tr>
</table>
</form>
</td>
<?
include('footer.php');
?>



----------------connect.php-----------------------
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="blogspot_database"; // Database name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>

-------------checkaddmember.php-----------------------
<?
$title = "check";
include ("header.php");
?>
<td class="left_content"><div align="center"><strong>Here are some Use full links</strong></div><br />
<?include ("leftside.php");?>
</td>
<td class="body_content"><strong><h4></h4></strong>
<?php

include('connect.php');

$mypassword=$_POST[password];
$salt = "website";
$encrypted_mypassword=hash('sha512', $salt.$mypassword);
$Name = $_POST[username];
$user = mysql_query("SELECT * FROM members WHERE (username='$Name')");
$exp = "^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";


if ($_POST[username] != "") {
if ($_POST[password] != "") {
if ($_POST[email] != "") {
if (mysql_num_rows($user) == 0) {
if (preg_match('~@(corban.edu|)$~', $_POST['email'])) {
if (mysql_num_rows($email2) == 0) {

$sqlquery = "INSERT INTO members (username, password, privlege)
VALUES('$_POST[username]','$encrypted_mypassword','$_POST[privlege]')";
$results = mysql_query($sqlquery);

mysql_close();
print "<p><p>Thanks again for registering and i hope you have a wonderful time.</p>";

?>
<form name="form1" method="post" action="/checklogin.php">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="2"><strong>Member Login </strong></td>
</tr>
<tr>
<td>Username:</td>

<td><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password:</td>

<td><input name="mypassword" type=PASSWORD id="mypassword"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</form>
<?

} else {
echo "Email already exists try using <a href='recovery.php'>recovery</a>";
}
} else {

echo "bad email address! Please use a corban.edu email address.";

}} else {
echo "<p> User Already Exists</p>";
}} else {
print "Please Put in an email";
}} else {
print "Please enter a Password";
}} else {
print "Need Please go back and put in an username";
}
?>

</td>
<?
include ("footer.php");
?>
------------------------------------------------------------

Sense i am running out of time on this I will finish this in part 2

More Resources: