刚来新公司用的是laravel框架,所以练习一下写了个简单的抽奖案例。
首先是登录页面
登录后端代码
public function login (Request $request )
{
if ($request ->isMethod('post' )) {
$requestInfo = $request ->all();
if (empty ($requestInfo ['user_name' ]) || empty ($requestInfo ['password' ])) {
echo '<script>alert("用户名或密码不能为空");</script>' ;
return view('index/login' );
}
$userInfo = UserModel::where('user_name' ,$requestInfo ['user_name' ])
->select('password' ,'uid' )
->first();
if (empty ($userInfo )) {
echo '<script>alert("用户名不存在!");</script>' ;
return view('index/login' );
}
if (md5($requestInfo ['password' ]) == $userInfo ['password' ]) {
session(['user_name' => $requestInfo ['user_name' ],
'uid' => $userInfo ['uid' ]]);
echo '<script>location.href="/index/index"</script>' ;
}
}
return view('index/login' );
}
登录页面前端代码
<h1 > 登录页面</h1 >
<br >
<form action ="/index/login" method ="POST" >
<input type ="hidden" name ="_token" value =" {
{
csrf _token ()}} " >
用户名:<input type ="text" name ="user_name" >
<br >
密码:<input type ="password" name ="password" >
<br >
<input type ="submit" value ="登录" >
</form >
建表语句
CREATE TABLE `lty_user` (
`uid` INT (10 ) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR (20 ) NOT NULL DEFAULT '' COMMENT '用户名' ,
`password` CHAR (36 ) NOT NULL DEFAULT '' COMMENT '用户密码' ,
`created_at` INT (10 ) UNSIGNED NOT NULL DEFAULT '0' COMMENT '创建时间' ,
`updated_at` INT (11 ) NOT N