php编程——验证码的实现(session方法)
2017-07-05 09:49
656 查看
index.PHP(实现输入验证码页面)代码如下:
<html>
<head>
<title>check code</title>
</head>
<body>
<form name=check method=post action=check.php>
<input type=hidden name=init value=1>
验证码:<input type=text name=code maxlength=4 style="width=50px;">
<!--得到验证码图片-->
<img src=image.php>
<p>
<input type=submit value="提交">
</form>
</body>
</html>
image.php(验证码生成页面)代码如下:
<?php
session_start();
srand((double)microtime()*1000000);
$authnum=rand(1000,9999);
$_SESSION["authnum"]= $authnum;
//session_register("authnum"); 这个php4.2以上版本已经不支持了,修改城红色显示
header("content-type:image/png");
function creat_image($width,$height,$authnum)
{
srand((double)microtime()*1000000);
$im = imagecreate($width,$height);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 3, $authnum, $black);
for($i=0;$i<200;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
}
creat_image(60,20,$authnum);
?>
check.php(验证界面)代码如下:
<?php
session_start();
if(!isset($init)) $init=0;
//if($init)
echo ($_POST["code"]);
echo $authnum;
if (isset($_POST['dosubmit']))
{
if($_POST['code']==$authnum)
{
echo "验证码正确!";
}
else echo "验证码错误,请重新输入!";
}
else echo "调用页面错误!"
?>
以上代码在调试过程中,出现以下错误提示:
Fatal error: Call to undefined function session_register() 的解决方法
<html>
<head>
<title>check code</title>
</head>
<body>
<form name=check method=post action=check.php>
<input type=hidden name=init value=1>
验证码:<input type=text name=code maxlength=4 style="width=50px;">
<!--得到验证码图片-->
<img src=image.php>
<p>
<input type=submit value="提交">
</form>
</body>
</html>
image.php(验证码生成页面)代码如下:
<?php
session_start();
srand((double)microtime()*1000000);
$authnum=rand(1000,9999);
$_SESSION["authnum"]= $authnum;
//session_register("authnum"); 这个php4.2以上版本已经不支持了,修改城红色显示
header("content-type:image/png");
function creat_image($width,$height,$authnum)
{
srand((double)microtime()*1000000);
$im = imagecreate($width,$height);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 3, $authnum, $black);
for($i=0;$i<200;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
}
creat_image(60,20,$authnum);
?>
check.php(验证界面)代码如下:
<?php
session_start();
if(!isset($init)) $init=0;
//if($init)
echo ($_POST["code"]);
echo $authnum;
if (isset($_POST['dosubmit']))
{
if($_POST['code']==$authnum)
{
echo "验证码正确!";
}
else echo "验证码错误,请重新输入!";
}
else echo "调用页面错误!"
?>
以上代码在调试过程中,出现以下错误提示:
Fatal error: Call to undefined function session_register() 的解决方法
1、PHP4.2以上版本不需要用session_register()注册SESSION变量,直接用: $_SESSION["string"]=“string"; 赋值。 2、用$_SESSION["string"]获取变量值。 3、用$_SESSION["string"][$n]可传递SESSION数组。
相关文章推荐
- php编程----验证码的实现(SESSION方法)
- php编程——验证码的实现(session方法)
- php编程——验证码的实现(session方法)
- PHP 用session与gd库实现简单验证码生成与验证的类方法
- PHP实例:用SESSION方法实现验证码
- PHP 用session与gd库实现简单验证码生成与验证的类方法
- PHP图形验证码的具体实现方法
- php实现session自定义会话处理器的方法
- jsp+Servlet编程实现验证码的方法
- 转:php中实现精确设置session过期时间的方法
- php实现多服务器共享session的方法
- PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
- PHP实现利用MySQL保存session的方法
- kphp框架中实现自定义session会话处理方法的php和msyql代码分享
- PHP实现利用MySQL保存session的方法
- php中实现精确设置session过期时间的方法
- PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
- PHP中的session永不过期的解决思路及实现方法分享