Bài 11 – Sử dụng session trong CakePHP chúng ta đã cùng làm quen với session và cách sử dụng session, với bài tiếp theo này sẽ là ứng dụng đăng nhập nhằm tổng hợp lại các kiến thức ở các bài học trước.

Về phần chuẩn bị:

– CSDL chúng ta sẽ sử dụng bảng users đã có ở Bài 4 – Tạo một Model – View – Controller

– Project thì vẫn là project cakephp.

– Các file sử dụng trong bài này: UsersController.php trong controller, User.php trong model, thư mục Users trong view tạo các 2 file trong thư mục này login.ctp(trang đăng nhập), info.ctp(thông tin sau đăng nhập)

Yêu cầu:  Hiển thị thông tin người dùng sau khi đăng nhập, sử dụng session để lưu lại thông tin đăng nhập

Nội dung:

1) UsersController.php

[code language=”php”]
<?php
class UsersController extends AppController{
var $name = "Users";
var $helpers = array("Html");
var $component = array("Session");

function login(){
$error="";
if(isset($_POST[‘ok’])){
$username = $_POST[‘username’];
$password = md5($_POST[‘password’]);
if($this->User->checkLogin($username,$password)){
$this->Session->write("session",$username); //ghi session
$this->redirect("info");//đăng nhập thành công chuyển trang thông tin
}else{
$error = "Tên đăng nhập và mật khẩu không đúng";
}
}
$this->set("error",$error);
}
function info(){
if($this->Session->check("session")){//kiểm tra có session hay không
$username = $this->Session->read(‘session’);
$this->set("name", $username);
}else{
$this->render("login");
}
}
function logout(){
$this->Session->delete(‘session’); //xóa session
$this->redirect("login"); //chuyển trang login
}
}
[/code]

2) User.php

[code language=”php”]
<?php
class User extends AppModel{
var $name = "User";
function checkLogin($username,$password){
$sql = "Select username,password from users where username=’$username’ AND password =’$password’";
$this->query($sql);
if($this->getNumRows()==0){
return false;
}else{
return true;
}
}
}
?>
[/code]

3) Các file view:

login.ctp

[code language=”html”]
<form method="post" action="">
<fieldset>
<legend>Đăng nhập</legend>
<label>Tên đăng nhập</label><input type="text" name="username" /><br />
<label>Mật khẩu</label><input type="password" name="password" /><br />
<input type="submit" name="ok" value="Login" />
<span class="error"><?php echo $error; ?></span>
</fieldset>
</form>
[/code]

info.ctp

[code language=”html”]
<span>Login : <?php echo $name;?> | <a href="logout">Logout</a></span>
<h1>Chúc mừng bạn đã đăng nhập thành công !</h1>
[/code]

Chạy thử http://localhost:8080/cakephp/users/login

2015-10-26_152803Sau khi đăng nhập thành công:

2015-10-26_152844

 

Đây là cách viết ứng dụng đăng nhập đơn giản và sử dụng cách viết bình thường gần như giống PHP thuần, trong cakephp còn có cách viết theo Auth vấn đề này sẽ được đề cập ở một bài khác. Chúc các bạn thành công.

 

[create_button_post thamso1=” thamso2=’Demo’ thamso3=’http://nongdanit.info/download/cakephp/bai12/bai-12.rar’] [/create_button_post]

 

[thongbao]

  1. Nếu có thắc mắc gì các bạn để lại comment bên dưới mình sẽ trả lời sớm nhất có thể.
  2. Cảm ơn các bạn đã đọc.

[/thongbao]

 

[Cakephp] Bài 12 – Ứng dụng đăng nhập trong Cakephp