(二)简单的登陆注册系统--增加验证码部分
生活随笔
收集整理的這篇文章主要介紹了
(二)简单的登陆注册系统--增加验证码部分
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
login.html
<html> <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"> <h1>登陸系統</h1> <form?action="login1.php"?method="post"> 用戶名:<input?type="text"??name="username"/> <br/> 密 碼:<input?type="password"??name="password"/> <br/> <input?type="submit"?value="登陸"?/> <input?type="button"?value="注冊"?onclick="window.location.href='register.php'"/> </form> </html>login1.php
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>?? <?php //session_start(); $conn=mysql_connect("localhost","root","root"); if(!$conn)?{echo?"connect?fail".mysql_error(); } $db=mysql_select_db("login",$conn); if(!$db)echo?"select?database?fail!"; mysql_query("set?names?gb2312"); $name=$_POST['username']; $pwd=$_POST['password']; if($name&&$pwd){$sql="select?*?from?login_info?where?username='$name'?and?password=md5('$pwd')";$result=mysql_query($sql);$rows=mysql_fetch_array($result);if($rows){echo?"<script>";echo?"alert('登陸成功,頁面將跳轉到百度');";echo?"location.href='http://www.baidu.com';";echo?"</script>";}else{echo?"<script>";echo?"alert('用戶名或密碼有錯誤!');";echo?"location.href='login.html';";echo?"</script>";} } else?if($name==''||$pwd==''){???echo?"<script>";echo?"alert('用戶名或密碼不能為空!');";echo?"location.href='login.html';";echo?"</script>"; } mysql_close($conn);register.php
<html> <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"> <head></head> <h2>這里是注冊頁面</h2> <script?type="text/javascript"> function?create_code(){document.getElementById('code').src='verification.php?n='+Math.random()*10000; } </script> </head> <form?action="check_form.php"?method="post"> <table> <tr><td><B>新用戶注冊</B></td></tr> <tr><td>用戶名:</td><td><input?type="text"??name="username"?required="required"/></td></tr> <tr><td>密碼:</td><td><input?type="password"??name="password1"?required="required"/></td></tr> <tr><td>確認密碼:</td><td><input?type="password"??name="password2"?required="required"/></td></tr> <tr><td>性別:</td><td><input?type="text"??name="sex"?placeholder="選填項"/></td></tr> <tr><td>驗證碼:</td><td><input??type="text"?maxlength="4"?name="yzm_code1"?id="srand"?required="required"/></td></tr> <tr><td></td><td><img?id="code"?name="code"?src="verification.php"?onclick="create_code()"?title="點擊圖片刷新驗證碼"?></td></tr> <tr><td><input?type="submit"?value="注冊"></td> <td><input?type="reset"?value="重置"/></td></tr> </table> </form> </html>check_form.php
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>?? <?php header("Content-Type:text/html;charset=utf-8"); session_start(); if($_SESSION["yzm_code2"]!=$_POST["yzm_code1"]){echo?"<script>";echo?"alert('驗證碼輸入錯誤,請重新輸入');";echo?"location.href='register.php';";echo?"</script>"; }else?{$conn=mysql_connect("localhost","root","root");if(!$conn)?{echo?"connect?fail".mysql_error();}$db=mysql_select_db("login",$conn);if(!$db)echo?"select?database?fail!";$name_check=$_POST['username'];$password_check1=$_POST['password1'];$password_check2=$_POST['password2'];$sex_check=$_POST['sex'];if?($password_check1!=$password_check2){echo?"<script>";echo?"alert('兩次輸入的密碼不一致');";echo?"location.href='register.php';";echo?"</script>";}else?{$sql="select?*?from?login_info?where?username='$name_check'";$result=mysql_query($sql,$conn);if(mysql_num_rows($result)>0){echo?"<script>";echo?"alert('用戶名已經存在,請重新輸入!');";echo?"location.href='register.php';";echo?"</script>";}else?{??echo?"<script>";echo?"alert('注冊成功!');";echo?"location.href='login.html';";echo?"</script>";$sqlinsert="insert?into?login_info?(username,password,sex)?value?('$name_check',md5('$password_check1'),'$sex_check')";mysql_query($sqlinsert);}} } mysql_close($conn);verification.php
<?php? session_start();?//在把用戶信息存儲到?PHP?session?中之前,首先必須啟動會話。session_start()就是啟動session?的意思 get_code(4,60,20);//調用函數生成四個隨機數 function?get_code($num,$w,$h){?//這里把$w改成$weight,$h改成$height就出錯了,不知道為什么,可能是因為后兩個變量不能隨便用吧$code="";for($i=0;$i<$num;$i++){?//循環拼接驗證碼字符串,其中$num是驗證碼的個數$code.=rand(0,9);}$_SESSION["yzm_code2"]=$code;??//將生成的驗證碼寫入session,備驗證時用header("Content-type:image/PNG");??//定義圖片頭部$im=imagecreate($w,?$h);//創建圖片,定義顏色值$black=imagecolorallocate($im,?0,?153,?51);??//邊框色$gray=imagecolorallocate($im,?200,?200,?200);?//填充背景色$bgcolor=imagecolorallocate($im,255,?255,?255);imagefill($im,?0,?0,?$gray);??//填充背景imagerectangle($im,?0,?0,?$w-1,?$h-1,?$black);?//畫邊框$style=array($black,$black,$black,$black,$black,$gray,$gray,$gray,$gray,$gray);//隨機繪制兩條虛線,起干擾作用imagesetstyle($im,$style);??$y1=rand(0,$h);$y2=rand(0,$h);$y3=rand(0,$h);$y4=rand(0,$h);imageline($im,?0,?$y1,?$w,?$y3,?IMG_COLOR_STYLED);imageline($im,?0,?$y2,?$w,?$y4,?IMG_COLOR_STYLED);for?($i?=?0;?$i<80;?$i++)?{?//在畫布上隨機生成大量黑點,起干擾作用;imagesetpixel($im,?rand(0,?$w),?rand(0,?$h),?$black);?//將數字隨機顯示在畫布上,字符的水平間距和位置都按一定波動范圍隨機生成}$strx?=?rand(3,?8);for?($i?=?0;?$i<$num;?$i++)?{$strpos?=?rand(1,?6);imagestring($im,?5,?$strx,?$strpos,?substr($code,?$i,?1),?$black);$strx?+=?rand(8,?12);}imagepng($im);?//輸出圖片imagedestroy($im);?//釋放圖片所占內存 }?參考例子:http://www.tuicool.com/articles/6ruYbq
???????????????? http://jingyan.baidu.com/article/495ba841099a5338b30edeec.html
轉載于:https://my.oschina.net/zhangxuman/blog/505985
總結
以上是生活随笔為你收集整理的(二)简单的登陆注册系统--增加验证码部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 存储 总结
- 下一篇: 用系统某一用户的的身份运行某一命令