驗證碼刷新php
1. php 驗證碼
問題一、你的驗證碼代碼,沒有發現開啟session,也沒有將隨機產生的字元串保存到session,沒保存,自然就無法比對
問題二、要點擊一下刷新,其實很簡單,給圖片加一個點擊事件,讓圖片重新請求一次伺服器執行php驗證碼文件,需要注意的是,有於緩存的關系,在重新請求伺服器的時候,給驗證碼圖片路徑加一個隨機參數,防止刷新而驗證碼不變化,代碼示例如下:
<img src='code.php' onclick="this.src='code.php?'+Math.random();" />
2. 實現php中圖形驗證碼刷新的問題
首先要說明,瀏覽器對圖片,JS等文件會進行緩存當瀏覽器訪問圖片的時候,瀏覽器會查看緩存中是否有這張圖片如果有則使用緩存圖片,沒有則對伺服器重新發起訪問而瀏覽器判斷是否存在緩存文件是通過文件的url進行識別的胡握如果url不同,瀏覽器就會對伺服器發起新的請求褲余慶所毀塵有加上一個隨機參數就能實現驗證碼圖片的刷新因為隨機數不同,所以url就不同,所以每次瀏覽器都會對驗證碼圖片發起新的訪問,達到刷新驗證碼的功能無論是img.src = "imgcode.php?"+Math.random();還是imgcode.php?tm="+Math.random();都是為了不要使用瀏覽器中圖片緩存其中tm沒有任何意思,你可以隨便取你想要的名字甚至就像第一種情況不用給名字
3. php不能實時刷新驗證碼
可以試一信返試在驗證碼圖片地址上面加上一串隨機數,比如原來戚坦讓是www.xxx.com/index.php,現在就變高局成www.xxx.com/index.php?a=123456,讓瀏覽器認為不是一張圖片,就不會從緩存中拿圖片了
4. php驗證碼刷新點擊圖片能刷新但是點擊小圖標按鈕刷新不了為什麼
js代碼:
<scripttype="text/jscript">
functionRefreshImage(){
varelid=document.getElementById("imcode");
elid.src=elid.src+'?';
}
</script>
<label>驗證碼<imgid="imcode"src="運芹逗__APP__/Public/verify/"onMouseUp="RefreshImage()"alt="點擊刷新"/></label>
根據情況自己修改代碼。問這個問題,需要你提交代碼才知道問題在哪裡。上面的是js代碼,首滑下面的是html代碼。
解釋:
onMouseUp="RefreshImage()"
點擊觸發js事旁賣件
varelid=document.getElementById("imcode");
獲取id為imcode中的參數
elid.src=elid.src+'?';
定義刷新地址
5. PHP驗證碼刷新不了,是什麼問題
有緩存了 在驗猜腔證碼的scr後面給一個隨機數 來強制刷備行新
如 '/php/穗滾衫toolsfile/captcha.php?date=new Date()'
6. 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=javascript>alert('用戶名或密碼錯誤');</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=javascript>alert('驗證碼錯誤');</script>";
?>
<!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=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>
</head><body>
<!--用戶登錄開始-->
<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;$i<5;$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);
?>
7. php圖片驗證碼問題,我把圖片驗證碼隱藏之後,驗證碼怎麼不能刷新了,而且每次刷新顯示的都是一樣的
這是瀏覽器的請求規則問題,當圖片處於隱藏狀態時,更改<img/>標簽的src屬性不會重新下載圖片。要解決這個問題只有在更改src屬性後顯示一次圖片,然後再隱藏;
如:
var _src = "imgCode.php?_="+Math.random();
img.src=_src;
img.style.display="block";
img.style.display="none";
8. PHP驗證碼 實現點擊刷新
隨機產生的驗證碼放在一個文件1中
在另一個文件中引用文件1
<img src="code.php" onClick="this.src='code.php?nocache='+Math.random()" style="cursor:hand" alt="點擊換一張"/>
實現點擊圖片自動刷新圖片
9. php如何在進入頁面的時候自動刷新一次驗證碼
fnServerData": function ( sSource, aoData, fnCallback ) {
/* Add some extra data to the sender */
aoData.push( { "name"鋒唯租手: "more_data", "銀型培value": "my_value" } );
$.getJSON( sSource, aoData, function (json) {
/* Do whatever additional processing you want on the callback, then tell DataTables */
fnCallback(json)
} );
}
10. 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()"/>//驗證碼顯示圖片