[BÀI 34] HƯỚNG DẪN VIẾT MỘT TRANG WEB CƠ BẢN (PHẦN 3: THỰC HIỆN CÁC CHỨC NĂNG USER)

http://laptrinhphp.vn/bai-34-huong-dan-viet-mot-trang-web-co-ban-phan-3-thuc-hien-cac-chuc-nang-user/

[BÀI 34] HƯỚNG DẪN VIẾT MỘT TRANG WEB CƠ BẢN (PHẦN 3: THỰC HIỆN CÁC CHỨC NĂNG USER)

[BÀI 34] HƯỚNG DẪN VIẾT MỘT TRANG WEB CƠ BẢN (PHẦN 3: THỰC HIỆN CÁC CHỨC NĂNG USER)

Việc quản lý tài khoản là cực kỳ quan trọng, gần như trang web nào cũng cần đến việc quản lý đăng nhập của người dùng, chỉ những ai có quyền quản trị mới có quyền quản trị trang web. Ở bài trước chúng ta tự thêm tài khoản vào database, trong thực tế không ai làm như vậy cả. Việc thêm/sửa/xóa người dùng nên thực hiện trên chính trang web của chúng ta.

Tạo trang thêm người dùng:

Ở phần quản lý người dùng, tôi có xây dựng giao diện để thực hiện một số chức năng với user:

Chức năng đầu tiên tôi sẽ hướng dẫn các bạn là chức năng thêm tài khoản:

Ở bài trước, tôi có thực hiện nhầm một số chỗ ở file admin/layout/layout.php, hôm nay tôi tiến hành sửa lại như sau:

Trang admin/layout/layout.php:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164

<!DOCTYPE html> <html> <head> <title>'Trang quản trị'</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <link rel="stylesheet" href="public/style.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body><div id="wrapper"> <!-- start avigation --> <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="index.php">'QUẢN TRỊ HỆ THỐNG'</a> </div> <!-- end avigation --> <!-- start top menu --> <ul class="nav navbar-right top-nav"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="glyphicon glyphicon-user"></i> 'Xin chào:&nbsp;<?php if(isset($_SESSION["loged"])) echo $_SESSION["loged"]; ?>'<b class="caret"></b></a> <ul class="dropdown-menu"> <li> <a href="index.php?controller=users&act=danhsach"><i class="glyphicon glyphicon-list-alt"></i>&nbsp;'Danh sách tài khoản'</a> </li> <li> <a href="index.php?controller=users&act=hoso"><i class="glyphicon glyphicon-user"></i>&nbsp;'Hồ sơ cá nhân'</a> </li> <li> <a href="index.php?controller=users&act=add"><i class="glyphicon glyphicon-plus-sign"></i>&nbsp;'Thêm tài khoản'</a> </li> <li class="divider"></li> <li> <a href="index.php?act=logout"><i class="glyphicon glyphicon-off"></i>&nbsp;'Đăng xuất'</a> </li> </ul> </li> </ul> <!-- end top menu --> <!-- start menu left --> <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul class="nav navbar-nav side-nav"> <li style="background:#52bc89;color:#fff;"> <a href="#" style="color:#fff;"><i class="glyphicon glyphicon-folder-open"></i>&nbsp;&nbsp;'Danh mục'</a> </li> <li> <a href="javascript:;" data-toggle="collapse" data-target="#demo_dmcc"><i class="glyphicon glyphicon-picture"></i> Banner <i class="glyphicon glyphicon-chevron-down"></i></a> <ul id="demo_dmcc" class="collapse"> <li> <a href="#">'Banner hiện tại'</a> </li> </ul> </li> <li> <a href="javascript:;" data-toggle="collapse" data-target="#demo_dm"><i class="glyphicon glyphicon-picture"></i>' Danh sách khóa học '<i class="glyphicon glyphicon-chevron-down"></i></a> <ul id="demo_dm" class="collapse"> <li> <a href="#">'Danh sách'</a> </li> <li> <a href="#">'Thêm mới'</a> </li> </ul> </li> <li> <a href="javascript:;" data-toggle="collapse" data-target="#demo_dmm"><i class="glyphicon glyphicon-sunglasses"></i>' Dịch vụ của chúng tôi '<i class="glyphicon glyphicon-chevron-down"></i></a> <ul id="demo_dmm" class="collapse"> <li> <a href="#">'Danh sách'</a> </li> <li> <a href="#">'Thêm'</a> </li> </ul> </li> <li> <a href="javascript:;" data-toggle="collapse" data-target="#demo_bv"><i class="glyphicon glyphicon-globe"></i> 'Thông tin về Devpro' <i class="glyphicon glyphicon-chevron-down"></i></a> <ul id="demo_bv" class="collapse"> <li> <a href="#">'Chi tiết'</a> </li> </ul> </li> <li> <a href="javascript:;" data-toggle="collapse" data-target="#demo_a1"><i class="glyphicon glyphicon-camera"></i> 'Thông tin liên hệ' <i class="glyphicon glyphicon-chevron-down"></i></a> <ul id="demo_a1" class="collapse"> <li> <a href="#">'Chi tiết'</a> </li> </ul> </li> <li> <a href="javascript:;" data-toggle="collapse" data-target="#demo_a2"><i class="glyphicon glyphicon-tag"></i> 'Tin nhắn học viên' <i class="glyphicon glyphicon-chevron-down"></i></a> <ul id="demo_a2" class="collapse"> <li> <a href="#">'Danh sách'</a> </li> </ul> </li> <li> <a href="javascript:;" data-toggle="collapse" data-target="#demo_a456"><i class="glyphicon glyphicon-star-empty"></i> Design <i class="glyphicon glyphicon-chevron-down"></i></a> <ul id="demo_a456" class="collapse"> <li> <a href="#">'Để nâng cấp website, liên hệ:'</a> <a href="http://facebook.com/trung.hongoc">'Hồ Ngọc Trung'</a> <a href="#">0969 540 038</a> </li> </ul> </li> </ul> </div> </nav> <!-- end menu left --> <!-- 'start nội dung' --> <div id="page-wrapper"> <div class="container-fluid"> <?php //gọi controller động: $duongdan_controller=""; if(isset($_GET["controller"])){ $duongdan_controller="controller/controller_".$_GET["controller"].".php"; if(file_exists($duongdan_controller)) include $duongdan_controller; } ?> </div> </div> <!-- 'end nội dung' --> </div></body></html>

Theo đó thì tôi sửa lại trang admin/index.php như sau:

1234567891011121314151617181920212223

<?php include "../config.php"; session_start(); date_default_timezone_set("Asia/Ho_Chi_Minh"); ob_start(); include "model/model.php"; include "controller/controller.php"; ?> <?php //thực hiện đăng xuất: if(isset($_GET["act"])) if($_GET["act"]=="logout"){ unset($_SESSION["loged"]); header("location:index.php"); } ?> <?php //gọi controller đăng nhập: include "controller/controller_login.php"; ob_end_flush();?>

Tạo thư mục users trong admin/view/ để lưu giao diện các trang quản lý người dùng.

Tiếp tục tạo file add.php ở trong admin/view/users/ (trang thêm người dùng).

12345678910111213141516171819202122232425262728293031323334353637383940

<div class="panel panel-default"> <div class="panel-heading">'Thêm người dùng'</div> <div class="panel-body"> <form class="form-horizontal" action="" method="post"> <div class="form-group"> <label class="control-label col-sm-2" for="email">'Tài khoản:'</label> <div class="col-sm-10"> <input type="email" required name="email" class="form-control" id="email" placeholder="Enter email"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="email">'Họ tên'</label> <div class="col-sm-10"> <input type="text" required name="ten" class="form-control" id="email" placeholder="Enter your name"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">'Mật khẩu:'</label> <div class="col-sm-10"> <input type="text" class="form-control" required name="pass1" id="pwd" placeholder="Enter password"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">'Nhập lại mật khẩu:'</label> <div class="col-sm-10"> <input type="text" class="form-control" required name="pass2" id="pwd" placeholder="Enter password"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default" name="add_user">'Thêm'</button> </div> </div> </form> </div></div>

Ở phần thêm người dùng này, tôi chỉ cho phép thêm tài khoản là email, mật khẩu và họ tên người dùng.

Sau đó tôi viết controller cho trang thêm user này, tôi tạo file controller_users.php ở admin/controller/

  • Lưu ý: file controller_users.php là tôi đã đặt theo quy chuẩn mà tôi đã đặt ra, bằng cách: ví dụ tôi truyền lên url là: http://localhost:8080/devpro/project_mvc/admin/index.php?controller=users&act=add , lúc này biến controller của tôi có giá trị là “users”. Nên tôi phải đặt tên controller của tôi là controller_users.php

  • Tại sao tôi lại đặt tên file như vậy? Cùng xem lại đoạn code sau ở trang admin/layout/layout.php:

  • 123456789

    <?php //gọi controller động: $duongdan_controller=""; if(isset($_GET["controller"])){ $duongdan_controller="controller/controller_".$_GET["controller"].".php"; if(file_exists($duongdan_controller)) include $duongdan_controller; } ?>

    Bởi vì tôi đã có lệnh gọi file controller phù hợp dựa vào biến controller được truyền lên url.

  • File controller tôi gọi luôn nằm trong admin/controller/ và luôn có tên là controller_***.php , trong đó 3 dấu * là giá trị của biến controller được truyền lên url

Trong file admin/controller/controller_users.php tôi viết như sau:

12345678910111213141516171819202122232425262728293031323334353637383940414243

<?php class controller_users extends controller { public function __construct(){ parent::__construct(); if(isset($_GET["act"])){ $act = $_GET["act"]; $this->act($act); } if(isset($_POST["add_user"])) $this->add_user(); } //function gọi file theo biến trên url: public function act($act){ if(file_exists("view/users/".$act.".php")) include "view/users/".$act.".php"; } //function thêm tài khoản: public function add_user(){ $tk = $_POST["email"]; $mk1 = $_POST["pass1"]; $mk2 = $_POST["pass2"]; $ten = $_POST["ten"]; //nếu 2 mật khẩu giống nhau: if($mk1==$mk2){ $mk = md5($mk1); $this->model->query(" insert into users(email,password,name) values ('$tk','$mk','$ten') "); } header("location:index.php?controller=users&act=add"); } // } new controller_users();?>

Lưu và quay lại thử thêm người dùng, bạn sẽ thêm được người dùng.

Tạo chức năng hiển thị danh sách người dùng:

Sau khi đã thêm một số người dùng vào csdl, tôi tiến hành lấy thông tin của những người này để hiển thị ra:

Trong thư mục admin/view/users/ tôi tạo một file có tên là danhsach_users.php (để hiển thị danh sách các tài khoản người dùng).

và ở phần “danh sách tài khoản” tôi sẽ sửa link lại thành: index.php?controller=users&act=danhsach_users

Trong file admin/view/users/danhsach_users.php tôi viết như sau:

123456789101112131415161718192021222324

<div class="panel panel-default"> <div class="panel-heading">'Danh sách tài khoản'</div> <div class="panel-body"> <div class="table-responsive"> <table class="table table-hover table-bordered"> <tr> <th class="text-center">STT</th> <th class="text-center">Email</th> <th class="text-center">'Họ tên'</th> <th class="text-center">'Quản lý'</th> </tr> <tr> <td class="text-center"></td> <td></td> <td></td> <td> <a href="index.php?controller=users&act=danhsach_users&do=del&id=1" class="btn btn-danger">'Xóa'</a> </td> </tr> </table> </div> </div></div>

Tạo controller xử lý trang danh sách vừa rồi:

Vẫn trong file admin/controller/controller_users.php, tôi viết tiếp như sau:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556

<?php class controller_users extends controller { public function __construct(){ parent::__construct(); //lấy biến act trên url: if(isset($_GET["act"])){ $act = $_GET["act"]; //gọi giao diện thêm người dùng: if($act == "add") include "view/users/add.php"; //gọi giao diện hiển thị danh sách người dùng: else if($act == "danhsach_users"){ //lấy ra danh sách người dùng và thông tin của người dùng tương ứng, gán tất cả vào 1 mảng: $arrs = $this->model->fetch_all(" select * from users "); include "view/users/danhsach_users.php"; } else echo "Lỗi"; } //thực hiện gọi hàm thêm người dùng: if(isset($_POST["add_user"])) $this->add_user(); } //function thêm tài khoản: public function add_user(){ $tk = $_POST["email"]; $mk1 = $_POST["pass1"]; $mk2 = $_POST["pass2"]; $ten = $_POST["ten"]; //nếu 2 mật khẩu giống nhau: if($mk1==$mk2){ $mk = md5($mk1); $this->model->query(" insert into users(email,password,name) values ('$tk','$mk','$ten') "); } header("location:index.php?controller=users&act=add"); } // } new controller_users();?>

Quay lại trang: admin/view/users/danhsach_users.php, bây giờ tôi đổ dữ liệu của người dùng vào giao diện này:

1234567891011121314151617181920212223242526

<div class="panel panel-default"> <div class="panel-heading">'Danh sách tài khoản'</div> <div class="panel-body"> <div class="table-responsive"> <table class="table table-hover table-bordered"> <tr> <th class="text-center">STT</th> <th class="text-center">Email</th> <th class="text-center">'Họ tên'</th> <th class="text-center">'Quản lý'</th> </tr> <?php $stt=0;foreach($arrs as $arr){$stt++; ?> <tr> <td class="text-center"><?php echo $stt; ?></td> <td><?php echo $arr["email"]; ?></td> <td><?php echo $arr["name"]; ?></td> <td> <a href="index.php?controller=users&act=danhsach_users&do=del&id=<?php echo $arr["id"]; ?>" class="btn btn-danger">'Xóa'</a> </td> </tr> <?php } ?> </table> </div> </div></div>

Ở bước này tôi đã hoàn tất việc đổ dữ liệu người dùng hiển thị ra màn hình, ví dụ như dữ liệu của tôi khi hiển thị ra sẽ là như thế này:

Viết chức năng đổi mật khẩu:

Tại giao diện sau:

Tôi muốn khi nhấn vào “hồ sơ cá nhân”, sẽ hiển thị ra các thông tin cơ bản của tôi, và tôi có thể đổi mật khẩu ở đấy, để làm được điều này tôi tạo file hoso.php ở trong thư mục admin/view/users/ để viết giao diện đổi mật khẩu:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546

<div class="panel panel-default"> <div class="panel-heading">'Danh sách tài khoản'</div> <div class="panel-body"> <div class="table-responsive"> <table class="table table-hover table-bordered"> <tr> <th class="text-center">Email</th> <th class="text-center">'Họ tên'</th> </tr> <tr> <td></td> <td></td> </tr> </table> </div> <div class="panel panel-primary"> <div class="panel-heading">'Đổi mật khẩu'</div> <div class="panel-body"> <form class="form-horizontal" action="" method="post"> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">'Mật khẩu mới:'</label> <div class="col-sm-10"> <input type="password" class="form-control" required name="pass1" id="pwd" placeholder="Enter password"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">'Nhập lại mật khẩu:'</label> <div class="col-sm-10"> <input type="password" class="form-control" required name="pass2" id="pwd" placeholder="Enter password"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default" name="edit_user">'Đổi'</button> </div> </div> </form> </div> </div> </div></div>

Viết xử lý đổi mật khẩu:

Vẫn ở file admin/controller/controller_users.php , tôi viết tiếp như sau để lấy thông tin của người dùng đang đăng nhập:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566

<?php class controller_users extends controller { public function __construct(){ parent::__construct(); //lấy biến act trên url: if(isset($_GET["act"])){ $act = $_GET["act"]; //gọi giao diện thêm người dùng: if($act == "add") include "view/users/add.php"; //gọi giao diện hiển thị danh sách người dùng: else if($act == "danhsach_users"){ //lấy ra danh sách người dùng và thông tin của người dùng tương ứng, gán tất cả vào 1 mảng: $arrs = $this->model->fetch_all(" select * from users "); include "view/users/danhsach_users.php"; } //gọi giao diện trang hồ sơ cá nhân: else if($act == "hoso"){ $ten = $_SESSION["loged"]; //lấy thông tin của người dùng: $arr = $this->model->fetch_one(" select * from users where name = '$ten' "); include "view/users/hoso.php"; } else echo "Lỗi"; } //thực hiện gọi hàm thêm người dùng: if(isset($_POST["add_user"])) $this->add_user(); } //function thêm tài khoản: public function add_user(){ $tk = $_POST["email"]; $mk1 = $_POST["pass1"]; $mk2 = $_POST["pass2"]; $ten = $_POST["ten"]; //nếu 2 mật khẩu giống nhau: if($mk1==$mk2){ $mk = md5($mk1); $this->model->query(" insert into users(email,password,name) values ('$tk','$mk','$ten') "); } header("location:index.php?controller=users&act=add"); } // } new controller_users();?>

Quay lại file admin/view/users/hoso.php tôi viết tiếp như sau:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546

<div class="panel panel-default"> <div class="panel-heading">'Danh sách tài khoản'</div> <div class="panel-body"> <div class="table-responsive"> <table class="table table-hover table-bordered"> <tr> <th class="text-center">Email</th> <th class="text-center">'Họ tên'</th> </tr> <tr> <td><?php echo $arr["email"]; ?></td> <td><?php echo $arr["name"]; ?></td> </tr> </table> </div> <div class="panel panel-primary"> <div class="panel-heading">'Đổi mật khẩu'</div> <div class="panel-body"> <form class="form-horizontal" action="" method="post"> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">'Mật khẩu mới:'</label> <div class="col-sm-10"> <input type="password" class="form-control" required name="pass1" id="pwd" placeholder="Enter password"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">'Nhập lại mật khẩu:'</label> <div class="col-sm-10"> <input type="password" class="form-control" required name="pass2" id="pwd" placeholder="Enter password"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default" name="edit_user">'Đổi'</button> </div> </div> </form> </div> </div> </div></div>

Đến đây, tôi đã đổ được dữ liệu người dùng đang đăng nhập hiển thị ra màn hình, tiếp theo, tôi sẽ thực hiện đổi mật khẩu khi người dùng nhấn thay đổi mật khẩu:

file admin/controller/controller_users.php tôi viết tiếp như sau:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384

<?php class controller_users extends controller { public function __construct(){ parent::__construct(); //lấy biến act trên url: if(isset($_GET["act"])){ $act = $_GET["act"]; //gọi giao diện thêm người dùng: if($act == "add") include "view/users/add.php"; //gọi giao diện hiển thị danh sách người dùng: else if($act == "danhsach_users"){ //lấy ra danh sách người dùng và thông tin của người dùng tương ứng, gán tất cả vào 1 mảng: $arrs = $this->model->fetch_all(" select * from users "); include "view/users/danhsach_users.php"; } //gọi giao diện trang hồ sơ cá nhân: else if($act == "hoso"){ $ten = $_SESSION["loged"]; //lấy thông tin của người dùng: $arr = $this->model->fetch_one(" select * from users where name = '$ten' "); include "view/users/hoso.php"; //thực hiện đổi mật khẩu người dùng đang đăng nhập: if(isset($_POST["edit_user"])) $this->doi_pass(); } else echo "Lỗi"; } //thực hiện gọi hàm thêm người dùng: if(isset($_POST["add_user"])) $this->add_user(); } //function thêm tài khoản: public function add_user(){ $tk = $_POST["email"]; $mk1 = $_POST["pass1"]; $mk2 = $_POST["pass2"]; $ten = $_POST["ten"]; //nếu 2 mật khẩu giống nhau: if($mk1==$mk2){ $mk = md5($mk1); $this->model->query(" insert into users(email,password,name) values ('$tk','$mk','$ten') "); } header("location:index.php?controller=users&act=add"); } //function đổi pass người dùng đang đăng nhập: public function doi_pass(){ $nd = $_SESSION["loged"]; $mk1 = $_POST["pass1"]; $mk2 = $_POST["pass2"]; if($mk1==$mk2){ $mk = md5($mk1); $this->model->query(" update users set password = '$mk' where name = '$nd' "); } header("location:index.php?controller=users&act=hoso"); } } new controller_users();?>

Kết quả sẽ đổi được mật khẩu, bạn hãy thử đăng xuất và đăng nhập lại để chắc chắn rằng mật khẩu đã được thay đổi thành công!

Viết chức năng xóa người dùng:

Quay lại mục “danh sách tài khoản”

Tôi muốn, khi tôi nhấn vào nút xóa thì tài khoản sẽ bị xóa đi. Cùng nhìn lại link của nút xóa, tôi viết link của nút xóa có dạng: index.php?controller=users&act=danhsach_users&do=del&id=… , việc làm của tôi đơn giản là kiểm tra xem biến $_GET[“do”] có tồn tại và bằng “del” không, sau đó tôi kiểm tra xem biến $_GET[“id”] có tồn tại hay không, nếu tồn tại tôi sẽ lấy giá trị của biến id để tiến hành xóa tài khoản theo id (trước khi xóa tôi tiến hành kiểm tra xem id có tồn tại không, nếu tồn tại thì mới thực hiện xóa). Chi tiết tôi sẽ viết tiếp ở file admin/controller/controller_users.php:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798

<?php class controller_users extends controller { public function __construct(){ parent::__construct(); //lấy biến act trên url: if(isset($_GET["act"])){ $act = $_GET["act"]; //gọi giao diện thêm người dùng: if($act == "add") include "view/users/add.php"; //gọi giao diện hiển thị danh sách người dùng: else if($act == "danhsach_users"){ //lấy ra danh sách người dùng và thông tin của người dùng tương ứng, gán tất cả vào 1 mảng: $arrs = $this->model->fetch_all(" select * from users "); include "view/users/danhsach_users.php"; //thực hiện xóa tài khoản: if(isset($_GET["do"])&&$_GET["do"]=="del"&&isset($_GET["id"])){ $id = $_GET["id"]; //kiểm tra xem id có tồn tại không: $count_id = $this->model->fetch_count(" select id from users where id = '$id' "); //vì id là duy nhất nên nếu tồn tại chỉ có 1 id trùng với id trên url: if($count_id==1) $this->model->query(" delete from users where id = '$id' "); header("location:index.php?controller=users&act=danhsach_users"); } } //gọi giao diện trang hồ sơ cá nhân: else if($act == "hoso"){ $ten = $_SESSION["loged"]; //lấy thông tin của người dùng: $arr = $this->model->fetch_one(" select * from users where name = '$ten' "); include "view/users/hoso.php"; //thực hiện đổi mật khẩu người dùng đang đăng nhập: if(isset($_POST["edit_user"])) $this->doi_pass(); } else echo "Lỗi"; } //thực hiện gọi hàm thêm người dùng: if(isset($_POST["add_user"])) $this->add_user(); } //function thêm tài khoản: public function add_user(){ $tk = $_POST["email"]; $mk1 = $_POST["pass1"]; $mk2 = $_POST["pass2"]; $ten = $_POST["ten"]; //nếu 2 mật khẩu giống nhau: if($mk1==$mk2){ $mk = md5($mk1); $this->model->query(" insert into users(email,password,name) values ('$tk','$mk','$ten') "); } header("location:index.php?controller=users&act=add"); } //function đổi pass người dùng đang đăng nhập: public function doi_pass(){ $nd = $_SESSION["loged"]; $mk1 = $_POST["pass1"]; $mk2 = $_POST["pass2"]; if($mk1==$mk2){ $mk = md5($mk1); $this->model->query(" update users set password = '$mk' where name = '$nd' "); } header("location:index.php?controller=users&act=hoso"); } } new controller_users();?>

Kết thúc bước này bạn sẽ xóa tài khoản thành công. Tải toàn bộ mã nguồn và sql của bài hôm nay tại đây!

Xem video hướng dẫn chi tiết bài hôm nay:

Last updated