ÔõôʹÓÃthinkphp¿ò¼ÜʵÏֵǼ¹¦Ð§
µÚÒ»²½£º½¨ÉèµÇ¼ҳÃæ
Òª×îÏÈ£¬±ØÐèÏȽ¨ÉèÒ»¸öµÇ¼ҳÃæ¡£¸ÃÒ³ÃæÐèÒªº¬ÓÐÓû§ºÍÃÜÂëÀ¸Î»£¬ÒÔ¼°¡°µÇ¼¡±°´Å¥¡£¹¹½¨Ò³Ãæʱ£¬Ó¦¸ÃʹÓÃHTMLºÍBootstrap£¬²¢ÔÚÊÓͼ·¾¶ÖоÙÐнç˵¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Login</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <h3>Login</h3> <form class="form-horizontal" role="form" method="post" action="/login/check"> <div class="form-group"> <label class="control-label col-sm-2" for="username">Username:</label> <div class="col-sm-10"> <input type="text" class="form-control" id="username" placeholder="Enter username" name="username"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="password">Password:</label> <div class="col-sm-10"> <input type="password" class="form-control" id="password" placeholder="Enter password" name="password"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">Login</button> </div> </div> </form> </div> </body> </html>
µÇ¼ºó¸´ÖÆ
µÚ¶þ²½£º½¨Éè¿ØÖÆÆ÷Action
½¨ÉèÒ»¸ö¿ØÖÆÆ÷Action£¬ÓÃÓÚ´¦ÀíµÇ¼ÇëÇó¡£Óû§¿Éͨ¹ýÔÚ¸ÃActionÖÐÌṩÓû§ÃûºÍÃÜÂë¾ÙÐÐÑéÖ¤£¬ÒÔÅжÏÆäÊÇ·ñ±£´æÓÚϵͳÖС£µ±Óû§ÃûºÍÃÜÂëÓÐÓÃʱ£¬Ó¦½«Óû§ÐÅÏ¢´æ´¢ÓڻỰÖУ¬²¢½«Óû§Öض¨Ïòµ½Ó¦ÓóÌÐòÖ÷Ò³¡£ÒÔÏÂÊÇÒ»¸öʾÀýµÇ¼¼ì²éActionµÄ´úÂ룺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
<?php namespace app\index\controller; use think\Controller; use think\Session; class Login extends Controller { public function index() { return view(); } public function check($username, $password) { // ÔÚ´Ë´¦Ê¹ÓÃÄúµÄÂß¼À´¼ì²âÓû§ÊÇ·ñÓÐÓà if ($username == 'admin' && $password == 'password') { Session::set('username', $username); $this->redirect('/'); } else { $this->error('Invalid username or password'); } } }
µÇ¼ºó¸´ÖÆ
µÚÈý²½£º½¨Éè·ÓÉ
×îºó£¬ÐèÔÚ·ÓÉÖн¨ÉèÒ»¸ö»á¼û¿ØÖÆÆ÷µÄ·¾¶£¬ÒÔ±ã¿ÉÒԴӵǼҳÃæŲÓÃËü¡£ÒÔÏÂÊÇÒ»¸öʾÀý·ÓɵÄÀý×Ó£º
<?php use think\Route; Route::rule('/', 'index/index'); Route::rule('/login', 'login/index'); Route::rule('/login/check', 'login/check');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬’/login’»á¼û·¾¶Í¨¹ýÖ¸ÏòLogin¿ØÖÆÆ÷µÄindexÒªÁìÀ´ÏÔʾµÇ¼ҳÃæ¡£ÇëÇóµÇ¼»áÓÉ Login ¿ØÖÆÆ÷µÄ check ÒªÁì´¦Àí£¬¸ÃÒªÁìµÄ·¾¶Îª ‘/login/check’¡£
ÒÔÉϾÍÊÇÔõôʹÓÃthinkphp¿ò¼ÜʵÏֵǼ¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡