php 验证码中$_SESSION里得到的值为什么和图片不一致呢
//验证码
yzm.php:
<?
session_start();
//生成验证码图片
//Header("Content-type: image/PNG");
$im = imagecreate(44,18);
$back = imagecolorallocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景srand((double)microtime()*1000000);
//生成4位数字
for($i=0;$i<4;$i++){
$font = imagecolorallocate($im, rand(100,255),rand(0,100),rand(100,255));
$authnum=rand(1,9);
@$vcodes .= $authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}for($i=0;$i<100;$i++) //加入干扰象素
{
$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
imagepng($im,'yzm.png');
imagedestroy($im);$_SESSION['VCODE'] = $vcodes;
?>//提交页面
check.php:
<?php
include_once('include/yzm.php');
error_reporting(E_ALL);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>管理登录 - 多多留言本腾讯版</title>
<link href="css/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<meta http-equiv="Page-Enter" content="blendTrans(Duration=1)" />
<meta http-equiv="Page-Exit" content="blendTrans(Duration=1)" />
<link rel="shortcut icon" href="favicon.ico"/>
<Script Language=Javascript Src="include/png.js"></Script>
<table width="750" border="0" align="center" cellpadding="6" cellspacing="0" background="images/head1.png">
<tr>
<td width="270" height="65" align="right"><a href="index.php"><img src="images/logo.png" alt="多多留言本腾讯版 - 留言本" border="0"></a></td> <td height="80" align="center" valign="bottom">
<table width="75%" border="0" cellspacing="0" cellpadding="3">
<tr align="center">
<td><a href="index.php"><img src="images/1.png" width="36" height="36" border="0"><br>
浏览留言</a> </td>
<td><a href="add.php"><img src="images/4.png" width="36" height="36" border="0"><br>
签写留言</a></td>
<td><a href="admin_login.php"><img src="images/3.png" width="36" height="36" border="0"><br> 管理留言</a></td>
<td><a href="/"><img src="images/5.png" width="36" height="36" border="0"><br>
鸽联首页</a></td>
</tr>
</table></td>
</tr>
</table>
<form name="form1" method="post" action="login_check.php" onSubmit="return FrontPage_Form1_Validator(this)">
<table width="750" border="0" align="center" cellpadding="0" cellspacing="10"> <tr>
<td align="center">
<table width="97%" border="0" cellpadding="0" cellspacing="15">
<tr>
<td align="center" valign="top">
<table width="100%" border="0" cellpadding="5" cellspacing="0" class="border2">
<tr>
<td height="50" colspan="2"> <table width="95%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="9%" align="center"><font color="#0066FF"><img src="images/7.png"></font></td> <td width="91%" class="font16"><img src="images/login.png"></td>
</tr>
</table></td>
</tr>
<tr>
<td width="35%" align="right"><font color="#333333">管理员帐号:</font></td>
<td width="65%"> <input name="admin_user" type="text" id="admin_user">
<font color="#999999">*</font></td> </tr>
<tr>
<td align="right"><font color="#333333">管理员密码:</font></td>
<td><input name="admin_pass" type="password" id="admin_pass">
<font color="#999999">*</font></td>
</tr>
<tr>
<td align="right"><font color="#333333">输入验证码:</font></td> <td>
<input name="unum" type="text" id="unum" size="10">
<font color="#999999">*</font> <IMG SRC='<?php echo 'yzm.png?'.time();?>' WIDTH='50' HEIGHT='25' BORDER=0 ALT=''></td>
</tr>
<tr>
<td align="right"> <script language=JavaScript>
function FrontPage_Form1_Validator(theForm)
{
if (theForm.admin_user.value == "")
{
alert("请输入管理员帐号!");
theForm.admin_user.focus();
return (false);
}
if (theForm.admin_pass.value == "")
{
alert("请输入管理员密码!");
theForm.admin_pass.focus();
return (false);
}
if (theForm.unum.value == "")
{
alert("请您输入验证码!");
theForm.unum.focus();
return (false);
}
return (true);
}
</script></td>
<td height="50" class="font14"> <input type="submit" name="Submit" value=" 登录 ">
<input name="action" type="hidden" id="action" value="add">
</td> </tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form><div align="center">
<table width="98%" border="0" cellspacing="5" cellpadding="3">
<tr>
<td align="center"><p>Copyright © 2007 - 2008 <a href="http://www.crpa.info">Federal Pigeons</a> Inc. All Rights Reserved
<br>多多留言本腾讯版 版权所有 <a href="http://www.miibeian.gov.cn" target="_blank">京ICP备08080088号</a><br />
</p> </td>
</tr>
</table>
</div>
</body>
</html>
<?php
include_once('include/config.inc.php');
include_once('include/yzm.php');
if (@$_POST['action'] == 'add')
{
$user = $_POST['admin_user'];
$pwd = $_POST['admin_pass'];
$unum = $_POST['unum'];
echo $unum;
echo $_SESSION['VCODE'];
if ($unum == $_SESSION['VCODE'])
{
$sql = "select * from tb_users where username=$user & password=$pwd";
$rs = mysql_query($sql);
$num = mysql_num_rows($rs);
if ($num > 0)
{
$_SESSION['ac'] = 'ok';
header("location:index.php");
}else
{
echo "<script language='javascript'>alert('用户名或密码错误');</script>";
}
}else
{
echo "<script language='javascript'>alert('验证码错误,请重新输入');</script>";
}
}?>
//验证码
yzm.php:
<?
session_start();
//生成验证码图片
//Header("Content-type: image/PNG");
$im = imagecreate(44,18);
$back = imagecolorallocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景srand((double)microtime()*1000000);
//生成4位数字
for($i=0;$i<4;$i++){
$font = imagecolorallocate($im, rand(100,255),rand(0,100),rand(100,255));
$authnum=rand(1,9);
@$vcodes .= $authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}for($i=0;$i<100;$i++) //加入干扰象素
{
$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
imagepng($im,'yzm.png');
imagedestroy($im);$_SESSION['VCODE'] = $vcodes;
?>//提交页面
check.php:
<?php
include_once('include/yzm.php');
error_reporting(E_ALL);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>管理登录 - 多多留言本腾讯版</title>
<link href="css/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<meta http-equiv="Page-Enter" content="blendTrans(Duration=1)" />
<meta http-equiv="Page-Exit" content="blendTrans(Duration=1)" />
<link rel="shortcut icon" href="favicon.ico"/>
<Script Language=Javascript Src="include/png.js"></Script>
<table width="750" border="0" align="center" cellpadding="6" cellspacing="0" background="images/head1.png">
<tr>
<td width="270" height="65" align="right"><a href="index.php"><img src="images/logo.png" alt="多多留言本腾讯版 - 留言本" border="0"></a></td> <td height="80" align="center" valign="bottom">
<table width="75%" border="0" cellspacing="0" cellpadding="3">
<tr align="center">
<td><a href="index.php"><img src="images/1.png" width="36" height="36" border="0"><br>
浏览留言</a> </td>
<td><a href="add.php"><img src="images/4.png" width="36" height="36" border="0"><br>
签写留言</a></td>
<td><a href="admin_login.php"><img src="images/3.png" width="36" height="36" border="0"><br> 管理留言</a></td>
<td><a href="/"><img src="images/5.png" width="36" height="36" border="0"><br>
鸽联首页</a></td>
</tr>
</table></td>
</tr>
</table>
<form name="form1" method="post" action="login_check.php" onSubmit="return FrontPage_Form1_Validator(this)">
<table width="750" border="0" align="center" cellpadding="0" cellspacing="10"> <tr>
<td align="center">
<table width="97%" border="0" cellpadding="0" cellspacing="15">
<tr>
<td align="center" valign="top">
<table width="100%" border="0" cellpadding="5" cellspacing="0" class="border2">
<tr>
<td height="50" colspan="2"> <table width="95%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="9%" align="center"><font color="#0066FF"><img src="images/7.png"></font></td> <td width="91%" class="font16"><img src="images/login.png"></td>
</tr>
</table></td>
</tr>
<tr>
<td width="35%" align="right"><font color="#333333">管理员帐号:</font></td>
<td width="65%"> <input name="admin_user" type="text" id="admin_user">
<font color="#999999">*</font></td> </tr>
<tr>
<td align="right"><font color="#333333">管理员密码:</font></td>
<td><input name="admin_pass" type="password" id="admin_pass">
<font color="#999999">*</font></td>
</tr>
<tr>
<td align="right"><font color="#333333">输入验证码:</font></td> <td>
<input name="unum" type="text" id="unum" size="10">
<font color="#999999">*</font> <IMG SRC='<?php echo 'yzm.png?'.time();?>' WIDTH='50' HEIGHT='25' BORDER=0 ALT=''></td>
</tr>
<tr>
<td align="right"> <script language=JavaScript>
function FrontPage_Form1_Validator(theForm)
{
if (theForm.admin_user.value == "")
{
alert("请输入管理员帐号!");
theForm.admin_user.focus();
return (false);
}
if (theForm.admin_pass.value == "")
{
alert("请输入管理员密码!");
theForm.admin_pass.focus();
return (false);
}
if (theForm.unum.value == "")
{
alert("请您输入验证码!");
theForm.unum.focus();
return (false);
}
return (true);
}
</script></td>
<td height="50" class="font14"> <input type="submit" name="Submit" value=" 登录 ">
<input name="action" type="hidden" id="action" value="add">
</td> </tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form><div align="center">
<table width="98%" border="0" cellspacing="5" cellpadding="3">
<tr>
<td align="center"><p>Copyright © 2007 - 2008 <a href="http://www.crpa.info">Federal Pigeons</a> Inc. All Rights Reserved
<br>多多留言本腾讯版 版权所有 <a href="http://www.miibeian.gov.cn" target="_blank">京ICP备08080088号</a><br />
</p> </td>
</tr>
</table>
</div>
</body>
</html>
<?php
include_once('include/config.inc.php');
include_once('include/yzm.php');
if (@$_POST['action'] == 'add')
{
$user = $_POST['admin_user'];
$pwd = $_POST['admin_pass'];
$unum = $_POST['unum'];
echo $unum;
echo $_SESSION['VCODE'];
if ($unum == $_SESSION['VCODE'])
{
$sql = "select * from tb_users where username=$user & password=$pwd";
$rs = mysql_query($sql);
$num = mysql_num_rows($rs);
if ($num > 0)
{
$_SESSION['ac'] = 'ok';
header("location:index.php");
}else
{
echo "<script language='javascript'>alert('用户名或密码错误');</script>";
}
}else
{
echo "<script language='javascript'>alert('验证码错误,请重新输入');</script>";
}
}?>
已经调试了好多次了!还是不行!
就是和图片不一致
通常在一个验证交互中 yzm.php 只能执行一次,除非你的脚本中设有“重新生成验证码”的功能
它的src是什么意思啊??
为什么多次执行?
每次不也是加载一次吗?
src就是图片链接
不过这是链接的一个动态生成的图片
加上session_start();<IMG SRC='<?php echo 'include/yzm.php?'.time();?>' WIDTH='50' HEIGHT='25' BORDER=0 ALT=''>
还是不行他不是同步得到的
imagedestroy($im);
而且直接src='yzm.php'会不会影响安全问题呢?
首先生成图片,你调用的是生成的图片,并不是php文件,这样无法随时更新验证码,你在后面加个time()很可笑,根本没用,因为那是生成的图片(静的),劝你把img的src编程yzm.php?x=time()这个格式,而yzm.php中显示验证码