存档

文章标签 ‘PHP’

PHP fsockopen 函数

2010年10月28日 admin 评论已被关闭

Description
resource fsockopen ( string target [, int port [, int &errno [, string &errstr [, float timeout]]]] )

Initiates a socket connection to the resource specified by target. PHP supports targets in the Internet and Unix domains as described in 附录 N. A list of supported transports can also be retrieved using stream_get_transports().

注: If you need to set a timeout for reading/writing data over the socket, use stream_set_timeout(), as the timeout parameter to fsockopen() only applies while connecting the socket.

As of PHP 4.3.0, if you have compiled in OpenSSL support, you may prefix the hostname with either ‘ssl://’ or ‘tls://’ to use an SSL or TLS client connection over TCP/IP to connect to the remote host.

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()).

If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference.

Depending on the environment, the Unix domain or the optional connect timeout may not be available.

The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking().

打开网络的 Socket 链接。
语法: int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);
返回值: 整数
函数种类: 网络系统

内容说明目前这个函数提供二个 Socket 资料流界面,分别为 Internet 用的 AF_INET 及 Unix 用的 AF_UNIX。当在 Internet 情形下使用时,参数 hostname 及 port 分别代表网址及埠号。在 UNIX 情形可做 IPC,hostname 参数表示到 socket 的路径,port 配置为 0。可省略的 timeout 选项表示多久没有连上就中断。在使用本函数之后会返回文件指针,供文件函数使用,包括 fgets()、fgetss()、fputs()、fclose() 与 feof()。参数 errno 及 errstr 也是可省略的,主要当做错误处理使用。使用本函数,会使用搁置模式 (blocking mode) 处理,可用 set_socket_blocking() 转换成无搁置模式。

使用范例
本例用来模拟成HTTP连接。

<?php
$fp = fsockopen("www.wenan8.com", 80, &$errno, &$errstr, 10);
if(!$fp) {
        echo "$errstr ($errno)<br>\n";
} else {
        fputs($fp,"GET / HTTP/1.0\nHost: www.wenan8.com\n\n");
        while(!feof($fp)) {
                echo fgets($fp,128);
        }
        fclose($fp);
}
?>
分类: PHP 标签: ,

定时运行PHP页面

2010年10月28日 admin 评论已被关闭

某个PHP页面需要在每天的某个时间定时运行。
其实就是应用windows的定时任务。
控制面板中任务计划–》添加任务计划–》 依次下一步找到php的安装目录php.exe 然后 高级设置里 -q 命令 运行某个页面。(如图)

分类: PHP 标签: , ,

PHP模拟浏览器重启路由器

2010年10月28日 admin 评论已被关闭

项目中需要用到经常采集搜索引擎的数据,而百度,google等对短时间内大流量搜索做了限制,会限制IP(百度) 等,google的更是做了识别本机生成cookie(猜测) 的限制,模拟cookie都不行。

大多数采集器都做了自动重启IP的功能, 当采集页面匹配到做了限制的 正则时,执行重启IP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//$this->noData 为匹配限制的页面正则, $this->page 为页面内容,可用file_get_content()获取
 
if(preg_match($this->noData,$this->page)){
$this->reStartRouter();
 
}
 
//操作路由
 
function router($target=''")
 
{
 
$server  = '192.168.0.1';            // IP address
 
$host    = '192.168.0.1';            // Domain name
 
$port    = 80;
 
$referer = 'http://'.$host.$target;    // Referer
 
$username = "admin";#路由的用户名
 
$password = "admin";#路由的管理密码
 
$authorization = base64_encode($username.":".$password);
 
$File = fsockopen($server, $port, $errno, $errstr, 50);
 
if ($File)
 
{
 
$out = "GET $target HTTP/1.1\r\n";
 
$out .= "Host: $host\r\n";
 
$out .= "Referer: $referer\r\n";
 
$out .= "Authorization: Basic $authorization\r\n";
 
$out .= "Connection: Close\r\n\r\n";
 
fputs($File, $out);
 
$makeFile = $buffer = "";
 
while ($buffer = fread($File,4096))
 
{
 
$makeFile = $makeFile.$buffer;
 
}
 
fclose($File);
 
return $makeFile;
 
}
 
}
 
//断开
 
function disconnectr() {
 
$url = '/if.cgi?redirect=sys_status.htm&amp;failure=fail.htm&amp;type=wan_state_reset&amp;ifname=ppp0&amp;ifstatus=Up&amp;ifcmd=DISCONNECT';
 
router($url);
 
}
 
//连接
 
function connectr() {
 
$url = '/if.cgi?redirect=sys_status.htm&amp;failure=fail.htm&amp;type=wan_state_reset&amp;ifname=ppp0&amp;ifstatus=Down&amp;ifcmd=CONNECT';
 
router($url);
 
}
 
function reStartRouter()
{
$r=new router();
$r->disconnectr();//断开
$r->connectr();//连接
sleep(3);//静止3秒,一般要过一会才可以 连上
return true;
}
分类: PHP 标签: ,

ThinkPHP 设置默认时区

2010年8月27日 admin 评论已被关闭

Thinkphp 设置默认时区

有些应用所在的服务器和访问的区域间隔较大,导致服务器时间不准确,我们可以通过设置默认时区的方法来处理。

我们只需要在项目配置文件中添加:
‘DEFAULT_TIMEZONE’=>’Asia/Singapore’ // 设置默认时区为新加坡

分类: PHP 标签: , , ,

PHP数组函数in_array与array_diff

2010年7月27日 admin 2 条评论

in_array — 检查数组中是否存在某个值
例子:

1
2
3
4
5
6
7
8
9
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>

array_diff() 返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值。注意键名保留不变。
例子:

1
2
3
4
5
6
7
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
 
print_r($result);
?>

用处
判断变量是否在数组中( in_array )
对不在其一数组中的键值进行操作:
例:

1
2
3
4
5
6
7
8
$subArr=array('0'=>'isTitle','1'=>'isUrl','2'=>'isIntro','3'=>'isAuthor','4'=>'isPub_Date','5'=>'isPub_Time', );
$notSelect = array_diff($subArr,$data); //取数组交集,判断没有选中的对象
foreach($notSelect AS $key=>$val)
    {
      $queryNo[]=$val.'=0';
	}
$queryNo=implode(',',$queryNo);
$queryNo= " UPDATE ......   ";
分类: PHP 标签: , , ,

[转]PHP程序61条面向对象分析设计的经验原则

2010年7月14日 admin 1 条评论

你不必严格遵守这些原则,违背它们也不会被处以宗教刑罚。但你应当把这些原则看成警铃,若违背了其中的一条,那么警铃就会响起 。 —– Arthur J.Riel

  (1)所有数据都应该隐藏在所在的类的内部。

  (2)类的使用者必须依赖类的共有接口,但类不能依赖它的使用者。

  (3)尽量减少类的协议中的消息。

  (4)实现所有类都理解的最基本公有接口[例如,拷贝操作(深拷贝和浅拷贝)、相等性判断、正确输出内容、从ASCII描述解析等等]。

  (5)不要把实现细节(例如放置共用代码的私有函数)放到类的公有接口中。

  如果类的两个方法有一段公共代码,那么就可以创建一个防止这些公共代码的私有函数。

  (6)不要以用户无法使用或不感兴趣的东西扰乱类的公有接口。

  (7)类之间应该零耦合,或者只有导出耦合关系。也即,一个类要么同另一个类毫无关系,要么只使用另一个类的公有接口中的操作。

  (8)类应该只表示一个关键抽象。

  包中的所有类对于同一类性质的变化应该是共同封闭的。一个变化若对一个包影响,则将对包中的所有类产生影响,而对其他的包不造成任何影响 .

  (9)把相关的数据和行为集中放置。

  设计者应当留意那些通过get之类操作从别的对象中获取数据的对象。这种类型的行为暗示着这条经验原则被违反了。

  (10)把不相关的信息放在另一个类中(也即:互不沟通的行为)。

  朝着稳定的方向进行依赖.
阅读全文…

分类: PHP 标签: ,

Memcached在大型网站中应用

2010年7月11日 admin 评论已被关闭

原文链接:http://tomore.myoow.com/showlog351.html
===================================================
memcached是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。最初为了加速 LiveJournal 访问速度而开发的,后来被很多大型的网站采用。起初作者编写它可能是为了提高动态网页应用,为了减轻数据库检索的压力,来做的这个缓存系统。它的缓存是一种分布式的,也就是可以允许不同主机上的多个用户同时访问这个缓存系统, 这种方法不仅解决了共享内存只能是单机的弊端, 同时也解决了数据库检索的压力,最大的优点是提高了访问获取数据的速度!基于memcached作者对分布式cache的理解和解决方案。memcached完全可以用到其他地方 比如分布式数据库, 分布式计算等领域。
阅读全文…

分类: PHP 标签: ,

数组&&字符串 相互转换函数(PHPCMS)

2010年5月15日 admin 评论已被关闭

将数组类型直接插入MYSQL时插入不进为ARRAY
插入数据前把你的数组序列化:
serialize($list);
读出的时候再使用
unserialize($list);

PHPCMS直接将数组存入数据库的函数。
数组转化为字符串: array2string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function array2string($string, $isformdata = 1)
{
	if($string == '') return '';
	if($isformdata) $string = new_stripslashes($string);
	return addslashes(var_export($string, TRUE));
}
 
 
function new_stripslashes($string)
{
	if(!is_array($string)) return stripslashes($string);
	foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
	return $string;
}

stripslashes 函数:
Returns a string with backslashes stripped off. (\’ becomes ‘ and so on.) Double backslashes (\\) are made into a single backslash (\).
该函数用于清理从数据库或 HTML 表单中取回的数据。
删除由 addslashes() 函数添加的反斜杠。

字符串转化为数组:输出。

1
2
3
4
5
6
function string2array($data)
{
	if($data == '') return array();
	eval("\$array = $data;");
	return $array;
}

eval
将值代入字符串之中。

语法: void eval(string code_str);
传回值: 无
函式种类: 数据处理

内容说明:
本函式可将字符串之中的变量值代入,通常用在处理数据库的数据上。参数 code_str 为欲处理的字符串。值得注意

的是待处理的字符串要符合 PHP 的字符串格式,同时在结尾处要有分号。使用本函式处理后的字符串会沿续到 PHP

程序结束。

分类: PHP 标签: , , ,

PHP合并拆分数组

2010年2月27日 admin 评论已被关闭

合并数组有三个函数:
1.array_combine()

携带两个参数数组,参数数组一的值作新数组的键,参数数组二的值作新数组的值。很简单。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< ?php 
$a = array('green', 'red', 'yellow'); 
$b = array('avocado', 'apple', 'banana'); 
$c = array_combine($a, $b); 
 
print_r($c); 
?> 
< ?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
 
print_r($c);
?>

上例将输出:

1
2
3
4
5
6
7
8
9
10
11
12
Array 
( 
[green] => avocado 
[red] => apple 
[yellow] => banana 
) 
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)

阅读全文…

分类: PHP 标签: , , ,

利用Jpgraph绘制统计图

2010年2月3日 admin 评论已被关闭

最近有个小系统要绘制统计图,没有做过,网上找了几个类试了下,没有很合适的,open-flash-chart 很好,但是对于我这新手来说还是比较复杂的,例子也少。正好以前买的本书《大道PHP》 上第29章有一讲是 用Jpgraph 画图的。挺简单的,输入数据就能出图, 看了下例子,拿来用用(最简单的)。
系统还没有做完,先把简单的统计图弄上来。 访问地址
要过年了,在家也不能上网,祝大家新年快乐哦。 ^_^

关键代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
include("config.php");
include("include/jpgraph.php");
include("include/jpgraph_line.php");
//创建背景图像
$resultNum=mysql_query("select * from down_flow ");
//将查询结果设为X坐标和Y坐标显示的数据
$i=0;
while($it=mysql_fetch_array($resultNum)){
	$datay[$i]=(int)$it['flow2'];
	$datax[$i]=(int)$it['hour'];
	$i++;
}
$graph=new Graph(820,490);
//设置刻度样式
$graph->SetScale("intint");
//$graph->SetScale("textint",1000,9500,1,24);
//设置边界范围
$graph->img->SetMargin(40,30,60,10);
//设置标题
$graph->title->Set("统计图");
// 设置中文字体
$graph->title->setFont(FF_SIMSUN,FS_BOLD);
 
 
//定义曲线图
 
$lineplot=new LinePlot($datay,$datax);
//改变曲线图颜色为蓝色
$lineplot->SetColor("blue");
//设置曲线的图例
$lineplot->SetLegend("dataFlow");
$lineplot->setFont(FF_SIMSUN,FS_BOLD);
 
 
 
//将曲线图加入背景图像中
$graph->Add($lineplot);
//将X-Y坐标图输出
$graph->Stroke();
?>
分类: PHP 标签: , ,