Hướng Dẫn Code Form Đăng Nhập, Secure Login System With Php And Mysql

This is in continuation of the tutorial on making a membership based web site. Please see the previous page PHP registration hoanhtao3d.vn for more details.

Đang xem: Code form đăng nhập

download the code

You can do.w.n.lo.ad the whole source code for the registration/login system from the link below: Registrationhoanhtao3d.vn.zip The ReadMe.txt file in the do.w.n.lo.ad contains detailed instructions.

The login hoanhtao3d.vn

*

Here is the hoanhtao3d.vn code for the login hoanhtao3d.vn.

hoanhtao3d.vn id='login' action='login.php' method='post' accept-charset='UTF-8'>fieldset >legend>Loginlegend>input type='hidden' name='submitted' id='submitted' value='1'/>label for='username' >UserName*:label>input type='text' name='username' id='username' maxlength=”50″ />label for='password' >Password*:label>input type='password' name='password' id='password' maxlength=”50″ />input type='submit' name='Submit' value='Submit' />fieldset>hoanhtao3d.vn>

Logging in

We verify the username and the password we received and then look up those in the database. Here is the code:

function Login(){ if(empty($_POST<'username'>)) { $this->HandleError(“UserName is empty!”); return false; } if(empty($_POST<'password'>)) { $this->HandleError(“Password is empty!”); return false; } $username = trim($_POST<'username'>); $password = trim($_POST<'password'>); if(!$this->CheckLoginInDB($username,$password)) { return false; } session_start(); $_SESSION<$this->GetLoginSessionVar()> = $username; return true;}
In order to identify a user as authorized, we are going to check the database for his combination of username/password, and if a correct combination was entered, we set a session variable.

Here is the code to look up the username and password.

function CheckLoginInDB($username,$password){ if(!$this->DBLogin()) { $this->HandleError(“Database login failed!”); return false; } $username = $this->SanitizeForSQL($username); $pwdmd5 = md5($password); $qry = “Select name, email from $this->tablename “. ” where username='$username' and password='$pwdmd5' “. ” and confirmcode='y'”; $result = mysql_query($qry,$this->connection); if(!$result || mysql_num_rows($result) = 0) { $this->HandleError(“Error logging in. “. “The username or password does not match”); return false; } return true;}
Please notice that we must compare the value for the password from the database with the MD5 encrypted value of the password entered by the user. If the query returns a result, we set an “authorized” session variable, and then redirect to the protected content. If there are no rows with the entered data, we just redirect the user to the login hoanhtao3d.vn again.

Xem thêm: Cách Cài Bluetooth Win 7 Đơn Giản Với 4 Bước, Cài Thiết Bị Bluetooth Trong Windows 7

Access controlled pages

For those pages that can only be accessed by registered members, we need to put a check on the top of the page. Notice that we are setting an “authorized” session variable in the login code above. On top of pages we want to protect, we check for that session variable. If user is authorized, we show him the protected content, otherwise we direct him to the login hoanhtao3d.vn.

Include this sample piece of code on top of your protected pages:

require_once(“./include/membersite_config.php”);if(!$fgmembersite->CheckLogin()){ $fgmembersite->RedirectToURL(“login.php”); exit;}?>
See the file: access-controlled.php in the do.w.n.lo.aded code for an example.

Here is the CheckLogin() function code.

function CheckLogin(){ session_start(); $sessionvar = $this->GetLoginSessionVar(); if(empty($_SESSION<$sessionvar>)) { return false; } return true;}
These are the basics of creating a membership site. Now that you have the basic knowledge, you can experiment with it and add new features, such as a “Forgot password” page to allow the user to retrieve or change his password if he forgets it.

Updates

9th Jan 2012 Reset Password/Change Password features are added. The code is now shared at GitHub.

Xem thêm: Tổng Hợp 10 Thủ Thuật Macbook 2017, 29+ Hướng Dẫn Cách Sử Dụng Macbook Air

License

*

The code is shared under LGPL license. You can freely use it on commercial or non-commercial websites.

Xem thêm bài viết thuộc chuyên mục: Đăng nhập

Khuyễn Mãi Hot