在php中我们要做伪静态可以直接利用php来处理url,但这种伪静态不是标准的可以说看起来像水货一样的,只是找个心里舒服了,当然如果要实现真正的伪静态我们可以使用apache Rewrite伪静态模块来实例,下面大家来看看。
php程序实现伪静态url如下。
代码如下 |
复制代码 |
<?php
//伪静态方法一
// localhost/php100/test.php?id|1@action|2
$Php2Html_FileUrl = $_SERVER["REQUEST_URI"];
echo $Php2Html_FileUrl."<br>";// /php100/test.php?id|1@action|2
$Php2Html_UrlString = str_replace("?","",str_replace("/", "", strrchr(strrchr($Php2Html_FileUrl, "/"),"?")));
echo $Php2Html_UrlString."<br>";// id|1@action|2
$Php2Html_UrlQueryStrList = explode("@", $Php2Html_UrlString);
print_r($Php2Html_UrlQueryStrList);// Array ( [0] => id|1 [1] => action|2 )
echo "<br>";
foreach($Php2Html_UrlQueryStrList as $Php2Html_UrlQueryStr)
{
$Php2Html_TmpArray = explode("|", $Php2Html_UrlQueryStr);
print_r($Php2Html_TmpArray);// Array ( [0] => id [1] => 1 ) ; Array ( [0] => action [1] => 2 )
echo "<br>";
$_GET[$Php2Html_TmpArray[0]] = $Php2Html_TmpArray[1];
}
//echo '假静态:$_GET变量<br />';
print_r($_GET); // Array ( [id|1@action|2] => [id] => 1 [action] => 2 )
echo "<br>";
echo "<hr>";
echo $_GET[id]."<br>";// 1
echo $_GET[action];// 2
?>
<?php
//伪静态方法二
// localhost/php100/test.php/1/2
$filename = basename($_SERVER['SCRIPT_NAME']);
echo $_SERVER['SCRIPT_NAME']."<br>";// /php100/test.php
echo $filename."<br>";// test.php
if(strtolower($filename)=='test.php'){
if(!empty($_GET[id])){
$id=intval($_GET[id]);
echo $id."<br>";
$action=intval($_GET[action]);
echo $action."<br>";
}else{
$nav=$_SERVER['REQUEST_URI'];
echo "1:".$nav."<br>";// /php100/test.php/1/2
$script=$_SERVER['SCRIPT_NAME'];
echo "2:".$script."<br>";// /php100/test.php
$nav=ereg_replace("^$script","",urldecode($nav));
echo $nav."<br>"; // /1/2
$vars=explode("/",$nav);
print_r($vars);// Array ( [0] => [1] => 1 [2] => 2 )
echo "<br>";
$id=intval($vars[1]);
$action=intval($vars[2]);
}
echo $id.'&'.$action;
}
?>
<?php
//伪静态方法三
function mod_rewrite(){
global $_GET;
$nav=$_SERVER["REQUEST_URI"];
echo $nav."<br>";
$script_name=$_SERVER["SCRIPT_NAME"];
echo $script_name."<br>";
$nav=substr(ereg_replace("^$script_name","",urldecode($nav)),1);
echo $nav."<br>";
$nav=preg_replace("/^.ht(m){1}(l){0,1}$/","",$nav);//这句是去掉尾部的.html或.htm
echo $nav."<br>";
$vars = explode("/",$nav);
print_r($vars);
echo "<br>";
for($i=0;$i<Count($vars);$i+=2){
$_GET["$vars[$i]"]=$vars[$i+1];
}
return $_GET;
}
mod_rewrite();
$year=$_GET["year"];//结果为'2006'
echo $year."<br>";
$action=$_GET["action"];//结果为'_add'
echo $action;
?>
<?php
//伪静态方法四
//利用server变量 取得PATH_INFO信息 该例中为 /1,100,8630.html 也就是执行脚本名后面的部分
if(@$path_info =$_SERVER["PATH_INFO"]){
//正则匹配一下参数
if(preg_match("//(d+),(d+),(d+).html/si",$path_info,$arr_path)){
$gid =intval($arr_path[1]); //取得值 1
$sid =intval($arr_path[2]); //取得值100
$softid =intval($arr_path[3]); //取得值8630
}else die("Path:Error!");
//相当于soft.php?gid=1&sid=100&softid=8630
}else die('Path:Nothing!');
?>
|
如果你有服务器权限我觉得还是使用apache%CE%B1%BE%B2%CC%AC/" target="_blank">apache伪静态
一、Apache配置:
进入/etc/httpd/conf/目录下,打开httpd.conf文件。
启用rewrite
# LoadModule rewrite_module modules/mod_rewrite.so 去除前面的 #
启用.htaccess
AllowOverride None 修改为: AllowOverride All
二、Rewrite写法
服务器有配置文件不可能由我们来改,所以大多情况下要在网站的根目录下建一个.htaccess文件。
代码如下 |
复制代码 |
RewriteEngine on //启动rewrite引擎
RewriteRule ^/index([0-9]*).html$ /index.php?id=$1 //“([0-9]*)” 代表范围 用(.*)代表所有,下同。
RewriteRule ^/index([0-9]*)/$ /index.php?id=$1 [R] //虚拟目录 |
三、mod_rewrite 规则修正符
1) R 强制外部重定向
2) F 禁用URL,返回403HTTP状态码。
3) G 强制URL为GONE,返回410HTTP状态码。
4) P 强制使用代理转发。
5) L 表明当前规则是最后一条规则,停止分析以后规则的重写。
6) N 重新从第一条规则开始运行重写过程。
7) C 与下一条规则关联
如果规则匹配则正常处理,以下修正符无效
8) T=MIME-type(force MIME type) 强制MIME类型
9) NS 只用于不是内部子请求
10) NC 不区分大小写
11) QSA 追加请求字符串
12) NE 不在输出转义特殊字符 %3d$1 等价于 =$1
前面有讲过很多关于php curl函数来实现post提交数据,下面我来给大家介绍一种提交xml一种提交表单数据了。
例1
CURL使用POST提交XML数据
代码如下 |
复制代码 |
$url = "http://www.111cn.net";
<!--?xml version="1.0"?-->
$ch = curl_init();
$header[] = "Content-type: text/xml";//定义content-type为xml
curl_setopt($ch, CURLOPT_URL, $url); //定义表单提交地址
curl_setopt($ch, CURLOPT_POST, 1); //定义提交类型 1:POST ;0:GET
curl_setopt($ch, CURLOPT_HEADER, 1); //定义是否显示状态头 1:显示 ; 0:不显示
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定义请求类型
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//定义是否直接输出返回流
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据,这里是XML文件
curl_close($ch);//关闭
|
在PHP中CURL使用POST提交XML数据时,一定要定义content-type为xml,要不然默认是text/html!
例2,post表单数据
curl是利用URL语法在命令行方式下工作的文件传输工具。
php教程实例:
代码如下 |
复制代码 |
<?php
set_time_limit(0);
@date_default_timezone_set('Asia/Shanghai');
function curlrequest($url,$postfield,$proxy=""){
$proxy=trim($proxy);
$user_agent ="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
$ch = curl_init(); // 初始化CURL句柄
if(!empty($proxy)){
curl_setopt ($ch, CURLOPT_PROXY, $proxy);//设置代理服务器
}
curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
//curl_setopt($ch, CURLOPT_FAILONERROR, 1); // 启用时显示HTTP状态码,默认行为是忽略编号小于等于400的HTTP信息
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//启用时会将服务器服务器返回的“Location:”放在header中递归的返回给服务器
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);// 设为TRUE把curl_exec()结果转化为字串,而不是直接输出
curl_setopt($ch, CURLOPT_POST, 1);//启用POST提交
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield); //设置POST提交的字符串
//curl_setopt($ch, CURLOPT_PORT, 80); //设置端口
curl_setopt($ch, CURLOPT_TIMEOUT, 25); // 超时时间
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);//HTTP请求User-Agent:头
//curl_setopt($ch,CURLOPT_HEADER,1);//设为TRUE在输出中包含头信息
//$fp = fopen("example_homepage.txt", "w");//输出文件
//curl_setopt($ch, CURLOPT_FILE, $fp);//设置输出文件的位置,值是一个资源类型,默认为STDOUT (浏览器)。
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
'Accept-Language: zh-cn',
'Connection: Keep-Alive',
'Cache-Control: no-cache'
));//设置HTTP头信息
$document = curl_exec($ch); //执行预定义的CURL
$info=curl_getinfo($ch); //得到返回信息的特性
//print_r($info);
if($info[http_code]=="405"){
echo "bad proxy {$proxy}n"; //代理出错
exit;
}
//curl_close($ch);
return $document;
}
//请求URL
$url="http://example.cn/getInfo.php";
//POST提交数据,可用HTTPWATCH查看
$postfield="userName=test&year=2008&passWord=123456&Submit=%CC%E1%BD%BB";
//代理服务器
$proxy = '';
//请求
$str=curlrequest($url,$postfield,$proxy);
//输出结果
echo $str; |
例3,一个简单利用curl post登录实例
模拟post登陆提交表单问题
SOOPY类:
之前写过一个程序是模拟post来推送一些资源
起初跟大家一样,各种百度谷歌,首先想到的就是用PHP自带的库CURL这个来模拟
自己想偷偷懒看有没有更简单的类来实现呢?
还是被我发现了,他就是snoopy类。(中文名史卢比)
代码如下 |
复制代码 |
//首先要引用这个类
include("/data/tools/pooy/Snoopy/Snoopy.class.php");
$snoopy = new Snoopy;
//$Parameters这个是要提交的数组
$Parameters["username"] = "user";
$Parameters["pass"] = "pass";
$file = "/test/test.jpg";
$serviceUrl = "http://www.你的地址/fileProcess.php";
$postfiles["image"] = $file; //$filename上传文件相对路径 例如"upload/taoav.jpg";image/jpg
$snoopy->_submit_type = "multipart/form-data"; //设定submit类型
$snoopy->submit($serviceUrl,$Parameters,$postfiles);
|
//$postforms,$postfiles为2中类型的值,其中$postfiles为上传文件数组
上面这个例子就是实现了一个POST表单提交的案例。由于需求比较复杂,这个snoopy的功能不能满足于我的需求,于是又开始
去进攻CURL。
CURL扩展库:
这个库是比较成熟的一个扩展库,功能很强大。强大到可以模拟浏览器的任何一个动作。
需求是这样子的:
第一登陆一个网站后台
第二接口页面,然后开始推送大量资源
(这里面的具体逻辑就缩略了)
为了操作方便,我把我需要模拟的几个函数封装到了一个类里面,简短代码如下:
代码如下 |
复制代码 |
/*
模拟资源推送类
2012-09-14 by POOY
*/
class TuisongPost{
//用构造登陆认证
function TuisongPost(){
//存放COOKIE的文件
global $cookie_jar;
$this->cookie_jar = tempnam('./tmp','cookie');
$url = "http://www.你的地址";
$post_data = array( "username" => "admin","password" => "admin" );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_jar); //保存cookie信息
$output1 = curl_exec($ch);
curl_close($ch);
//echo $this->cookie_jar."n";
}
/*得到组ID*/
function getGid($groupname,$channel,$lanmu){
$url = "http://XXXX.com/creategroup";
//格式化要推送的数据
$data = $this->getGidArr($groupname,$channel,$lanmu);
$ch = curl_init();
$Ref_url = "http://www.你的地址";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $Ref_url); //伪装REFERER
curl_setopt($ch, CURLOPT_POST, 1); //post方式提交数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回数据,而不是直接输出
curl_setopt($ch, CURLOPT_HEADER, 0); // 设置是否显示header信息 0是不显示,1是显示 默认为0
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //发送cookie文件
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //发送POST数据
$output2 = curl_exec($ch); //发送HTTP请求
//这个返回值是用作判断的依据
return $output2;
curl_close($ch);
//$this->unlink($this->cookie_jar);
}
//推送数据
function sendPic($note,$groupid,$groupindex,$img){
$url = "http://XXXX/addimage";
$groupid = intval($groupid);
$data = $this->sendPicArr($note,$groupid,$groupindex,$img);
$ch = curl_init();
$Ref_url = "http://www.你的地址";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $Ref_url); //伪装REFERER
curl_setopt($ch, CURLOPT_POST, 1); //post方式提交数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回数据,而不是直接输出
curl_setopt($ch, CURLOPT_HEADER, 0); // 设置是否显示header信息 0是不显示,1是显示 默认为0
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //发送cookie文件
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //发送POST数据
$output2 = curl_exec($ch); //发送HTTP请求
return $output2 ;
curl_close($ch);
//$this->unlink($this->cookie_jar);
}
/*推送数据操作*/
function sendMes($url,$img,$imgdesc,$groupid,$groupname,$channel,$lanmu)
{
//var_dump($this->cookie_jar);
//exit();
$url = "http://XXXX/add";
$data = $this->getArr($img,$imgdesc,$groupid,$groupname,$channel,$lanmu);
$ch = curl_init();
$Ref_url = "http://www.你的地址";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $Ref_url); //伪装REFERER
curl_setopt($ch, CURLOPT_POST, 1); //post方式提交数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回数据,而不是直接输出
curl_setopt($ch, CURLOPT_HEADER, 0); // 设置是否显示header信息 0是不显示,1是显示 默认为0
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //发送cookie文件
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //发送POST数据
$output2 = curl_exec($ch); //发送HTTP请求
curl_close($ch);
//$this->unlink($this->cookie_jar);
}
function getArr($img,$imgdesc,$groupid,$groupname,$channel,$lanmu)
{
$post_data = array(
//windows使用如下写法,linux不适用
//"img"=>"@".$img.";type=image/jpeg",
"img"=>"@".$img,
"imgdesc"=>$imgdesc,
"groupid"=>$groupid,
"groupname"=>$groupname,
"channel"=>$channel,
"lanmu"=>$lanmu,
"cdate"=>date('Y-m-d')
);
return $post_data;
}
//格式化getGidArr
function getGidArr($groupname,$channel,$lanmu)
{
$post_data = array(
"groupname"=>$groupname,
"channel"=>$channel,
"lanmu"=>$lanmu,
"cdate"=>date('Y-m-d')
);
return $post_data;
}
//格式化sendPicArr
function sendPicArr($note,$groupid,$groupindex,$img)
{
$post_data = array(
"notes"=>$note,
"id"=>$groupid,
"index"=>$groupindex,
"cdate"=>date('Y-m-d'),
//windows使用如下写法,linux不适用
//"img"=>"@".$img.";type=image/jpeg",
"img"=>"@".$img
);
return $post_data;
}
//清理cookie文件
function unlink($cookie_jar){
unlink($cookie_jar);
}
} |
以上就是用CURL来完美解决了这个问题,他能有效的解决cookie存储问题。
ucenter我想有不少朋友会用到了,就是一个会员同步功能了,我们经常会把几个系统或不同论坛整合起来,下面我来给各位同学介绍一下。
UCenter同步流程:
前提是需要在Ucenter上面添加好需要同步登录的应用(至少2个才能看到效果),并且显示:通信成功
假如我添加了A,B两个应用网站
1.首先当A站登录时,登录成功后,处理实质是调用uc_client提供的方法向UCenter获取2个脚本代码(A、B站),这两个脚本代码就是访问A、B两个站的api/uc.php中的登录方法,可以在方法中做登录所需session和cookie操作。
2.当登出时,操作和登录是一样的,都需要向UCenter获取2个脚本代码,目的是用来触发A、B站的api/uc.php中登出方法。
重点:当获取到2个脚本代码后一定需要在输出让浏览器运行,否则将不会实现同步登录登出。
建议:开发调试的时候,借助浏览器监控来查看返回输出值,我当时用的是chrome F12 后 打开Network标签 。
实例详解
所谓单点登录,无非就是几个站点共用一个用户中心,实现同步登陆,同步退出。
服务器端:Loog SSO
客服端: ucenter,说实话dz商业化确实让php发展了不少。
ucenter 基本原理:
其实最终还是 用户去登录 只是采用了ajax 用户不会发现。
我们来看看和ucenter的具体程序:
config.php [PHP代码]:
代码如下 |
复制代码 |
define(‘UC_CONNECT’, ’mysql’); // 连接 UCenter 的方式: mysql/NULL, 默认为空时为fscoketopen()
//数据库相关 (mysql 连接时, 并且没有设置 UC_DBLINK 时, 需要配置以下变量)
define(‘UC_DBHOST’, ’localhost’); // UCenter 数据库主机
define(‘UC_DBUSER’, ’root’); // UCenter 数据库用户名
define(‘UC_DBPW’, ’123′); // UCenter 数据库密码
define(‘UC_DBNAME’, ’ucenter’); // UCenter 数据库名称
define(‘UC_DBCHARSET’, ’utf8′); // UCenter 数据库字符集
define(‘UC_DBTABLEPRE’, ’ucenter.uc_’); // UCenter 数据库表前缀
define(‘UC_KEY’, ’safefewfef’); // 与 UCenter 的通信密钥, 要与 UCenter 保持一致
define(‘UC_API’, ’http://www.111cn.net/uc’);// UCenter 的 URL 地址, 在调用头像时依赖此常量
define(‘UC_CHARSET’, ’utf-8′); // UCenter 的字符集
define(‘UC_IP’, ’127.0.0.1′); // UCenter 的 IP, 当 UC_CONNECT 为非 mysql 方式时, 并且当前应用服务器解析域名有问题时, 请设置此值
define(‘UC_APPID’, ’3′); // 对应到ucenter当前应用的 ID
define(‘UCDOMAIN’,'http://www.111cn.net/’); // 域名设置
//一些 Cookie 设置
$_UC = array();
$_UC["cookiedomain"] = ”; // cookie 作用域
$_UC["cookiepath"] = ’/'; // cookie 作用路径
$_UC["cookiepre"] = ’uc_’; // cookie 前缀
$_UC["cookietime"] = ’31536000′; //cookie 作用时间
|
配置文件写好后,到ucenter后台里面添加一个应用记住是自定义的 ‘UC_KEY’必须和config.php里面相同
接下来就是你的主目录下的api/uc.php
例如应用url 填写为 http://www.111cn.net 那么我就有对应的http://www.111cn.net/api/uc.php
如果要自定义的话 请确认你的对应关系。
最重要的就是api/uc.php ,同步登录就是访问各个应用的uc.php 关于这个dz已经给了demo
[PHP代码]:
代码如下 |
复制代码 |
define(‘API_DELETEUSER’,0); //note 用户删除 API 接口开关
define(‘API_RENAMEUSER’, 0); //note 用户改名 API 接口开关
define(‘API_GETTAG’, 0); //note 获取标签 API 接口开关
define(‘API_SYNLOGIN’, 1); //note 同步登录 API 接口开关
define(‘API_SYNLOGOUT’, 1); //note 同步登出 API 接口开关
define(‘API_UPDATEPW’, 0); //note 更改用户密码 开关
define(‘API_UPDATEBADWORDS’, 0); //note 更新关键字列表 开关
define(‘API_UPDATEHOSTS’, 0); //note 更新域名解析缓存 开关
define(‘API_UPDATEAPPS’, 0); //note 更新应用列表 开关
define(‘API_UPDATECLIENT’, 0); //note 更新客户端缓存 开关
define(‘API_UPDATECREDIT’, 0); //note 更新用户积分 开关
define(‘API_GETCREDITSETTINGS’, 0); //note 向 UCenter 提供积分设置 开关
define(‘API_GETCREDIT’,0); //note 获取用户的某项积分 开关
define(‘API_UPDATECREDITSETTINGS’, 0); //note 更新应用积分设置 开关
|
这些参数都是向别的应用提供的功能开关
最后关于 自己的页面如何同步登录 别的应用
[PHP代码]:
代码如下 |
复制代码 |
include_once ’../config.php’;
include_once ’../uc_client/client.php’;
|
你的验证登录部分
代码如下 |
复制代码 |
list($uid, $username, $password) = uc_user_login($_POST[username], $_POST[password]);//进入ucenter验证
$ucsynlogin = uc_user_synlogin($uid);//同步登录
echo $ucsynlogin;//因为是ajax 要echo
|
只用php,康盛的解决方案是比较不错的了,而且利用了p3p头实现了 不同域名 单点登录
缺点就是采用ajax 客服端请求 ,如果有10个以上应用,登录速度就慢下来了,这时候就可以考虑下七夜的Loong SSO
了解了以上的东东 php的CMS和ucenter通信 就不难了。
这里给大家介绍一个移动设备检查类了,从google找到的mobile-detect类,这个类可以ct判断android、ios、Tablet平板,浏览器,等比较完美完善的判断ipad,iPhone,win8等
使用方法
代码如下 |
复制代码 |
<?php
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
// Any mobile device.
}
if($detect->isTablet()){
// Any tablet device.
}
Check for a specific platform:
if($detect->isiOS()){
// Code to run for the Apple's iOS platform.
}
if($detect->isAndroidOS()){
// Code to run for the Google's Android platform.
}
|
官方实例
代码如下 |
复制代码 |
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
$scriptVersion = $detect->getScriptVersion();
?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<title>Mobile Detect Local Demo</title>
<style type="text/css">
html { font-size: 100%; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
body { margin: 0; padding: 0 1em; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1em; color: #333333; background-color: #ffffff; max-width:320px; }
body, td { font-size: 1em; }
table th { text-align:left; }
a { color: #0088cc; text-decoration: underline; }
a:hover { color: #005580; text-decoration: underline; }
header h1 small { font-size:small; }
section { margin-bottom:2em; }
section h1 { font-size:100%; }
.infoText { font-size:85%; }
.response { color:red; }
.computer { background-color:blue; color:white; }
.tablet { background-color:yellow; color:black; }
.phone, .true { background-color:green; color:white; }
.sendDataButton { border-radius: 1em; -moz-border-radius: 1em; -webkit-border-radius: 1em; padding:0.5em 1em; cursor: pointer; }
.sendDataButton_yes {
color:white;
border: 1px solid #56A00E;
background: #74B042;
font-weight: bold;
color: #ffffff;
text-shadow: 0 1px 0 #335413;
background-image: -webkit-gradient(linear, left top, left bottom, from( #74B042 ), to( #56A00E )); /* Saf4+, Chrome */
background-image: -webkit-linear-gradient( #74B042 , #56A00E ); /* Chrome 10+, Saf5.1+ */
background-image: -moz-linear-gradient( #74B042 , #56A00E ); /* FF3.6 */
background-image: -ms-linear-gradient( #74B042 , #56A00E ); /* IE10 */
background-image: -o-linear-gradient( #74B042 , #56A00E ); /* Opera 11.10+ */
background-image: linear-gradient( #74B042 , #56A00E );
}
.sendDataButton_no {
color:white;
border: 1px solid #cd2c24;
background: red;
font-weight: bold;
color: #ffffff;
text-shadow: 0 1px 0 #444444;
background-image: -webkit-gradient(linear, left top, left bottom, from( #e13027 ), to( #b82720 )); /* Saf4+, Chrome */
background-image: -webkit-linear-gradient( #e13027 , #b82720 ); /* Chrome 10+, Saf5.1+ */
background-image: -moz-linear-gradient( #e13027 , #b82720 ); /* FF3.6 */
background-image: -ms-linear-gradient( #e13027 , #b82720 ); /* IE10 */
background-image: -o-linear-gradient( #e13027 , #b82720 ); /* Opera 11.10+ */
background-image: linear-gradient( #e13027 , #b82720 );
}
#feedbackForm fieldset { border:1px dotted #333; }
#feedbackForm label { font-weight:bold; font-size:85%; }
#feedbackForm textarea { width: 260px; }
</style>
</head>
<body>
<section>
<p>This is a <b><?php echo $deviceType; ?></b>. Your UA is <b class="<?php echo $deviceType; ?>"><?php echo htmlentities($_SERVER['HTTP_USER_AGENT']); ?></b></p>
</section>
<!-- copy to GitHub demo.php -->
<section>
<h1>Supported methods</h1>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<th colspan="2">Basic detection methods</th>
</tr>
<tr>
<td>isMobile()</td><td <?php $check = $detect->isMobile(); if($check): ?>class="true"<?php endif; ?>><?php var_dump($check); ?></td>
</tr>
<tr>
<td>isTablet()</td><td <?php $check = $detect->isTablet(); if($check): ?>class="true"<?php endif; ?>><?php var_dump($check); ?></td>
</tr>
</tbody>
<tbody>
<tr>
<th colspan="2">Custom detection methods</th>
</tr>
<?php foreach($detect->getRules() as $name => $regex):
$check = $detect->{'is'.$name}();
?>
<tr>
<td>is<?php echo $name; ?>()</td>
<td <?php if($check): ?>class="true"<?php endif; ?>><?php var_dump($check); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<tbody>
<tr>
<th colspan="2">Experimental version() method</th>
</tr>
<?php
foreach($detect->getProperties() as $name => $match):
$check = $detect->version($name);
if($check!==false):
?>
<tr>
<td>version(<?php echo $name; ?>)</td>
<td><?php var_dump($check); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tbody>
<tr>
<th colspan="2">Other tests</th>
</tr>
<tr>
<td>isiphone()</td>
<td><?php echo var_dump($detect->isiphone()); ?></td>
</tr>
<tr>
<td>isIphone()</td>
<td><?php echo var_dump($detect->isIphone()); ?></td>
</tr>
<tr>
<td>istablet()</td>
<td><?php echo var_dump($detect->istablet()); ?></td>
</tr>
<tr>
<td>isIOS()</td>
<td><?php echo var_dump($detect->isIOS()); ?></td>
</tr>
<tr>
<td>isWhateverYouWant()</td>
<td class="randomcrap"><?php echo var_dump($detect->isWhateverYouWant()); ?></td>
</tr>
</tbody>
</table>
</section>
</body>
</html>
|
php-mobile-detect文件下载地焉: http://file.111cn.net/upload/2013/10/M.rar
以前做过一个招聘网站这样我们需要让别有采集不到我们客户的手机号码与邮箱地址了,所以我们会利用php实现从数据库读出来的手机号码与邮箱地址直接生成一张图片了,这样采集过去只能是图片并且无法识别了,下面我来给大家介绍两个实例。
PHP字符串处理-将手机号码生存图片
代码如下 |
复制代码 |
<?php
////电话号码转变成图片
//$str 要显示的字串,$rand是否启用扰码
function str_to_image($str,$w=130,$h=25,$rand=true)
{
//生成11位的数字图片
Header("Content-type:image/png"); //告诉浏览器,下面的数据是图片,而不要按文字显示
//定义图片宽高
$nwidth=$w;
$nheight=$h;
//srand((double)microtime()*1000000); //取得目前时间的百万分之一秒值,以执行时的百万分之一秒当乱数种子
$randval=$str; //11位数
$im=@imagecreate($nwidth,$nheight) or die("Can't initialize new GD image stream"); //建立图象
//图片色彩设置
$background_color=imagecolorallocate($im,255,255,255); //匹配颜色
$text_color=imagecolorallocate($im,23,14,91);
//绘制图片边框
imagefilledrectangle($im,0,0,$nwidth-1,$nheight-1,$background); //矩形区域着色
imagerectangle($im,0,0,$nwidth-1,$nheight-1,$background_color); //绘制矩形
imagestring($im,8,10,4,$randval,$text_color); //绘制横式字串
if($rand){
//加入干扰因素
for($i=0;$i<260;$i++)
{
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im,rand()%($nwidth-5),rand()%($nheight+5),$randcolor); //点
}
}
//imagestring($im,3,5,5,"A Simple Text String",$text_color);
//imageinterlace($im,1);
imagepng($im); //建立png图型
imagedestroy($im); //结束图型
}
$str = '13087263453';
echo str_to_image($str,$w=130,$h=25,$rand=true)
|
例2
代码如下 |
复制代码 |
<?php //前面不要有空行
$id=$_GET[id];
include("admin/config.php");
$sql="select * from user where id=$id";
$data=mysql_fetch_array(mysql_query($sql));
$p=SBC_DBC($data[Phone],1);
function get_str($str,$strlen=16) {
$str=stripslashes($str);
for($i=0;$i<$strlen;$i++)
if(ord(substr($str,$i,1))>0xa0) $j++;
if($j%2!=0) $strlen++;
$tmp_str=substr($str,0,$strlen);
return $tmp_str;
}
if($p<>''){
//生成5位的数字图片
Header("Content-type:image/png"); //告诉浏览器,下面的数据是图片,而不要按文字显示
//定义图片宽高
$nwidth=120;
$nheight=25;
$im=@imagecreate($nwidth,$nheight) or die("Can't initialize new GD image stream"); //建立图象
//图片色彩设置
$background_color=imagecolorallocate($im,255,255,255); //匹配颜色
$text_color=imagecolorallocate($im,23,14,91);
//绘制图片边框
imagefilledrectangle($im,0,0,$nwidth-1,$nheight-1,$background); //矩形区域着色
imagerectangle($im,0,0,$nwidth-1,$nheight-1,$background_color); //绘制矩形
//srand((double)microtime()*1000000); //取得目前时间的百万分之一秒值,以执行时的百万分之一秒当乱数种子
//$randval=rand();
$randval=$p; //5位数
imagestring($im,8,10,2,$randval,$text_color); //绘制横式字串
//加入干扰因素
//for($i=0;$i<478;$i++)
//{
//$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
//imagesetpixel($im,rand()%100,rand()%30,$randcolor); //点
//}
//imagestring($im,3,5,5,"A Simple Text String",$text_color);
//imageinterlace($im,1);
imagepng($im); //建立png图型
imagedestroy($im); //结束图型
}else{
echo "<font size=2>商家未输入电话号码</font>";
}
?>
|
标签:[!--infotagslink--]