首页 > 编程技术 > php

图片验证码生成应用实例

发布时间:2016-11-25 17:10

一款简单的php生成图形验证码程序,实例告诉你了如何去使用这段验证程序,有需要的可以参考一下。
 代码如下 复制代码

<?php教程
session_start();//开启session
if(isset($_POST['check']))
{
if($_POST['check'])
 {
if($_POST['check']==$_SESSION['check_pic'])
 {
 echo " 验证码正确".$_SESSION['check_pic'];
 }
else
 {
 echo " 验证码错误".$_SESSION['check_pic'];
 }
}
}
?>
<FORM METHOD=POST ACTION="">
<img" width=100% src="index.php"><br>    <!----链接图片--->
<input type="text" name="check" >
<input type="submit" value="提交">
</FORM>

index.php验证码生成程序

 代码如下 复制代码

<?php
session_start();


for($i=0;$i<4;$i++) //四位验证码
{@$rand.=dechex(rand(1,15));//先生成随机数,再将十进制转十六进制,注意"."
}
$_SESSION['check_pic']=$rand;
 $im=imagecreatetruecolor(100,30);//创建图片
$bg=imagecolorallocate($im,0,0,0);//设置颜色
$wh=imagecolorallocate($im,255,255,255);

imagestring($im,5,15,8,$rand,$wh);//字体,1-6

header("Content-type: image/jpeg");//输出图片
imagejpeg($im);
?>


 

一款讲得非常详细的登录代码,对php入门者有很大的帮助,有需要的朋友可以免费查看。

效果图。

index.php教程

 代码如下 复制代码

<!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=gbk" />
<title>系统登录</title>
<link href="css教程.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="box">
  <div id="denglu">
  <div id="pic">&nbsp;系统后台登陆</div>
    <form action="check.php" method="post">
      <p>用户名:
        <input type="text" name="name" id="name" />
      <span class="must">*</span></p>
      <p>密&nbsp;&nbsp;码:
        <input name="password" type="password" id="password" />
      <span class="must">*</span></p>
      <p>验证码:
        <input name="check" type="text" id="check" size="8" />
        <img" width=100% src="piccheck.php" /> <span class="must">*</span></p>
      <p class="form_button">
        <input type="submit" name="sub" id="sub" value="登陆" />
        <input type="reset" name="unsub" id="unsub" value="取消" />
      </p>
    </form>
  </div>
</div>
</body>
</html>

数据库教程连接 connect.php

 代码如下 复制代码

<?php
 $connect=mysql教程_connect("localhost","root","")or die("服务器连接失败");
 mysql_select_db("test",$connect)or die("没有建立相应的数据库");
$sql="select * from admin";
?>

图片验证码 piccheck.php

 代码如下 复制代码
<?php
/*
 * Created on 2011-8-10
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclips教程e - PHP - Code Templates
 */
 session_start();
 $code=rand(0,9).dechex(rand(10,15)).rand(0,9).dechex(rand(10,15));
 $_SESSION[pic]=$code;
 $image=imagecreatetruecolor(50,18);
 $color=imagecolorallocate($image,0,0,0);//第一次使用调色板,会设为背景颜色
 $colortext=imagecolorallocate($image,rand(100,255),rand(100,255),rand(100,255));
 imagestring($image,10,rand(1,15),rand(1,5),$code,$colortext);
 imagegif($image);
?>

css.css文件

 代码如下 复制代码

@charset "utf-8";
/* CSS Document */

body {
 background-color: #9CF;
 text-align: left;
}
#denglu {
 width: 400px;
 margin-top: 0px;
 margin-right: auto;
 margin-bottom: 0px;
 margin-left: auto;
 background-image: url(images/login.gif);
 background-repeat: no-repeat;
 height: 320px;
 text-indent: 6px;
}
#box #denglu form {
 position: relative;
 top: 50px;
 left: 10px;
 width: 92%;
}
.must {
 color: #F00;
 font-size: 12px;
}
#box #denglu #pic {
 background-image: url(images/dot1.gif);
 background-repeat: no-repeat;
 height: 20px;
 width: 100px;
 font-size: 12px;
 color: #F00;
 text-align: left;
 line-height: 18px;
 left: 10px;
 top: 30px;
 position: relative;
}

下面是一个将datetime日期时间转换成年\', \'个月\', \'天\', \'小时\', \'分种\', \'秒\'来显示,有需要的朋友可以参考一下。

下面是一个将datetime日期时间转换成年', '个月', '天', '小时', '分种', '秒'来显示,有需要的朋友可以参考一下。

/**
* 友好日期时间
*
* @param DateTime $datetime 日期时间
* @param int $size 精确到位数
* @throws InvalidArgumentException
* @return string
*/
function friendly_date($datetime, $size=1)
{
if (is_int($datetime)) {
$datetime = new DateTime($datetime);
}
if (!($datetime instanceof DateTime)) {
throw new InvalidArgumentException('invalid "DateTime" object');
}
$now = new DateTime();
$interval = $now->diff($datetime);
$intervalData = array(
$interval->y, $interval->m, $interval->d,
$interval->h, $interval->i, $interval->s,
);
$intervalFormat = array('年', '个月', '天', '小时', '分种', '秒');
foreach($intervalData as $index=>$value) {
if ($value) {
$intervalData[$index] = $value . $intervalFormat[$index];
} else {
unset($intervalData[$index]);
unset($intervalFormat[$index]);
}
}
return implode('', array_slice($intervalData, 0, $size));
}

你自己也一个 函数呀 php教程 内置的很多函数都有问题(不是错误,是适用方位窄)

for(旧数组.....){
  if(是要删除的值)
    continue
  $newArr[]=每个值
}

return $newArr

实例

Array([0] => Hello[1] => world.[2] => It's[3] => a[4] => beautiful[5] => day.)

foreach($array as $k=>$v){
  if($v == 'day'){
  unset($array[$k]):
}
}

利用foreach效率不高下在, 看php自带函数

$arr = array("Hello","world","It's","beautiful","day");

实例一

$arr = array_flip($arr);

unset($arr['world']);

$arr = array_flip($arr);

print_r($arr);

实例二

array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。

 

$arr = Array([0] => Hello[1] => world.[2] => It's[3] => a[4] => beautiful[5] => day.);

if(($key = array_search('day',$arr))){

    unset($arr[$key]);

}


实例三

array_splice() 函数与 array_slice() 函数类似,选择数组中的一系列元素,但不返回,而是删除它们并用其它值代替。

 

if(($key = array_search('day',$arr))){

    array_splice($arr, $key,1);

}

php教程获取上传文件名的文件类型

$imgname = $_FILES["file"]["name"]; //获取上传的文件名称
$filetype = pathinfo($imgname, PATHINFO_EXTENSION);//获取后缀
$newname = date("Ymdhis").".".$filetype; //构建新名称

再看

$file = 'www.111cn.net.gif';
echo getfix( $file );

//得到的值为gif 这种方法最简单也最实例了,好下面我们来看看方法二一种用substr读取扩展名方法

 

$file ='aaa.gif';
echo substr($file,strpos($file,'.')+1);

//方法三 还是用数组

$file = '111cn.gif';
$d111cn = explode('.',$file);
echo $d111cn[count($d111cn)-1];

function getfix($l1){
 return end(explode('.', $l1));
}

 

$extname=substr($upload_file_name,strpos($upload_file_name,".")+1);//获取文件扩展名

strpos() 函数返回字符串在另一个字符串中第一次出现的位置。如果没有找到该字符串,则返回 false。

 

更多详细内容请查看:http://www.111cn.net/phper/21/358ad3dd52a90fd7894a1047adc80208.htm

标签:[!--infotagslink--]

您可能感兴趣的文章: