控制器代碼:
~~~
<?php
/**
* Created by PhpStorm.
* User: 小灰灰
* Date: 2019/12/28 0028
* Time: 21:03
*/
namespace app\admin\controller;
use think\Controller;
use think\captcha\Captcha;
use think\Db;
use think\facade\{Cookie,Session,Cache};
class Login extends Controller
{
public function index()
{
$captcha = new Captcha();
if(input('post.dosubmit')){
if(config('huiadmin.login_captcha')){
if( !captcha_check(input('code') )){
return json(['status'=>1001,'msg'=>'驗證碼輸出錯誤,請重新輸入!!!']);
}
}
$user_info = Db::name('admin')->where('username',input('post.username'))->find();
if(empty($user_info['username'])){
return json(['status'=>1002,'msg'=>'用戶名或密碼錯誤!']);
}
$login_failure_retry = config('huiadmin.login_failure_retry');
$login_failure_times = config('huiadmin.login_failure_times');
$login_failure_min = config('huiadmin.login_failure_min');
if( $login_failure_retry && $user_info['loginfailure'] >=$login_failure_times && (time()-$user_info['updatetime'])< $login_failure_min*60 )
{
return json(['status'=>1002,'msg'=>'密碼錯誤次數超過'.$login_failure_times.'次,請'.$login_failure_min.'分鐘之后重試!']);
}
$pass = md5(md5(input('post.password')) . $user_info['salt']);
if($user_info['password']!==$pass){
Db::name('admin')->where('username',input('post.username'))->setInc('loginfailure');
return ['status'=>1002,'msg'=>'用戶名或密碼錯誤!!!'];
}else{
if($user_info['status']!='normal'){
return ['status'=>1003,'msg'=>'該用戶已被禁止訪問'];
}else{
$user_session_info = [
'uid' => $user_info['id'],
'username' => $user_info['username'],
'nickname' => $user_info['nickname'],
'logintime'=> $user_info['logintime'],
'loginip' => $user_info['loginip'],
];
Cache::clear();
session('user_info', $user_session_info);
$data = ['loginip'=>ip(),'loginfailure'=>0,'logintime'=>time()];
Db::name('admin')->where('username',input('post.username'))->data($data)->update();
return['status'=>1,'msg'=>'登錄成功,正在跳轉~~~'];
}
}
}else{
return $this->fetch();
}
}
//自定義驗證碼類
public function verify()
{
$captcha = new Captcha(config('captcha.'));
return $captcha->entry();
}
/**
* 退出
*/
public function logout()
{
Session::clear();
Cookie::clear();
$this->success('退出成功!', 'index');
}
/**
*獲取bing背景圖
*/
public function getbing_bgpic(){
$idx = input('idx');
$api = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=$idx&n=1";
$data = self::object2array(json_decode(self::get_url($api)));
$pic_url = $data['images'][0]->{'url'}; //獲取數據里的圖片地址
if($pic_url){
$images_url ="https://cn.bing.com/".$pic_url; //如果圖片地址存在,則輸出圖片地址
}else{
$images_url="https://s1.ax1x.com/2018/12/10/FGbI81.jpg"; //否則輸入一個自定義圖
}
header("Location: $images_url"); //header跳轉
}
private function get_url($url)
{
$ch = curl_init();
$header[] = "";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
private function object2array($object) {
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
}
else {
$array = $object;
}
return $array;
}
}
~~~
登錄頁面HTML
~~~
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HuiCMF-后臺管理系統-v3.0</title>
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="__STATIC_ADMIN__css/font.css">
<link rel="stylesheet" href="__STATIC_ADMIN__css/weadmin.css">
<link rel="stylesheet" href="__STATIC_ADMIN__css/style.css">
</head>
<body class="login-bg" style="background: url({:url('getbing_bgpic')})">
<div class="login">
<div class="message">小灰灰內容管理系統-后臺管理系統</div>
<div id="darkbannerwrap"></div>
<form method="post" class="layui-form" action="javascript:;" onsubmit="return dosub(this)">
<input name="username" id="username" placeholder="用戶名" type="text" lay-verify="required" class="layui-input" required="required">
<hr class="hr15">
<input name="password" id="password" lay-verify="required" placeholder="密碼" type="password" class="layui-input" required="required">
{if config('huiadmin.login_captcha')}
<hr class="hr15">
<img src="{:url('verify')}" id="imgVcode" alt="captcha" title="點擊刷新驗證碼" border="0" class="verifyimg" onclick="this.src=this.src+'?'" style="float:right;height: 50px;"/>
<input type="text" name="code" id="code" class="login_input verify_input" lay-verify="required" placeholder="驗證碼" required="required" style="float:left; width: 40%">
{/if}
<hr class="hr20">
<input type="hidden" id="dosubmit" value="1">
<input class="loginin" value="登錄" lay-submit lay-filter="login" style="width:100%;" type="submit">
</form>
</div>
<div id="click_pic">
<a href="javascript:;" class="click_pic click_pic_a" title="上一個圖像"></a>
<a href="javascript:;" class="click_pic click_pic_b" title="下一個圖像"></a>
</div>
<div id="focus_ovr" data-bm="21"></div>
<!-- 底部結束 -->
</body>
</html>
{include file='footer'/}
<script>
$(document).ready(function () {
if (window != top) {
top.location.href = location.href;
}
});
function dosub() {
$.post("{:url('index')}",{
username:$('#username').val(),
password:$('#password').val(),
code:$('#code').val(),
dosubmit:$('#dosubmit').val(),
},function (res) {
if(res.status=='1001'){
$('#imgVcode').attr("src","{:url('verify')}"+"?" + Math.random());
layer.msg(res.msg,{'icon':2});
}
if(res.status=='1002'||res.status=='1003'){
layer.msg(res.msg,{'icon':2},function () {
location.reload();
});
}
if(res.status==1){
layer.msg(res.msg,{'icon':1},function () {
window.location.href="{:url('index/index')}";
});
}
})
}
var i = 0;
$(".click_pic_a").click(function () {
i += 1;
if (i <= 7) {
$(".login-bg").css({
"background": "url({:url('getbing_bgpic')}?idx=" + i + ")",
"transition": "500ms ease 500ms",
"-webkit-transition": "500ms ease 500ms"
});
$(".click_pic_a").css("opacity", "1")
} else {
i = 7;
alert("沒有了")
}
});
$(".click_pic_b").click(function () {
i -= 1;
if (i >= 0) {
$(".login-bg").css({
"background": "url({:url('getbing_bgpic')}?idx=" + i + ")",
"transition": "500ms ease 500ms",
"-webkit-transition": "500ms ease 500ms"
});
$(".click_pic_b").css("opacity", "1")
} else {
i = 0;
alert("沒有了");
}
})
</script>
~~~
- thinkphp
- thinkphp筆記
- 后臺登陸退出
- config配置
- 隱藏后臺模塊
- 單獨調用騰訊云行為驗證碼
- api接口跨域問題
- api接口創建案例代碼
- 使用gateway worker
- 使用swoole代碼筆記
- 使用隊列 think-queue筆記
- 后臺布局
- MySQL
- 1、關于lnmp mysql的一個坑
- 2、mysql實現group by后取各分組的最新一條
- 其他
- 搞笑的注釋代碼
- 分頁類
- nodejs 打包網址為exe
- 免費天氣預報API接口
- Ajax
- 簡單的ajax分頁1
- 通用ajax-post提交
- 引用的類庫文件
- Auth.php
- Auth.php權限控制對應的數據庫表結構
- Layui.php
- Pinyin.php
- Random.php
- Tree.php
- Tree2.php
- Js-Jq
- Git的使用
- 3、bootstrap-datetimepicker實現兩個時間范圍輸入
- CentOS安裝SSR做梯子
- Python爬蟲
- 1、安裝Gerapy
- 2、安裝Scrapy
- 3、Scrapy使用
- 4、Scrapy框架,爬取網站返回json數據(spider源碼)
- 0、Python pip更換國內源(一句命令換源)
- 服務器運維
- 1、寶塔使用webhook更新服務器代碼
- 2、搭建內網穿透
- 3、數據庫主從同步
- 4、數據庫復制
- hui-Shop問題
- 1、前端模板的注意事項
- 2、模板標簽