本文目錄一覽:
PHP註冊關於刷新驗證碼的問題
驗證碼的作用是防止機器人頻繁提交
本身就是不被建議的一種體驗方式 而你這種使用隨機數字來作為驗證碼的
根本對機器人一點防範都沒有 而且也根本不存在看不清的效果
實際驗證碼一般都是圖片 php使用示例
img id=”verifyImg” src=”verify.php?uid=xxxxx” /span onclick=”chgVerify()”/span
verify.php 這個是php生成圖片的頁面
uid=xxxxx 這個是隨機字元串 防止緩存導致圖片不發生改變
chgVerify() js函數
chgVerify()
{
document.getElementById(‘verifyImg’).src=’verify.php?uid=’+Math.random();
}
驗證碼是把雙刃劍 用之前先考慮是否非得要用
php點擊刷新驗證碼
第二個 onclick 事件寫的不對。真確寫法如下
a href=”#” onclick=”document.getElementById(‘code’).src=’code.php?tm=’+Math.random()”看不清楚,刷新/a
你貌似沒搞清楚 js 裡面的this 到底是什麼,所簡單點 this 寫在哪個標籤里,指的就是那個標籤。
PHP驗證碼 實現點擊刷新
隨機產生的驗證碼放在一個文件1中
在另一個文件中引用文件1
img src=”code.php” onClick=”this.src=’code.php?nocache=’+Math.random()” style=”cursor:hand” alt=”點擊換一張”/
實現點擊圖片自動刷新圖片
thinkphp驗證碼怎麼點擊刷新
HTML代碼如下:
span
input type=”text” name=”code” placeholder=”驗證碼” style=”width:155px;height:35px;”///驗證碼輸入框
img id=”code” src=”__CONTROLLER__/verify” width=”128″ height=”35″///驗證碼顯示圖片
/span
JS控制點擊刷新代碼如下:
方法一:
!–驗證碼SRC點擊隨機生成–
script type=”text/javascript” charset=”utf-8″
var code = document.getElementById(“code”);
code.onclick = function(){
this.src = this.src+’?’+Math.random();
}
/script
方法二(推薦使用此方法,簡潔):
img id=”code” src=”__CONTROLLER__/verify” width=”128″ height=”35″ onClick=”this.src=this.src+’?’+Math.random()”///驗證碼顯示圖片
php中如何刷新驗證碼
我做的一個簡單的登錄界面有刷新驗證碼還有註冊功能 希望對LZ有幫助
這個是登錄界面 land.php
?php
@include_once(‘global.php’);
session_start();
$user = $_POST[‘username’];
$sql = sprintf(“select * from `p_admin` where `username` = ‘%s'”,$user);
//echo $sql;
$query = $db-query($sql);//調用golbal裡面的$db類
$fetch = $db-fetch_array($query);
if($_POST[‘sccode’]==$_SESSION[‘rand’]){
$state = $fetch ? md5($_POST[‘password’].$extra)==$fetch[‘password’]:FALSE;//是否登錄成功 如果失敗了返回為空echo $state沒有結果
if(!$state)
echo”script language=javascriptalert(‘用戶名或密碼錯誤’);/script”;
else {
$_SESSION[‘id’] = $fetch[‘m_id’];
$_SESSION[‘shell’] = md5($fetch[‘username’].$fetch[‘password’]);
$_SESSION[‘ontime’] = time();
//echo $_SESSION[‘id’].”br”;
//echo $_SESSION[‘shell’];
$action = new action();
$action -get_show_msg(‘admin/admin_main.php’, $show = ‘操作已成功!’);
}
}else
echo “script language=javascriptalert(‘驗證碼錯誤’);/script”;
?
!DOCTYPE HTML PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”
html xmlns=””head
meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″
title用戶登錄/title
link href=”style/global.css” rel=”stylesheet” type=”text/css”
link href=”style/link.css” rel=”stylesheet” type=”text/css”
link href=”style/layoutid.css” rel=”stylesheet” type=”text/css”
script language=”javascript”
function chform (obj) {
if (obj.username.value == “”) {
alert(“請輸入用戶名!”);
obj.username.focus();
return false;
}
if (obj.password.value == “”) {
alert(“請輸入密碼!”);
obj.password.focus();
return false;
}
if (obj.sccode.value == “”) {
alert(“請輸入驗證碼!”);
obj.sccode.focus();
return false;
}
return true;
}
function RefreshImage(id)
{
document.getElementById(id).src =’Confirm.php?’+Math.random(1);
}
/script
/headbody
!–用戶登錄開始–
div class=”login”
form id=”form1″ name=”form1″ method=”post” action=”” onsubmit=”return chform(this)”
ul
li class=”loginbtbj fright” style=”background-image: url(“images/hydl.gif”);”a title=”” href=”javascript:%20close();”img src=”images/close.gif” alt=”關閉” align=”right” border=”0″ height=”18″ width=”18″/a/li
li class=”fyellowxx fcenter”
……………………………………………………………………………………………….
/li
li class=”padleft43″
用戶名:input name=”username” size=”15″ style=”width: 150px;” type=”text”
/li
li class=”padleft43″
密 碼:input name=”password” size=”15″ style=”width: 150px;” type=”password”
/li
li class=”padleft43″
驗證碼:input name=”sccode” size=”6″ style=”width: 50px;” type=”text”img id=”re_confirm” onclick=”RefreshImage(‘re_confirm’)” src=”Confirm.php”
a title=”看不清?” href=”#” onclick=”RefreshImage(‘re_confirm’)”看不清?/a a href=register.php 註冊/a
/li
li class=”fyellowxx fcenter”
……………………………………………………………………………………………….
br
input name=”Submit” src=”images/dl.gif” style=”border: 0pt none; width: 80px; height: 31px;” type=”image”
/li
/ul
/form
/div
!–用戶登錄結束–
/body/html
這個是驗證碼的程序 confirm.php 圖片什麼的代碼我就不穿了 LZ可以借鑒下 有一點需要注意 就是這個confirm文件裡面不能報錯 我在這卡了很久
因為header這個之前不能輸出文本 所以如果報錯 就會無法顯示驗證碼
?php
session_start();
$random=”;
for($i=1;$i5;$i++){
$random .= dechex(rand(1,15));}
$_SESSION[‘rand’]=$random;
$im = imagecreatetruecolor(40,20);
$bg = imagecolorallocate($im,0,0,0);
$te = imagecolorallocate($im,255,255,255);
imagestring($im,rand(1,6),rand(1,6),rand(1,6),$random,$te);
header(“Content-type: image/jpeg”);
imagejpeg($im);
?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/241529.html