php md5加密函数参考详解

 更新时间:2016年11月25日 15:24  点击:2094
很多php开发者都会使用这个md()加密函数,但其中的参考有些朋友未必知道了,今天我们来看看md5()函数的参数吧.

语法
md5(string,raw)

md5() 函数计算字符串的 MD5 散列


string 必需。规定要计算的字符串

charlist

可选。规定十六进制或二进制输出格式:
TRUE - 原始 16 字符二进制格式
FALSE - 默认。32 字符十六进制数
注释:该参数是 PHP 5.0 中添加的。
 

 代码如下 复制代码

<?
$str ="123456";
echo 'md5 16位'.md5($str,true);

//输出乱码

//如果你想得到php5中md5( "abc ",   true)这样的返回值,那么可以:
function   bin_md5($val)   {
    return   pack( "H32 ",md5($val));
}

 

echo '<br />md5 32位'.md5($str);

//10adc3949ba59abbe56e057f20f883e
?>

一款国外网站下载的php生成验证码图片代码,这个比较实用我们还举例说明了,有需要的朋友按上面保存成php就可以用了哦。
 代码如下 复制代码

<?php
session_start();

if( isset($_POST['submit'])) {
   if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
  // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
  echo 'Thank you. Your message said "'.$_POST['message'].'"';
  unset($_SESSION['security_code']);
   } else {
  // Insert your code for showing an error message here
  echo 'Sorry, you have provided an invalid security code';
   }
} else {
?>

 <form action="form.php" method="post">
  <label for="name">Name: </label><input type="text" name="name" id="name" /><br />
  <label for="email">Email: </label><input type="text" name="email" id="email" /><br />
  <label for="message">Message: </label><textarea rows="5" cols="30" name="message" id="message"></textarea><br />
  <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
  <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br />
  <input type="submit" name="submit" value="Submit" />
 </form>

<?php
 }
?>

验证程序 CaptchaSecurityImages.php

 代码如下 复制代码

<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class CaptchaSecurityImages {

 var $font = 'monofont.ttf';

 function generateCode($characters) {
  /* list all possible characters, similar looking characters and vowels have been removed */
  $possible = '23456789bcdfghjkmnpqrstvwxyz';
  $code = '';
  $i = 0;
  while ($i < $characters) {
   $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
   $i++;
  }
  return $code;
 }

 function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
  $code = $this->generateCode($characters);
  /* font size will be 75% of the image height */
  $font_size = $height * 0.75;
  $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  /* set the colours */
  $background_color = imagecolorallocate($image, 255, 255, 255);
  $text_color = imagecolorallocate($image, 20, 40, 100);
  $noise_color = imagecolorallocate($image, 100, 120, 180);
  /* generate random dots in background */
  for( $i=0; $i<($width*$height)/3; $i++ ) {
   imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  }
  /* generate random lines in background */
  for( $i=0; $i<($width*$height)/150; $i++ ) {
   imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  }
  /* create textbox and add text */
  $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
  $x = ($width - $textbox[4])/2;
  $y = ($height - $textbox[5])/2;
  imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
  /* output captcha image to browser */
  header('Content-Type: image/jpeg');
  imagejpeg($image);
  imagedestroy($image);
  $_SESSION['security_code'] = $code;
 }

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>

自己写的一款sql注入检测函数,可以有效的检测用户post,get过来的参考进行过滤,有需要的朋友参考一下。
 代码如下 复制代码

<?php
/*sql 注入 字符的检测
* 在所有用户输入的数据,post传参, get传参 都需要检测下 
* 如果有匹配到关键字 则 返回该关键字  否则返回false
* 这个和敏感字符的检测不是一样的
*/
function Filter_SQL($strData)
{
 $strFilter=$blnFlag=$arrayFilter='';

 $strFilter="'|and|(|)|exec|insert|select|delete|update|count|*|%27|chr|mid|master|truncate|char|declare|union|or"; //需要过滤的字符,可以自己添,"|"是分隔符
 $blnFlag=false;   //过滤标志,如果产生过滤,那么就是真
 
 $arr=explode("|",$strFilter); 
 $str="";
 
 foreach($arr as $row)
 {
  $str.=preg_quote($row)."|";
 } 
 
 $str=trim($str,"|"); 
 
 if(preg_match('/'.$str.'/i',$strData,$word))
 {
  return $word[0];
 }
 
 return false;
}

/*
测试
$string="fasdf union ";
echo Filter_SQL($string);
*/

?>

这里介绍了一款安全性比较高的验证生成程序,可以带干扰线等内容,可以有效的防止用户用程序识别验证码的难度了。
 代码如下 复制代码

<?php教程
/*
 * Created on 2011-3-11
 * Programmer : xiaoyao, QQ:1045195056
 验证通过判断输入值与$_SESSION['check_pic']值
 */
session_start();
 function RandAscii($number){//$number产生数字和字母个数
$arr=array('0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','w','v','u','x','y','z');
for ($i=1;$i<=$number;$i++)
{
$rand= $rand.$arr[rand(0,35)];
}
return $rand;
}
 $rand=RandAscii(4);

$_SESSION['check_pic']=$rand;//随机产生的四个数赋值session中,用于验证。
$x=80;
$y=24;
 $im=imagecreatetruecolor($x,$y);//创建图片
$bg=imagecolorallocate($im,255,255,255);//设置颜色背景
imagefill( $im,0,0,$bg);
$wh=imagecolorallocate($im,255,255,0);
$grey=imagecolorallocate($im,128,128,128);
$yellow=imagecolorallocate($im,255,255,0);
$red=imagecolorallocate($im,0,255,0);
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
  imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
  imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
  imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
 );//字颜色数组
 //画边框
 $border = imagecolorallocate($im, 133, 153, 193);
 imagerectangle($im, 0, 0, $x - 1, $y - 1, $border);

for($i=0;$i<10;$i++){        //画干扰线,10条
imageline($im,rand(0,60),2,rand(0,60),20,$yellow);

}
for($j=0;$j<100;$j++){
 imagesetpixel($im,rand()%76,rand()%20,$red);
}
//imagestring($im,6,15,8,$rand,$wh);//字体大小1-5
imagettftext($im, 14,rand(30, -30), 5, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[0]);
imagettftext($im, 14,rand(50, -50), 24, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[1]);
imagettftext($im, 14,rand(50, -50), 43, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[2]);
imagettftext($im, 14,rand(30, -30), 62, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[3]);

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

调用方法

 代码如下 复制代码
<?php
/*
 * Created on 2011-3-11
 * Programmer : xiaoyao, QQ:1045195056
 验证通过判断输入值与$_SESSION['check_pic']值
 */
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 src="index.php"><br>    <!----链接图片--->
<input type="text" name="check" >
<input type="submit" value="提交">
</FORM>

 

利用php自身带的函数来实现图片验证码生成功能,有需要的朋友可以参考一下。
<?php教程
//must start or continue session and save CAPTCHA string in $_SESSION for
//it to be available to other requests
if(!isset($_SESSION)){
session_start();
header('Cache-control:private');
}
//create a 65*20 pixel image
$width=65;
$height=20;
$image=imagecreate(65,20);
//fill the image background color
$bg_color=imagecolorallocate($image,0x33,0x66,0xFF);
imagefilledrectangle($image,0,0,$width,$height,$bg_color);
//fetch random text
$text=random_text(5);
//determine x and y coordinates for centering text
$font=5;
$x=imagesx($image)/2-strlen($text)*imagefontwidth($font)/2;
$y=imagesy($image)/2-imagefontheight($font)/2;
//write text on image
$fg_color=imagecolorallocate($image,0xFF,0xFF,0xFF);
imagestring($image,$font,$x,$y,$text,$fg_color);
//save the CAPTCHA string for later comparison
$_SESSION['captcha']=$text;
//output the image
header('Content-type:image/png');
imagepng($image);
imagedestroy($image);
?>
[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • node.JS md5加密中文与php结果不一致怎么办

    这次文章要给大家介绍的是node.JS md5加密中文与php结果不一致怎么办,不知道具体解决办法的下面跟小编一起来看看。 因项目需要,需要Node.js与PHP做接口调用,发现nod...2017-07-06
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
  • SQL Server中row_number函数的常见用法示例详解

    这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • PHP加密解密函数详解

    分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
  • php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法

    最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在。 1.使用 PEAR 的...2015-10-21
  • C#中加载dll并调用其函数的实现方法

    下面小编就为大家带来一篇C#中加载dll并调用其函数的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#虚函数用法实例分析

    这篇文章主要介绍了C#虚函数用法,实例分析了C#中虚函数的功能与基本使用技巧,需要的朋友可以参考下...2020-06-25