Registration and Login System
Written by Jen

Before you begin with this tutorial, please keep in mind that your host must support PHP 4.0 and have a MySQL database for you to add tables to.

Registration
Quite obviously, when people try to register with your site, they will need a form to fill out. You can set the following up on a normal html page:

<form name="register" method="post" action="register.php">
<input name="login id" type="text" value="loginid" size="20"><br>
<input name="password" type="text" value="password" size="20"><br>
<input name="email" type="text" value="email" size="50"><br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</form>

Notice how register.php is bolded. You will need to create another file called 'register.php' where the remainder of the registration processing script will be stored. you can choose to name it something else, but you will need to rename the action file name in the html form code above to that name as well) in the same directory. If you place the 'register.php' file in another directory, you will need to insert the entire directory path into where it currently says 'register.php' (ie. http://www.yourhost.com/your/directory/register.php). In the 'register.php' file, add the following script:

@mysql_connect("localhost", "mysql_login", "mysql_pwd") or die("Cannot connect to database!");
@mysql_select_db("tbl_login") or die("Cannot select database!");
$sql="INSERT INTO login_tbl (loginid, password and email) VALUES (".$loginid.”,”.$password.”,”.$email.”)”;
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}

localhost is where you put the URL of your host's MySQL database. If you are using your own network system on your computer, then you can leave it as is.
mysql_login and mysql_pwd is where you put your MySQL details (ie. username and password) so it can connect to the MySQL database in your host.

@mysql_select_db("tbl_login") refers to the table you have created in your MySQL database, which is named tbl_login. If you change the name of the table in your MySQL databse, you will also need to change it in the code above.

Login
A login form is now required. You can set this up on a normal html page by using the following code:

<form name="authentication" method="post" action="authentication.php">
<input name="login id" type="text" value="loginid" size="20"><br>
<input name="password" type="text" value="password" size="20"><br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</form>

Notice that authentication.php has been bolded. You will need to create a php file called 'authentication.php' (you can choose to name it something else, but you will need to rename the action file name in the html form code above to that name as well) in the same directory. If you place the 'authentication.php' file in another directory, you will need to insert the entire directory path into where it currently says 'authentication.php' (ie. http://www.yourhost.com/your/directory/authentication.php).

In the 'authentication.php' file, add the following script:

@mysql_connect("localhost", "mysql_login", "mysql_pwd") or die("Cannot connect to database!");
@mysql_select_db("tbl_login") or die("Cannot select database!");
$sql="SELECT loginid FROM login_tbl WHERE loginid=’".$loginid."’ and password=’".$password."’";
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
print "no such login in the system. please try again.";
exit();
}
else{
print "You have successfully logged in.";
//proceed to perform website functions
}

localhost is where you put the URL of your host's MySQL database. If you are using your own network system on your computer, then you can leave it as is.
mysql_login and mysql_pwd is where you put your MySQL details (ie. username and password) so it can connect to the MySQL database in your host.

@mysql_select_db("tbl_login") refers to the table you have created in your MySQL database, which is named tbl_login. If you change the name of the table, you will also need to change it in the code above.

Forgot Password
For those of you who don't want your database to be clogged up with the same user who keeps forgetting his/her password, you can set up a password retrieval system to go along with the registration and login system:

<form name="forgot" method="post" action="forgot.php">
<input name="login id" type="text" value="loginid" size="20"><br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</form>

You will need to create a forgot.php file to fill in the following script with:

@mysql_connect("localhost", "mysql_login", "mysql_pwd") or die("Cannot connect to database!");
@mysql_select_db("tbl_login") or die("Cannot select database!");
$sql="SELECT password, email FROM login_tbl WHERE loginid=’".$loginid."’";
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
print "No such login found in the system. Please try again.";
exit();
}
else {
$row=mysql_fetch_array($r);
$password=$row["password"];
$email=$row["email"];
$subject="Your password";
$header="from:you@yourdomain.com";
$content="Your password is ".$password;
mail($email, $subject, $row, $header);
print "An email containing the password has been sent to you";
}

localhost is where you put the URL of your host's MySQL database. If you are using your own network system on your computer, then you can leave it as is.
mysql_login and mysql_pwd is where you put your MySQL details (ie. username and password) so it can connect to the MySQL database in your host.

@mysql_select_db("tbl_login") refers to the table you have created in your MySQL database, which is named tbl_login. If you change the name of the table, you will also need to change it in the code above.

you@yourdomain.com is your email address that you want the email to be sent from. It won't actually use your email account to send it, the forgot password email will be automatically generated with your email address as the sender, so that visitors can reply to you if they wish.

Final thoughts
The above tutorial was on making a basic login system. It is definitely not the safest system, but it is the easiest to understand and implement if you just want to use it for something simple (ie. something that doesn't affect your privacy).