验证码刷新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()"/>//验证码显示图片