日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php后台登录验证(含验证码)

發(fā)布時間:2023/12/20 php 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php后台登录验证(含验证码) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

驗證碼參考:https://www.cnblogs.com/qqblog/p/7658782.html

session實現(xiàn)功能:https://www.runoob.com/w3cnote/php-session-login.html(關(guān)鍵詞搜索:php登錄頁面session存儲問題)

index.php(登錄頁面):

<?php session_start(); if(empty($_SESSION['is_login'])){ ?> <!DOCTYPE html> <html> <head><meta charset="UTF-8"><meta name="renderer" content="webkit"><meta name="force-rendering" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><title>后臺登錄</title><style type="text/css">*{padding: 0;margin: 0;}.layout{position: absolute;left: 50%;transform: translateX(-50%);-webkit-transform: translateX(-50%);-moz-transform: translateX(-50%);-ms-transform: translateX(-50%);}.list{border: 1px solid #ddd;display: inline-block;padding: 15px 50px;}.txt_p{text-align: center;font-size: 1.8rem;padding: 50px 0 10px 0;}.list span{display: inline-block;width: 100px;font-size: 1rem;padding: 8px 0;text-align: right;}.list input{padding: 5px 0;outline:none;border: 1px solid #ccc;-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);-webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;-o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;}.list input:focus {border-color: #66afe9;outline: 0;-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);}.verify,.verify input,.verify img{vertical-align: middle;}.list .up_btn{padding-top: 20px;text-align: center;}.list .up_btn input{padding: 5px 10px;font-size: 1rem;border: 1px solid #2285ed;background-color: #FFF;margin: 0 10px;}.list .up_btn input:hover{background-color: #2285ed;color: #fff;}</style> </head> <body><p class="txt_p">后臺登錄</p><div class="layout"><form class="ajaxform list" action="./login_index.php" method="post"><div><span>用戶名:</span><input type="text" name="user" value="" class="input-text" /></div><div><span>密碼:</span><input type="password" name="password" value="" class="input-text" /></div> <div class="verify"><span>驗證碼:</span><input type="text" name="captcha" placeholder="請輸入圖片中的驗證碼"><img src="./image_captcha.php" onclick="this.src='image_captcha.php?'+new Date().getTime();"></div> <div class="up_btn"><input type="submit" value="提交" class="submit" /><input type="reset" value="重置" class="btn" /></div></div></form></div> </body> </html> <?php }else{echo "<script>window.location.href='./list_pl.php'</script>"; }?>

login_index.php(驗證頁面):exit;很重要一定要加,不然程序不會驗證驗證碼,輸入用戶名和密碼就能登錄進(jìn)去了。

<?php session_start(); header( "Content-type:text/html;charset=utf-8" ); $link = mysqli_connect( 'localhost' , 'root' , '123456' , 'login' ); if (! $link ) {die ( "連接失敗:" .mysqli_connect_error()); }$user = trim($_POST['user']);//用戶名 $password = trim($_POST['password']);//密碼 $captcha = strtolower($_POST['captcha']);//接受從登陸輸入框提交過來的驗證碼并轉(zhuǎn)化為小寫; $captchaimg = strtolower($_SESSION['captchaimg']) ;//從session中取得驗證碼并轉(zhuǎn)化為小寫; if(empty($user) || empty($password)){echo "<script>alert('用戶名密碼為空!');window.location.href='./index.php'</script>";exit; }elseif($captcha!=$captchaimg){echo "<script>alert('驗證碼錯誤!');window.location.href='./index.php'</script>";exit; } $rows=mysqli_fetch_array(mysqli_query( $link , "select * from login where user = '$user' and password = '$password'" )); if($rows){$_SESSION['is_login']=$rows['user'];//設(shè)置一個session值echo "<script>alert('登錄成功!');window.location.href='list_pl.php'</script>" ; }else{echo "<script>'用戶名或密碼錯誤!';window.location.href='./index.php'</script>" ; } ?>

list_pl.php(登錄成功跳轉(zhuǎn)的頁面):

<?php session_start(); if(empty($_SESSION['is_login'])){echo "<script>window.location.href='./index.php'</script>"; }else{$con = mysqli_connect('localhost','root','123456','tests'); mysqli_set_charset($con,'utf8'); if(!$con){ die('Could not connect:' . mysql_error($con)); } $sql = "select * from news"; $result = mysqli_query($con,$sql); $rows = array(); while($row = mysqli_fetch_assoc($result)){$rows[] = $row; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>列表</title><style>*{padding:0;margin:0;}.pd-lr{padding: 10px 0px}.container{width: 1200px;margin: 0 auto;text-align: center;}.clearfix{display:block;}.clearfix::after{content: "";display: block;height: 0;clear: both;visibility: hidden;overflow: hidden;}.txt{font-size: 1.8rem;padding: 40px 0 20px 0;}table{border-color: #2285ed;}.container td{padding: 5px 0;}.container table a{text-decoration: none;color: #000000;}.container table a:hover{color: #2285ed;}.btn{border: 0;padding: 8px 10px;font-size: 1rem;border: 1px solid #2285ed;background-color: #FFF;}.btn:hover{background-color: #2285ed;color: #fff;}.txt2{display:inline-block;font-size: 1rem;/* line-height: 39px; */}.align-right{text-align: right;}.a1{margin-top: 10px;}.a1 a{text-decoration: none;color: #000000;border: 1px solid #000;padding: 5px 10px;display: inline-block;}.a1 a:hover{color: #2285ed;border-color: #2285ed;}</style></head><body> <div class="container"><div class="align-right a1"><a href="./sign_out.php" class="">退出登錄</a></div><p class="txt">表單信息</p><table width="100%" border="1" cellpadding="0" cellspacing="0"> <colgroup><col width="60" /><col width="60" /><col width="100" /><col width="80" /><col width="150" /><col width="150" /><col width="80" /><col width="150" /></colgroup><thead><tr> <td width="30">全選:<input id="checkAll" type="checkbox"></td><td>id</td><td>客戶名稱</td><td>客戶手機(jī)號</td><td>公司名稱</td><td>客戶需求</td><td>操作</td> <td>提交時間</td> </tr></thead><tbody><?php foreach($rows as $k => $v) {?><tr><td><input type="checkbox" name="box" value="<?= $v['id'] ?>" class="checkOne"></td><td><?php echo $v['id'];?></td> <td><?php echo $v['username'];?></td> <td><?php echo $v['userphone'];?></td> <td><?php echo $v['company'];?></td><td><?php echo $v['textarea'];?></td><td><a href="javascript:confirm_delete('del.php?sno=<?php echo $v['id'];?>')">刪除</a><a href='modify.php?sno=<?php echo $v['id'];?>'>修改</a> </td> <td><?php echo $v['CreateTime'];?></td></tr><?php } ?></tbody></table><div class="pd-lr clearfix"><button style="float:left;" class="btn">批量刪除</button><!-- 數(shù)據(jù)表的總條數(shù) --><p style="float:right;" class="txt2">總共<span><?php$sql2 = "SELECT COUNT(*) FROM news"; //獲取某一張表的所有數(shù)據(jù)$all_value = $con->query($sql2); while ($nums=$all_value->fetch_assoc()) { //輸出每一行數(shù)據(jù) echo ($nums['COUNT(*)']); //獲取數(shù)據(jù)庫總條數(shù)} ?></span>條</p></div> </div><script src="../js/jquery-1.12.4.min.js"></script><script>// 單項刪除function confirm_delete(url){if(confirm('您確定要執(zhí)行刪除操作?')){window.location.href=url;}else{return false;}}// 全選,反選 $("#checkAll").on('change', function () {if ($(this).is(":checked")) { // 全選$(".checkOne").prop("checked",true);} else { // 反選$(".checkOne").prop("checked",false);} }); // 批量刪除 $('button').click(function(){var ids=$('.checkOne:checkbox');var str='';var count=0;for(var i=0;i<ids.length;i++){if(ids.eq(i).is(':checked')){str+=','+ids.eq(i).val();count++;}}var str=str.substr(1);if(confirm('你確定要刪除這'+count+'條數(shù)據(jù)嗎?')){//獲取id后刪除$.ajax({type:'GET',url:'admindel.php',// contentType: "application/json;charset=utf-8",data:{'str':str},// dataType:'json',//用get方法時不指定這項(可能是json數(shù)據(jù)不夠嚴(yán)謹(jǐn))success:function(res){if(res>0){for(var i=ids.length-1;i>=0;i--){if(ids.eq(i).is(':checked')){ids.eq(i).parent().parent().remove();}}}alert('刪除成功!');window.location.reload();},error:function(data){ console.log("數(shù)據(jù)加載失敗",data); } })}return false;});</script></body></html><?php }?>

sign_out.php(退出登錄的頁面):

<?php session_start(); // 這種方法是將原來注冊的某個變量銷毀 unset($_SESSION['is_login']); header("location:index.php"); /* 這種方法是銷毀整個 Session 文件session_destroy(); */ ?>

image_captcha.php(驗證碼生成頁面):

<?php /*** 字母+數(shù)字的驗證碼生成*/ // 開啟session session_start(); //1.創(chuàng)建黑色畫布 $image = imagecreatetruecolor(100, 30);//2.為畫布定義(背景)顏色 $bgcolor = imagecolorallocate($image, 255, 255, 255);//3.填充顏色 imagefill($image, 0, 0, $bgcolor);// 4.設(shè)置驗證碼內(nèi)容//4.1 定義驗證碼的內(nèi)容 $content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";//4.1 創(chuàng)建一個變量存儲產(chǎn)生的驗證碼數(shù)據(jù),便于用戶提交核對 $captcha = ""; for ($i = 0; $i < 4; $i++) {// 字體大小$fontsize = 10;// 字體顏色$fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));// 設(shè)置字體內(nèi)容$fontcontent = substr($content, mt_rand(0, strlen($content)), 1);$captcha .= $fontcontent;// 顯示的坐標(biāo)$x = ($i * 100 / 4) + mt_rand(5, 10);$y = mt_rand(5, 10);// 填充內(nèi)容到畫布中imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } $_SESSION["captchaimg"] = $captcha;//4.3 設(shè)置背景干擾元素 for ($$i = 0; $i < 200; $i++) {$pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor); }//4.4 設(shè)置干擾線 for ($i = 0; $i < 3; $i++) {$linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor); }//5.向瀏覽器輸出圖片頭信息 header('content-type:image/png');//6.輸出圖片到瀏覽器 imagepng($image);//7.銷毀圖片 imagedestroy($image); ?>

?

總結(jié)

以上是生活随笔為你收集整理的php后台登录验证(含验证码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。