当前位置: 编程技术>php
本页文章导读:
▪PHP验证码类代码( 最新修改,完全定制化! )
Authnum.class.php 下载 代码如下: <?php session_start(); class Authnum { //图片对象、宽度、高度、验证码长度 private $im; private $im_width; private $im_height; private $len; //随机字符串、y轴坐标值、随机颜色 p.........
▪PHP项目开发中最常用的自定义函数整理
<?php //alert提示 function alert($msg){ echo "<script>alert('$msg');</script>"; } //把一些预定义的字符转换为 HTML 实体 function d_htmlspecialchars($string) { if(is_array($string)) { foreach($string as $key => $val) .........
▪PHP自动选择 连接本地还是远程数据库
Mysql.class.php 文件见 http://www./article/25496.htm 代码如下: <?php // 包含Mysql操作类 include_once 'Mysql.class.php'; // 本地mysql数据 $mysql_local_data = array('db_host'=>'localhost', 'db_user'=>'root', 'db_pass'=>'roo.........
[1]PHP验证码类代码( 最新修改,完全定制化! )
来源: 互联网 发布时间: 2013-11-30
Authnum.class.php 下载
<?php
session_start();
class Authnum {
//图片对象、宽度、高度、验证码长度
private $im;
private $im_width;
private $im_height;
private $len;
//随机字符串、y轴坐标值、随机颜色
private $randnum;
private $y;
private $randcolor;
//背景色的红绿蓝,默认是浅灰色
public $red=238;
public $green=238;
public $blue=238;
/**
* 可选设置:验证码类型、干扰点、干扰线、Y轴随机
* 设为 false 表示不启用
**/
//默认是大小写数字混合型,1 2 3 分别表示 小写、大写、数字型
public $ext_num_type='';
public $ext_pixel = false; //干扰点
public $ext_line = false; //干扰线
public $ext_rand_y= true; //Y轴随机
function __construct ($len=4,$im_width='',$im_height=25) {
// 验证码长度、图片宽度、高度是实例化类时必需的数据
$this->len = $len; $im_width = $len * 15;
$this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
// 设置图片背景颜色,默认是浅灰色背景
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// 获得任意位数的随机码
function get_randnum () {
$an1 = 'abcdefghijklmnopqrstuvwxyz';
$an2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$an3 = '0123456789';
if ($this->ext_num_type == '') $str = $an1.$an2.$an3;
if ($this->ext_num_type == 1) $str = $an1;
if ($this->ext_num_type == 2) $str = $an2;
if ($this->ext_num_type == 3) $str = $an3;
for ($i = 0; $i < $this->len; $i++) {
$start = rand(1,strlen($str) - 1);
$randnum .= substr($str,$start,1);
}
$this->randnum = $randnum;
$_SESSION[an] = $this->randnum;
}
// 获得验证码图片Y轴
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
else $this->y = $this->im_height / 4 ;
}
// 获得随机色
function get_randcolor () {
$this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
}
// 添加干扰点
function set_ext_pixel () {
if ($this->ext_pixel) {
for($i = 0; $i < 100; $i++){
$this->get_randcolor();
imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);
}
}
}
// 添加干扰线
function set_ext_line () {
if ($this->ext_line) {
for($j = 0; $j < 2; $j++){
$rand_x = rand(2, $this->im_width);
$rand_y = rand(2, $this->im_height);
$rand_x2 = rand(2, $this->im_width);
$rand_y2 = rand(2, $this->im_height);
$this->get_randcolor();
imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor);
}
}
}
/**创建验证码图像:
* 建立画布(__construct函数)
* 设置画布背景($this->set_bgcolor();)
* 获取随机字符串($this->get_randnum ();)
* 文字写到图片上(imagestring函数)
* 添加干扰点/线($this->set_ext_line(); $this->set_ext_pixel();)
* 输出图片
**/
function create () {
$this->set_bgcolor();
$this->get_randnum ();
for($i = 0; $i < $this->len; $i++){
$font = rand(4,6);
$x = $i/$this->len * $this->im_width + rand(1, $this->len);
$this->get_y();
$this->get_randcolor();
imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor);
}
$this->set_ext_line();
$this->set_ext_pixel();
header("content-type:image/png");
imagepng($this->im);
imagedestroy($this->im); //释放图像资源
}
}//end class
/**使用验证码类的方法:
* $an = new Authnum(验证码长度,图片宽度,图片高度);
* 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片
* 表单页面检测验证码的方法,对比 $_SESSION[an] 是否等于 $_POST[验证码文本框ID]
* 可选配置:
* 1.验证码类型:$an->ext_num_type=1; 值为1是小写类型,2是大写类型,3是数字类型
* 2.干扰点:$an->ext_pixel = false; 值为false表示不添加干扰点
* 3.干扰线:$an->ext_line = false; 值为false表示不添加干扰线
* 4.Y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片Y轴随机
* 5.图片背景:改变 $red $green $blue 三个成员变量的值即可
**/
$an = new Authnum();
$an->ext_num_type='';
$an->ext_pixel = true; //干扰点
$an->ext_line = false; //干扰线
$an->ext_rand_y= true; //Y轴随机
$an->green = 238;
$an->create();
?>
代码如下:
<?php
session_start();
class Authnum {
//图片对象、宽度、高度、验证码长度
private $im;
private $im_width;
private $im_height;
private $len;
//随机字符串、y轴坐标值、随机颜色
private $randnum;
private $y;
private $randcolor;
//背景色的红绿蓝,默认是浅灰色
public $red=238;
public $green=238;
public $blue=238;
/**
* 可选设置:验证码类型、干扰点、干扰线、Y轴随机
* 设为 false 表示不启用
**/
//默认是大小写数字混合型,1 2 3 分别表示 小写、大写、数字型
public $ext_num_type='';
public $ext_pixel = false; //干扰点
public $ext_line = false; //干扰线
public $ext_rand_y= true; //Y轴随机
function __construct ($len=4,$im_width='',$im_height=25) {
// 验证码长度、图片宽度、高度是实例化类时必需的数据
$this->len = $len; $im_width = $len * 15;
$this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
// 设置图片背景颜色,默认是浅灰色背景
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// 获得任意位数的随机码
function get_randnum () {
$an1 = 'abcdefghijklmnopqrstuvwxyz';
$an2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$an3 = '0123456789';
if ($this->ext_num_type == '') $str = $an1.$an2.$an3;
if ($this->ext_num_type == 1) $str = $an1;
if ($this->ext_num_type == 2) $str = $an2;
if ($this->ext_num_type == 3) $str = $an3;
for ($i = 0; $i < $this->len; $i++) {
$start = rand(1,strlen($str) - 1);
$randnum .= substr($str,$start,1);
}
$this->randnum = $randnum;
$_SESSION[an] = $this->randnum;
}
// 获得验证码图片Y轴
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
else $this->y = $this->im_height / 4 ;
}
// 获得随机色
function get_randcolor () {
$this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
}
// 添加干扰点
function set_ext_pixel () {
if ($this->ext_pixel) {
for($i = 0; $i < 100; $i++){
$this->get_randcolor();
imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);
}
}
}
// 添加干扰线
function set_ext_line () {
if ($this->ext_line) {
for($j = 0; $j < 2; $j++){
$rand_x = rand(2, $this->im_width);
$rand_y = rand(2, $this->im_height);
$rand_x2 = rand(2, $this->im_width);
$rand_y2 = rand(2, $this->im_height);
$this->get_randcolor();
imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor);
}
}
}
/**创建验证码图像:
* 建立画布(__construct函数)
* 设置画布背景($this->set_bgcolor();)
* 获取随机字符串($this->get_randnum ();)
* 文字写到图片上(imagestring函数)
* 添加干扰点/线($this->set_ext_line(); $this->set_ext_pixel();)
* 输出图片
**/
function create () {
$this->set_bgcolor();
$this->get_randnum ();
for($i = 0; $i < $this->len; $i++){
$font = rand(4,6);
$x = $i/$this->len * $this->im_width + rand(1, $this->len);
$this->get_y();
$this->get_randcolor();
imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor);
}
$this->set_ext_line();
$this->set_ext_pixel();
header("content-type:image/png");
imagepng($this->im);
imagedestroy($this->im); //释放图像资源
}
}//end class
/**使用验证码类的方法:
* $an = new Authnum(验证码长度,图片宽度,图片高度);
* 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片
* 表单页面检测验证码的方法,对比 $_SESSION[an] 是否等于 $_POST[验证码文本框ID]
* 可选配置:
* 1.验证码类型:$an->ext_num_type=1; 值为1是小写类型,2是大写类型,3是数字类型
* 2.干扰点:$an->ext_pixel = false; 值为false表示不添加干扰点
* 3.干扰线:$an->ext_line = false; 值为false表示不添加干扰线
* 4.Y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片Y轴随机
* 5.图片背景:改变 $red $green $blue 三个成员变量的值即可
**/
$an = new Authnum();
$an->ext_num_type='';
$an->ext_pixel = true; //干扰点
$an->ext_line = false; //干扰线
$an->ext_rand_y= true; //Y轴随机
$an->green = 238;
$an->create();
?>
[2]PHP项目开发中最常用的自定义函数整理
来源: 互联网 发布时间: 2013-11-30
<?php
//alert提示
function alert($msg){
echo "<script>alert('$msg');</script>";
}
//把一些预定义的字符转换为 HTML 实体
function d_htmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = d_htmlspecialchars($val);
}
} else {
$string = str_replace('&', '&', $string);
$string = str_replace('"', '"', $string);
$string = str_replace(''', ''', $string);
$string = str_replace('<', '<', $string);
$string = str_replace('>', '>', $string);
$string = preg_replace('/&(#\d;)/', '&\1', $string);
}
return $string;
}
//在预定义字符前加上反斜杠,包括 单引号、双引号、反斜杠、NULL,以保护数据库安全
function d_addslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) $string[$key] = d_addslashes($val, $force);
}
else $string = addslashes($string);
}
return $string;
}
//生成随机字符串,包含大写、小写字母、数字
function randstr($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
//转换时间戳为常用的日期格式
function trans_time($timestamp){
if($timestamp < 1) echo '无效的Unix时间戳';
else return date("Y-m-d H:i:s",$timestamp);
}
//获取IP
function get_ip() {
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if ($_SERVER["HTTP_CLIENT_IP"])
$ip = $_SERVER["HTTP_CLIENT_IP"];
else if ($_SERVER["REMOTE_ADDR"])
$ip = $_SERVER["REMOTE_ADDR"];
else if (getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "Unknown";
return $ip;
}
//计算时间差:默认返回类型为“分钟”
//$old_time 只能是时间戳,$return_type 为 h 是小时,为 s 是秒
function timelag($old_time,$return_type='m'){
if($old_time < 1){
echo '无效的Unix时间戳';
}else{
switch($return_type){
case 'h':
$type = 3600; break;
case 'm':
$type = 60; break;
case 's':
$type = 1; break;
case '':
$type = 60; break;
}
$dif = round( (time()-$old_time)/$type ) ;
return $dif;
}
}
//获取当前页面的URL地址
function url_this(){
$url = "http://".$_SERVER ["HTTP_HOST"].$_SERVER["REQUEST_URI"];
$return_url = "<a href='/blog_article/$url/index.html'>$url</a>";
return $return_url;
}
//跳转函数
function url_redirect($url,$delay=''){
if($delay == ''){
echo "<script>window.location.href='/blog_article/$url/index.html'</script>";
}else{
echo "<meta http-equiv='refresh' content='$delay;URL=$url' />";
}
}
} //end func
?>
//alert提示
function alert($msg){
echo "<script>alert('$msg');</script>";
}
//把一些预定义的字符转换为 HTML 实体
function d_htmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = d_htmlspecialchars($val);
}
} else {
$string = str_replace('&', '&', $string);
$string = str_replace('"', '"', $string);
$string = str_replace(''', ''', $string);
$string = str_replace('<', '<', $string);
$string = str_replace('>', '>', $string);
$string = preg_replace('/&(#\d;)/', '&\1', $string);
}
return $string;
}
//在预定义字符前加上反斜杠,包括 单引号、双引号、反斜杠、NULL,以保护数据库安全
function d_addslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) $string[$key] = d_addslashes($val, $force);
}
else $string = addslashes($string);
}
return $string;
}
//生成随机字符串,包含大写、小写字母、数字
function randstr($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
//转换时间戳为常用的日期格式
function trans_time($timestamp){
if($timestamp < 1) echo '无效的Unix时间戳';
else return date("Y-m-d H:i:s",$timestamp);
}
//获取IP
function get_ip() {
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if ($_SERVER["HTTP_CLIENT_IP"])
$ip = $_SERVER["HTTP_CLIENT_IP"];
else if ($_SERVER["REMOTE_ADDR"])
$ip = $_SERVER["REMOTE_ADDR"];
else if (getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "Unknown";
return $ip;
}
//计算时间差:默认返回类型为“分钟”
//$old_time 只能是时间戳,$return_type 为 h 是小时,为 s 是秒
function timelag($old_time,$return_type='m'){
if($old_time < 1){
echo '无效的Unix时间戳';
}else{
switch($return_type){
case 'h':
$type = 3600; break;
case 'm':
$type = 60; break;
case 's':
$type = 1; break;
case '':
$type = 60; break;
}
$dif = round( (time()-$old_time)/$type ) ;
return $dif;
}
}
//获取当前页面的URL地址
function url_this(){
$url = "http://".$_SERVER ["HTTP_HOST"].$_SERVER["REQUEST_URI"];
$return_url = "<a href='/blog_article/$url/index.html'>$url</a>";
return $return_url;
}
//跳转函数
function url_redirect($url,$delay=''){
if($delay == ''){
echo "<script>window.location.href='/blog_article/$url/index.html'</script>";
}else{
echo "<meta http-equiv='refresh' content='$delay;URL=$url' />";
}
}
} //end func
?>
[3]PHP自动选择 连接本地还是远程数据库
来源: 互联网 发布时间: 2013-11-30
Mysql.class.php 文件见 http://www./article/25496.htm
<?php
// 包含Mysql操作类
include_once 'Mysql.class.php';
// 本地mysql数据
$mysql_local_data = array('db_host'=>'localhost',
'db_user'=>'root',
'db_pass'=>'root',
'db_name'=>'test');
// 远程mysql数据
$mysql_remote_data = array('db_host'=>'61.183.41.178',
'db_user'=>'XXX',
'db_pass'=>'XXX',
'db_name'=>'XXX');
// 公用数据
$tb_prefix = 'php95_';
$db_charset = 'UTF-8';
//本地连接成功则实例化本地Mysql类,否则连接远程数据库并实例化Mysql类
if (@mysql_connect($mysql_local_data[db_host], $mysql_local_data[db_user], $mysql_local_data[db_pass]))
$db = new Mysql($db_host, $mysql_local_data[db_user], $mysql_local_data[db_pass], $mysql_local_data[db_name], $db_charset, $conn);
else
$db = new Mysql($mysql_remote_data[db_host], $mysql_remote_data[db_user], $mysql_remote_data[db_pass], $mysql_remote_data[db_name], $db_charset, $conn);
$db->show_tables(); //测试:显示当前数据库下的所有表名
?>
代码如下:
<?php
// 包含Mysql操作类
include_once 'Mysql.class.php';
// 本地mysql数据
$mysql_local_data = array('db_host'=>'localhost',
'db_user'=>'root',
'db_pass'=>'root',
'db_name'=>'test');
// 远程mysql数据
$mysql_remote_data = array('db_host'=>'61.183.41.178',
'db_user'=>'XXX',
'db_pass'=>'XXX',
'db_name'=>'XXX');
// 公用数据
$tb_prefix = 'php95_';
$db_charset = 'UTF-8';
//本地连接成功则实例化本地Mysql类,否则连接远程数据库并实例化Mysql类
if (@mysql_connect($mysql_local_data[db_host], $mysql_local_data[db_user], $mysql_local_data[db_pass]))
$db = new Mysql($db_host, $mysql_local_data[db_user], $mysql_local_data[db_pass], $mysql_local_data[db_name], $db_charset, $conn);
else
$db = new Mysql($mysql_remote_data[db_host], $mysql_remote_data[db_user], $mysql_remote_data[db_pass], $mysql_remote_data[db_name], $db_charset, $conn);
$db->show_tables(); //测试:显示当前数据库下的所有表名
?>
最新技术文章: