当前位置: 编程技术>php
本页文章导读:
▪php验证码的三个实例代码分享 验证码-代码1:
<?php
//checkNum.php
session_start();
function random($len)
{
$srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand();//配置乱数种子
$strs="";
for($i=0;$i <$len;$i++){
$strs.=$srcstr[mt_rand(0,35)];
}
.........
▪php 文件缓存数据类的代码分享 说到php文件缓存,回顾之前介绍的文章,找到了这么几篇:
php 缓存类 调用示例 PHP 数据缓存的实例代码 php 页面缓存类,大家可以参考下。
有了以上对于php 文件缓存的基础,下面开始.........
▪php操作access数据库的类分享 代码如下:
<?php
/*
FileName:cls_access.php
Summary: Access数据库操作类
范例:
$databasepath='D:/wwwroot/cef/www/class/testdb.mdb';
//或者$databasepath='testdb.mdb';
$dbusername='';
$dbpassword='';
require_once('cls_.........
[1]php验证码的三个实例代码分享
来源: 互联网 发布时间: 2013-12-24
验证码-代码1:
<?php
//checkNum.php
session_start();
function random($len)
{
$srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand();//配置乱数种子
$strs="";
for($i=0;$i <$len;$i++){
$strs.=$srcstr[mt_rand(0,35)];
}
return strtoupper($strs);
}
$str=random(4); //随机生成的字符串" title="字符串" >字符串
$width = 50; //验证码图片的宽度
$height = 25; //验证码图片的高度
@header("Content-Type:image/png");
$_SESSION["code"] = $str;
//echo $str;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊点颜色
$pix=imagecolorallocate($im,187,230,247);
//字体色
$font=imagecolorallocate($im,41,163,238);
//绘模糊作用的点
mt_srand();
for($i=0;$i <1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 5,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
$_SESSION["code"] = $str;
session_destroy();
?>
验证码--代码2:
<?php
session_start();
$authnum=random(6);//验证码字符.
$_SESSION['yan']=$authnum;
//生成验证码图片
Header("Content-type: image/PNG");
$im = imagecreate(55,18); //imagecreate() 新建图像,大小为 x_size 和 y_size 的空白图像。
$red = ImageColorAllocate($im, 153,51,0); //设置背景颜色
$white = ImageColorAllocate($im, 255,204,0);//设置文字颜色
$gray = ImageColorAllocate($im, 102,102,0); //设置杂点颜色
imagefill($im,55,18,$red);
for ($i = 0; $i < strlen($authnum); $i++)
{
// $i%2 == 0?$top = -1:$top = 3;
imagestring($im, 6, 13*$i+4, 1, substr($authnum,$i,1), $white);
//int imagestring ( resource image, int font, int x, int y, string s, int col)
//imagestring() 用 col 颜色将字符串" title="字符串" >字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。如果 font 是 1,2,3,4 或
5,则使用内置字体。
}
for($i=0;$i<100;$i++) //加入干扰象素
{
imagesetpixel($im, rand()%55 , rand()%18 , $gray);
//int imagesetpixel ( resource image, int x, int y, int color)
//imagesetpixel() 在 image 图像中用 color 颜色在 x, y 坐标(图像左上角为 0, 0)上画一个点。
}
ImagePNG($im); //以 PNG 格式将图像输出到浏览器或文件
ImageDestroy($im);//销毁一图像
//产生随机数函数
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
?>
验证码--代码3:
<?php
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(62,20);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
while(($authnum=rand()%100000)<10000);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 3, $authnum, $white);
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);
$_SESSION['SESSION_VALIDATE_CODE'] = $authnum.'';
?>
您可能感兴趣的文章:php验证码简单函数代码(附效果图)
分享一个php 验证码类及调用示例
一个php验证码的封装类
php自定义大小验证码的实例代码
php生成扭曲及旋转的验证码图片的实例代码
php仿QQ验证码的实现代码
php验证码函数使用的例子
php5验证码类(简易实用型)
php验证码(GD库生成验证码)的例子
php点击验证码实时刷新的实现代码
php图片验证码的例子
php彩色验证码的简单例子
php验证码刷新与局部刷新的实现方法
php GD库生成验证码的实例
php生成验证码的例子
php随机验证码 php生成随机验证码(图文)
一个比较稳定的php登陆系统验证码
用php生成带有雪花背景的验证码
[2]php 文件缓存数据类的代码分享
来源: 互联网 发布时间: 2013-12-24
说到php文件缓存,回顾之前介绍的文章,找到了这么几篇:
php 缓存类 调用示例 PHP 数据缓存的实例代码 php 页面缓存类,大家可以参考下。
有了以上对于php 文件缓存的基础,下面开始今天的内容。
代码如下:
<?php
/**
* 文件缓存类
*
* @author 志凡
* @package cache
* @version v0.1
* edit by www.
*/
class FileCache {
/**
* @var string $cachePath 缓存文件目录
* @access public
*/
public $cachePath = './';
/**
* 构造函数
* @param string $path 缓存文件目录
*/
function __construct($path = NULL) {
if ($path) {
$this->cachePath = $path;
}
}
/**
* 析构函数
*/
function __destruct() {
//nothing
}
/**
* 在cache中设置键为$key的项的值,如果该项不存在,则新建一个项
* @param string $key 键值
* @param mix $var 值
* @param int $expire 到期秒数
* @param int $flag 标志位
* @return bool 如果成功则返回 TRUE,失败则返回 FALSE。
* @access public
*/
public function set($key, $var, $expire = 36000, $flag = 0) {
$value = serialize($var);
$timeout = time() + $expire;
$result = safe_file_put_contents($this->cachePath . urlencode($key) .'.cache',
$timeout . '<<%-==-%>>' . $value);
return $result;
}
/**
* 在cache中获取键为$key的项的值
* @param string $key 键值
* @return string 如果该项不存在,则返回false
* @access public
*/
public function get($key) {
$file = $this->cachePath . urlencode($key) .'.cache';
if (file_exists($file)) {
$content = safe_file_get_contents($file);
if ($content===false) {
return false;
}
$tmp = explode('<<%-==-%>>', $content);
$timeout = $tmp[0];
$value = $tmp[1];
if (time()>$timeout) {
$result = false;
} else {
$result = unserialize($value);
}
} else {
$result = false;
}
return $result;
}
/**
* 清空cache中所有项
* @return 如果成功则返回 TRUE,失败则返回 FALSE。
* @access public
*/
public function flush() {
$fileList = FileSystem::ls($this->cachePath,array(),'asc',true);
return FileSystem::rm($fileList);
}
/**
* 删除在cache中键为$key的项的值
* @param string $key 键值
* @return 如果成功则返回 TRUE,失败则返回 FALSE。
* @access public
*/
public function delete($key) {
return FileSystem::rm($this->cachePath . $key .'.cache');
}
}
if (!function_exists('safe_file_put_contents')) {
function safe_file_put_contents($filename, $content)
{
$fp = fopen($filename, 'wb');
if ($fp) {
flock($fp, LOCK_EX);
fwrite($fp, $content);
flock($fp, LOCK_UN);
fclose($fp);
return true;
} else {
return false;
}
}
}
if (!function_exists('safe_file_get_contents')) {
function safe_file_get_contents($filename)
{
$fp = fopen($filename, 'rb');
if ($fp) {
flock($fp, LOCK_SH);
clearstatcache();
$filesize = filesize($filename);
if ($filesize > 0) {
$data = fread($fp, $filesize);
}
flock($fp, LOCK_UN);
fclose($fp);
return $data;
} else {
return false;
}
}
}
?>
调用示例:
<?php
//php缓存类的例子
$cache = new FileCache();
$data = $cache->get('yourkey');//yourkey是你为每一个要缓存的数据定义的缓存名字
if ($data===false) {
$data = '从数据库取出的数据或很复杂很耗时的弄出来的数据';
$cache->set('yourkey',$data,3600);//缓存3600秒
}
// use your $data
?>
[3]php操作access数据库的类分享
来源: 互联网 发布时间: 2013-12-24
代码如下:
<?php
/*
FileName:cls_access.php
Summary: Access数据库操作类
范例:
$databasepath='D:/wwwroot/cef/www/class/testdb.mdb';
//或者$databasepath='testdb.mdb';
$dbusername='';
$dbpassword='';
require_once('cls_access.php');
$access=new Access($databasepath,$dbusername,$dbpassword);
print_r($access->getlist("SELECT TOP 5 * FROM `Result` WHERE `ResultNo` < 12 ORDER BY `ResultNo` DESC"));
echo $access->num_rows("update `conner` set `connerip` = '222.35.142.101' where `connerno` < 83");
*/
class Access{
var $databasepath,$constr,$dbusername,$dbpassword,$link;
function Access($databasepath,$dbusername,$dbpassword){
$this->databasepath=$databasepath;
$this->username=$dbusername;
$this->password=$dbpassword;
$this->connect();
}
function connect(){
$this->constr='DRIVER={Microsoft Access Driver (*.mdb)};DBQ='.realpath($this->databasepath).';';
$this->link=odbc_connect($this->constr,$this->username,$this->password,SQL_CUR_USE_ODBC);
if(!$this->link) echo "数据库连接失败!";
return $this->link;
}
function query($sql){ //直接运行SQL,可用于更新、删除数据
return @odbc_exec($this->link,$sql);
}
function fetch_array($query){ //取得当前指针处记录
return odbc_fetch_array($query);
}
function fetch_row($query){ //基本同fetch_array(),但只有数字索引
return odbc_fetch_row($query);
}
function num_rows($sql){ //返回SQL查询影响到的行的数目
return odbc_num_rows($this->query($sql));
}
function close(){ //关闭数据库连接函数
odbc_close($this->link);
}
function getlist($sql){ //取得记录列表
$query=$this->query($sql);
$recordlist=array();
while ($row=$this->fetch_array($query)){
$recordlist[]=$row;
}
return $recordlist;
}
}
?>
以上类中使用了ODBC,而不是ODBC数据源。
直接新建一个*.mdb文件即可使用。
附,部分常用COM方式操作Access代码:
<?php
$db = $_SERVER['DOCUMENT_ROOT'].'/zhuanti/ugdkfjlsg54356k6546jk654gfslkg6544s.mdb';
$conn = new COM('ADODB.Connection') or die('can not start Active X Data Objects');
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");
?>
JS调用页js.php
<?php
include_once('conn.php');
$rs = $conn->Execute("SELECT TOP 3 * FROM userstar where chk = 1 ORDER BY id DESC");
$idgb=1;
while(!$rs->EOF){
$gbinf[$idgb]['id'] = $rs->Fields['id']->Value;
$gbinf[$idgb]['usname'] = $rs->Fields['usname']->Value;
$gbinf[$idgb]['usage'] = $rs->Fields['usage']->Value;
$gbinf[$idgb]['usprovince'] = $rs->Fields['usprovince']->Value;
$gbinf[$idgb]['usfile'] = $rs->Fields['usfile']->Value;
$gbinf[$idgb]['thumb'] = $rs->Fields['thumb']->Value;
$gbinf[$idgb]['usmessage'] = $rs->Fields['usmessage']->Value;
$rs->MoveNext();
$idgb++;
}
$rs->Close();
$rs = null;
$uhtml='';
$uhtml.='<table border="0" align="center"><tr>';
for ($i=1; $i<4; $i++){
$uhtml.='<td width="240" align="center">';
if ($gbinf[$i]['usfile']){
$uhtml.='<a href="/index.html" target="_blank" title="'.$gbinf[$i]['usname'].'"><img src="/'.$gbinf[$i]['thumb'].'" /></a><br
/>'.$gbinf[$i]['usprovince'].' '.$gbinf[$i]['usname'];
if ($gbinf[$i]['usage'] != 0){
$uhtml.=' ('.$gbinf[$i]['usage'].'岁)<br />';
}
$uhtml.='<div ><a href="/index.html" target="_blank"
title="'.$gbinf[$i]['usmessage'].'" >'.$gbinf[$i]['usmessage'].'</a></div>';
}
$uhtml.='</td>';
}
$uhtml.='</tr></table>';
echo "document.write('".$uhtml."')";
?>
另一个例子,以本文件路径确定数据库路径:
<?php
/*
数据库连接配置
*/
$db = getcwd().'\images\90d\90dicr_os6546oftff_ic543e_cces_4535s.mdb';
$conn = new COM('ADODB.Connection') or die('can not start Active X Data Objects');
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");
//print_r($_POST);
if ($_POST['name'] && $_POST['tel'] && $_POST['addr'] && $_POST['email'] && $_POST['goal'] && $_POST['high'] &&
$_POST['weight'] && $_POST['message'])
{
$addsql = "INSERT INTO d90usr (`name`, `tel`, `addr`, `email`, `goal`, `high`, `weight`, `message`) VALUES ('".$_POST['name']."',
'".$_POST['tel']."',
'".$_POST['addr']."', '".$_POST['email']."', '".$_POST['goal']."', '".$_POST['high']."', '".$_POST['weight']."',
'".strip_tags($_POST['message'])."')";
//echo '<hr />'.$addsql;
$rsadd = $conn->Execute($addsql);
echo '<script type="text/javascript">alert("报名成功,请等待工作人员与您联系!");</script>';
echo '<p align="center"><h1>恭喜您报名成功,请等待工作人员与您联系!</h1></p>';
echo '<p align="center"><a href="/blog_article/90d.html">返回活动首页</a> <a href="/">返回商城首页</a></p>';
}
?>
您可能感兴趣的文章:
php连接access数据库的三种方法
php ADO组件连接access数据库的例子
最新技术文章: