Creating Web Applications with facilities Sign in & Sign out (part 1 )

Many websites on the internet or web applications that use the facility registration with the Sign in and Sign out . Functions of course restrict access to certain services provided on those sites. Only members who have registered are allowed to access the special services . This time we will discuss how to create applications that have the facilities Sign in / Sign out . Discussed here are the pages only, while the contents of the application which is located in it can you develop yourself .
In making this application we need a database that will store data from members who have registered . Create a MySQL database with the following :
mysql > create database user ;
mysql > create table tbl_user (
- > username varchar ( 20 ) ,
- > passwd varchar ( 32 ) ) ;
The database consists of one table called " tbl_user "and the table have two fields namely fruit username and passwd for storing passwords . Why passwd field is determined to have 32 characters? We will discuss later .
Now at first we will create a member registration page first . Script is as follows :
<HTML>
<HEAD>
<TITLE> Sign Up Page < / TITLE >
< / HEAD >
<BODY>

Fill in your username and password that you want <br />
<pre>
NAME="SignUp" <form method="POST" ACTION="signup_process.php">
Username : <input TYPE="text" NAME="username">
Password : <input TYPE="password" NAME="passwd">
<br />
<input TYPE="submit" VALUE="Submit">
< / FORM >
< / BODY >
< / HTML >
Keep this script with any name signup.htm signup.php or may , as in reality there is no php script on this file .
Next we will create a file signup_process.php that will process user registration will do. Script is as follows :
<HTML>
<HEAD>
Processing your <TITLE> Account < / TITLE >
< / HEAD >
<BODY>
<? php
$ Username = $ _POST [ 'username '] ;
$ passwd = md5 ( $ _POST [' passwd '] ) ;
$ host = " localhost ";
$ db_user = "root ";
$ db_passwd = "root ";
$ db = "user ";
$ stop = 0 ;
$ sql = "select username from tbl_user ";
$ conn = @ mysql_connect ( $ host , $ db_user , $ db_passwd ) or
die ( " Connection failed : " . mysql_error ( ) ) ;
mysql_select_db ( $ db ) ;
$ qry = mysql_query ( $ sql ) or
die ( " Query is : " . mysql_error ( ) ) ;
while ( $ row = mysql_fetch_array ( $ qry ) ) {
if ( $ username == $ row [ 'username '] ) {
echo " username $ username was already Chosen by Someone else <br /> ";
echo " Go back and choose another username " ;
$ stop = 1 ;
break;
    }
}
if ( $ stop == 0 ) {
$ sql = "insert into tbl_user values ( '$ username ', '$ passwd ') ";
$ qry = mysql_query ( $ sql ) or
die ( " Query is : " . mysql_error ( ) ) ;
echo " Your account is successfully created <BR> ";
echo " <A href='login_page.php'> Click here < / A > to log in Page ";
}
?>
< / BODY >
< / HTML >
It is noteworthy that on the second line of the php script (after the <BODY> ) there is a function of md5 . md5 function is a function that calculates the md5 hash of a string . Md5 hash generate 32 characters in hexadecimal format . That's why the passwd field created character data type with 32 characters long . With this hash md5 your password will be stored in a form that is very difficult to guess and if you forget your password then the password can not be decoded as base64encode .
In this script was first checked whether the chosen username is already in the database. If you have any, then php will give a warning that the user name already exists and you are welcome to make a new username . If no , then your username and password information will be registered into the database and you will be notified that your account has been created.
After the sign up process is successful, now we 'll create a page to login . Script is as follows :
<HTML>
<HEAD>
<TITLE> Login Page < / TITLE >
< / HEAD >
<BODY>
Please Login : <br />
<pre>
NAME="login" <form method="POST" ACTION="login_process.php">
Username : <input TYPE="text" NAME="username">
Password : <input TYPE="password" NAME="passwd">
<br />
<input TYPE="submit" VALUE="Submit">
< / FORM >
< / PRE >
New users please register <A href="/signup.php"> here < / A >
< / BODY >
< / HTML >
Keep this file with a name or login.htm login.php .
This script will be processed on login_process.php file . Well , the script from login_process.php file is as follows :
<HTML>
<HEAD>
Authentification <TITLE> Page < / TITLE >
< / HEAD >
<BODY>
<?
$ Username = $ _POST [ 'username '] ;
$ passwd = md5 ( $ _POST [' passwd '] ) ;
$ host = " localhost ";
$ db_user = "root ";
$ db_passwd = "root ";
$ db = "user ";
$ stop = 0 ;
$ sql = "select * from tbl_user Nowhere username = '$ username ' " ;
$ conn = @ mysql_connect ( $ host , $ db_user , $ db_passwd ) or
die ( " Connection failed : " . mysql_error ( ) ) ;
mysql_select_db ( $ db ) ;
$ qry = mysql_query ( $ sql ) or
die ( " Query is : " . mysql_error ( ) ) ;
$ num = mysql_num_rows ( $ qry ) ;
$ Row = mysql_fetch_array ( $ qry ) ;

if ( $ num == 0 ) {
echo " Username not found <br /> ";
echo " Go back and try another username " ;
} else {
if ( $ passwd ! = $ row [' passwd '] ) {
echo " Your password is invalid <br /> ";
echo " Go back and type the valid password " ;
} else {
echo " You are successfully logged in. " ;
    }
}
?>
< / BODY >
< / HTML >
So many scripts with facilities Sign in and sign out , happy learning .

0 komentar:

Post a Comment