preg_match 函数获取链接或许碰到的致命错误

这个错误可以把人雷死,耽误了我两个多小时,我获取的一个链接是 “http://demo.com/?id=1&and=3”,但是每次请求这个网址都显示不存在,然后我把这个网址直接echo出来,和通过preg_match函数得到的进行比较,发现一模一样,但是就是显示无法访问,我没办法,用strlen函数进行比较长度,意外的发现长度不一样,我这时候纳闷了,明明一样的链接为什么不同方式获取的长度不一样,然后又trim下又urldecode urlencode 最后还是没发现原理,没办法了,我就查看输出的源代码,发现有一点点不一样,用preg_match获取的链接竟然带实体符号!!!!大爷的,最后通过htmlspecialchars_decode 函数解决了!!!

无限级分类的动态显示

用到的知识Ajax +php

我以商品销售区域的动态选择为例

数据库设计:

[sql]
CREATE TABLE `think_area` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`pid` int(20) NOT NULL DEFAULT ‘0’,
`area_name` char(100) NOT NULL,
`area_path` char(50) NOT NULL,
`ban_id` int(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;</pre>
[/sql]

其中pid是父级分类的ID值,area_name 是当前区域名称呢 area_path是父级area_path名加“-”+父级ID,例如“0-19-22”,o是顶级区域ID,19是父级的父级区域ID,22是父级区域ID。

ban_id是板块ID,不同的板块销售不同的商品可能送货区域不同,所以绑定下板块ID

实验数据

[sql]
<pre>INSERT INTO `think_area` VALUES (‘2’, ‘0’, ‘测试1’, ‘0’, ”, ‘2’);
INSERT INTO `think_area` VALUES (‘3’, ‘2’, ‘测试1-1’, ‘0-2’, ”, ‘2’);
INSERT INTO `think_area` VALUES (‘4’, ‘0’, ‘南京’, ‘0’, ”, ‘1’);
INSERT INTO `think_area` VALUES (‘5’, ‘4’, ‘江宁’, ‘0-4’, ”, ‘1’);
INSERT INTO `think_area` VALUES (‘6’, ‘5’, ‘河海大学’, ‘0-4-5’, ”, ‘1’);
INSERT INTO `think_area` VALUES (‘8’, ‘4’, ‘仙林’, ‘0-4’, ”, ‘1’);
INSERT INTO `think_area` VALUES (‘9’, ‘8’, ‘南京大学’, ‘0-4-8’, ”, ‘1’);
INSERT INTO `think_area` VALUES (’10’, ‘4’, ‘鼓楼’, ‘0-4’, ”, ‘1’);
[/sql]

选择原理如下:

1.如果用户曾经没选择过任何区域,取得该板块的所有顶级分类传过去供选择,如果曾经选择了某个区域则不管是不是选择到了最底下,都调用函数显示已经选择过的区域并加上下一级的区域供选择(如果有下一级的话)

2.你可以把选择ID传递在cookie或querystring中,这问题不大,我选择的是在这两个中都传递、

3.监听select的值变化,变化了触发刚才的显示函数

HTML:

[html]
<div id="select1"rel="{$Think.get.banid}" aid="{$Think.get.aid}" ></div>
<select class="span2 area " onchange="area_select(this.value,true)"id="select_start">
<option value="0" >选择区域</option>
<volist name="area_data" id="li">
<option value="{$li.id}">{$li.area_name}</option>
</volist>
</select>
<span id="area_append">
</span>
</div>
[/html]
很明显我把板块id $banid 区域ID $aid隐藏在HTML中,供JS读取数据,选择框数值变化激发area_select()函数
需要jquery.js和jquery.cookies.2.2.0.min.js 两个文件
[js]
$("document").ready(function(){
var ban_id=$("#select1").attr("rel");
aid=$.cookies.get("area_id"+ban_id);
if(aid){
area_select(aid);

}
})
function area_select(area_id,r){
var ban_id=$("#select1").attr("rel");
if(area_id==0)
exit;
url=ROOT+"Ajax/select_area?area_id="+area_id;//ROOT="http://localhost/index.php/";

$.get(url,function(data){

$("#select_start").remove();
$("#area_append").html(data)

})
$.cookies.setOptions(cookieOptions);
$.cookies.set("area_id"+ban_id,area_id);
if(r){
n_url=ROOT+"Shop/index?banid="+ban_id+"&aid="+area_id;
window.location.href=n_url;
}

}
[/js]
上面的自己分析
下面是PHP代码,用的thinkphp框架,简化了查询操作、、、不懂的查手册,三分钟就能看明白什么回事
[php]
function select_area() {
$Area = D("Area");
$id = (int) $_GET["area_id"];
$data = $Area->find($id);
$area_path = $data["area_path"];
$ban_id = $data["ban_id"];
$level_num = count($level_data = explode("-", $area_path)); //计算层级
$result = "";
for ($i = 0; $i < $level_num; $i++) {
$area_path = "0";
for ($j = 1; $j < $i + 1; $j++) {
$area_path.="-" . $level_data[$j];
}
$row = $Area->where("area_path=" . "’$area_path’ and ban_id=" . $ban_id)->select();

$str = ‘<select class="span2 area" onchange="area_select(this.value,true)" id="select’ . $i . ‘">’;
foreach ($row as $key => $value) {
$selected = "";//注意为什么要引入这个值!!是为了记住刚才选择过的项。
if ($value["id"] == $level_data[$i + 1] || $value["id"] == $id)
$selected = "selected";
$str.="<option value=" . $value[‘id’] . " " . $selected . " >" . $value["area_name"] . "</option>";
}
$str.="</select>";
$result.=$str;
}
/* * ******
* 下面是判断有无子级元素并取出来
*/
$data = $Area->where("pid=" . $id)->select();
if ($data) {
$str = ‘<select class="span2 area" onchange="area_select(this.value,true)" id="select’ . $level_num . ‘">’;
foreach ($data as $key => $value) {
$str.="<option value=" . $value[‘id’] . " " . $selected . " >" . $value["area_name"] . "</option>";
}
$str.="</select>";
$result.=$str;
}
echo $result;
}
[/php]

QQ聊天机器人

[php]
<?php

include "http_no_cookie.class.php";

class qq {

public $sid;
public $http;
public $qq_num;

function __construct() {
$this->http = new http_no_cookie;
}

function login($qq_num, $qq_pwd) {
echo $data = $this->http->get("http://pt.3g.qq.com/");
$action = preg_match("/action="(.+)?"/", $data, $matches);
$action = $matches[1];
$params = array();
$params["login_url"] = "http://pt.3g.qq.com/s?aid=nLogin";
$params["sidtype"] = 1;
$params["loginTitle"] = "手机腾讯网";
$params["bid"] = 0;
$params["qq"] = $qq_num;
$params["pwd"] = $qq_pwd;
$params["loginType"] =1;
echo $data = $this->http->post($action, http_build_query($params));
if(preg_match("/http://vc.gtimg.com//",$data,$matches)){
echo "需要输入验证码";
return 0;
exit;
}

if(preg_match("/密码错误/",$data,$matches)){
echo "密码错误";
return 1;
exit;
}
$action = preg_match("/sid=(.+?)&/", $data, $matches);
$this->sid = $matches[1];
return $this->sid;
}

function sendMsg($to_num, $msg, $sid = 0) {
$sid = $sid ? $sid : $this->sid;
if (!$sid)
exit("sid值未传入进去");
$params = array();
$params["msg"] = $msg;
$params["u"] = $to_num;
$params["saveURL"] = 0;
$params["do"] = "send";
$params["on"] = 1;
$params["aid"] = "发送";
$url = "http://q16.3g.qq.com/g/s?sid=" . $sid;
echo $data = $this->http->post($url, http_build_query($params));
return $data;
}

function getMsg($qq_num = 0, $sid = 0) {
$qq_num = $qq_num ? $qq_num : $this->qq_num;
if (!$qq_num)
exit("qq_num值未传入进去");
$sid = $sid ? $sid : $this->sid;
if (!$sid)
exit("sid值未传入进去");
$url = "http://q16.3g.qq.com/g/s?sid=" . $sid . "&3G_UIN=" . $qq_num . "&saveURL=0&aid=nqqChat";
$data = $this->http->get($url);
preg_match("/name="u" value="(d+)"/", $data, $matches);
$result["qq"] = $matches[1];
$data = explode("<form", $data);
$data = $data[0];
preg_match_all("/<p>(.+)?</p>/", $data, $matches);
unset($matches[1][0]);
$result["content"] = $matches[1];
return $result;
}
function logout($sid){
$url="http://pt.3g.qq.com/s?sid=".$sid."&aid=nLogout";
echo $url;
echo $this->http->get($url);
}
function getFriendsList($qq_num = 0, $sid = 0){
$result=array();

$qq_num = $qq_num ? $qq_num : $this->qq_num;
if (!$qq_num)
exit("qq_num值未传入进去");
$sid = $sid ? $sid : $this->sid;
if (!$sid)
exit("sid值未传入进去");
$url="http://q16.3g.qq.com/g/s?aid=nqqchatMain&sid=".$sid."&myqq=".$qq_num;
while(true){
$i=1;
$url.="&p=".$i;
$data=$this->http->get($url);
preg_match_all("/u=(.+?)&/",$data,$matches);
foreach($matches[1] as $key=>$value){
$result[]=$value;
}
if(count($matches[1])<13)
break;
$i++;
}
return $result;
}
}
[/php]

 

[php]
<?php

class http_no_cookie {

private $curl;
public $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13";

public function get($url) {
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 8);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HEADER, 0);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($this->curl);
curl_close($this->curl);
return $data;
}

public function post($url, $params) {
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 8);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($this->curl);
curl_close($this->curl);
return $data;
}

}

?>
[/php]

PHP使用GD库实现截屏功能

PHP5.2.2以上版本的GD库实现了两个截屏函数 imagegrabscreen 和 imagegrabwindow
分别用于截取整个屏幕和截取某个窗口(同ALT+PrintScreen)的屏幕。

1. 截取整个屏幕 Screenshot
[php]
<?php
$im = imagegrabscreen();
imagepng($im, "myscreenshot.png");
?>
[/php]

2. 截取一个窗口 Capture a window (IE for example)

[php]
<?
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$im = imagegrabwindow($handle);
$browser->Quit();
imagepng($im, "iesnap.png");
$im = imagegrabscreen();
?>
[/php]

3. 截取IE内容 Capture a window (IE for example) but with its content!

[php]
<?php
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://www.abcd9.com/");

/* Still working? */
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
?>
[/php]

4. 截取IE的全屏模式 IE in fullscreen mode
[php]
<?php
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;

$browser->Visible = true;
$browser->FullScreen = true;
$browser->Navigate("http://www.abcd9.com/");

/* Is it completely loaded? (be aware of frames!)*/
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
?>
[/php]

附加说明:
1、imagegrabscreen和imagegrabwindow只能在windows环境下工作
2、截图黑屏解决办法:web 服务器(iis或apache)做为windows服务时,必须打开”允许与桌面交互”的选项(点击服务属性->登录->勾选”允许与桌面交互”,设置后重启服务生效)。

如何让网站程序定时执行

技术原理

cron+taskqueue

有人说直接用cron就行了,为什么还要用taskqueue呢,是这样的,如果加入一个定时执行任务就要建立一个cron计划的话未免太麻烦,也有可能受到服务器资源的限制,比如我用新浪SAE只能设置15个cron计划,那假如我有一千个定时执行任务,就没办法了。

所以我采取以下策略

我把每个计划任务分成两个部分:需要请求的URL和POST数据,post数据用json格式储存。创建两个表circle表和task表,如果是隔特定时间执行一次任务就在task表中创建一个任务并把任务ID加入circle表中。如果是定时执行,比如x时x分x秒执行,只需把该任务加入task表中即可,并附上时间信息。

然后建了15个cron任务,每隔4秒执行特定的一个脚本,该脚本任务是从task表和circle表中读取时间信息,判断哪个任务需要当前执行,然后取出加入taskqueue队列。。