日历
| |||||||||
| 日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
| 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 | ||||
存档
搜索标题
最新来客
统计信息
- 访问量: 313
- 日志数: 3
- 建立时间: 2008-03-02
- 更新时间: 2008-03-21
我的最新日志
-
缺德的php cookie 规范
2008-3-21
以前真没在意cookie的作用域问题,可能以前用.com域名没出过问题,今天用公司的一个cn域名测试,怎么也不能实现cookie共享,后来采用js设置cookie,在另外一台服务器上共享cookie成功,真叫气人!翻阅了一下php的cookie 规范 ,里面有这么一段
Only hosts within the specified domain can set a cookie for a domain and domains must have at least two (2) or three (3) periods in them to prevent domains of the form: ".com", ".edu", and "va.us". Any domain that fails within one of the seven special top level domains listed below only require two periods. Any other domain requires at least three. The seven special top level domains are: "COM", "EDU", "NET", "ORG", "GOV", "MIL", and "INT". 看出来了吧,真他娘的草蛋!除了"COM", "EDU", "NET", "ORG", "GOV", "MIL", and "INT"这几个后缀的域名可以设置二级作用域,其他的域名至少三级才可以,也就是说想通过设置作用域为b.cn来实现a.b.cn和c.b.cn下的cookie共享用php是不行的,php只能实现作用域b.a.cn下的d.b.a.cn和e.b.a.cn间的cookie共享,希望大家注意,再遇到此类问题,别浪费时间拼命的测试了……
-
清空目录的小函数
2008-3-19
<?php
/**
* function rmdirs
*
* @link http://www.pams.cn
* @author Gavin yang
*
* @para String $dir Path to delete
*
*/function rmdirs($dir)
{
if (is_dir($dir))
{
$fs = @scandir($dir);
array_shift($fs);array_shift($fs);
foreach($fs as $f)
{
if (is_file($dir.'/'.$f))
{
unlink($dir.'/'.$f);
}
else
{
rmdirs($dir.'/'.$f);
}
}
@rmdir($dir);}
}函数很简单,大家都会写,这里主要强调的是最后那个@rmdir($dir),这里必须使用@或者php已经关闭报错才能彻底删除目录,不然只能把目录清空
-
用curl 实现远程数据提交,支持文件上传
2008-3-02
example.php:
<?php
if ($_SERVER['REQUEST_METHOD']=='POST')
{
require_once("client.php");
$curl = new CurlForm("http://pams.cn/server.php");
$curl->post();
}
?>
<div>
<form enctype="multipart/form-data" action="" method="post">
<input type="text" name="name" />
<input type="file" name="file" />
<input type="file" name="file1[]" />
<input type="file" name="file1[]" />
<input type="submit" />
</form>
</div>
client.php:
<?php
/**
* Class CurlForm
*
* submit form data to a remote server use CURL
* file upload support
*
* @link http://www.pams.cn
* @author Gavin yang
* @msn: athotmail88 @ hotmail.com
*/
class CurlForm
{
/**
* url where you want to submit the form
* @var String
*/
private $remoteUrl;
/**
* form vars
* @var Array
*/
private $postvars = array();
/*
* Constructor for CurlForm
* @param String $remoteUrl url where you want to submit the form
*/
public function __construct($remoteUrl)
{
$this->remoteUrl = $remoteUrl;/*处理提交上来的表单变量,将post变量和files变量同时放入$this->postvars 中,注意,这里是返回的是数组,当然如果只是post变量也可以是字符串"a=b&c=d"的形式,如果含有files变量,就只能放在数组里面*/
if(isset($_POST))
{
foreach ($_POST as $var => $val)
{
$this->postvars[$var] = $val;
}
}/*上传文件的过程实际上分两步,第一步将文件上传到本地服务器,然后取得文件在本地服务器的缓存地址,也就是tmp_name;第二步是将这个缓存文件的数据发送到远程服务器,当然读取文件数据的过程已经在curl函数内部实现了,只要你在文件地址前面加上"@",curl就会认为这是个要传送的附件,但要保证这个文件是真实存在的。这样远程服务器端就接收到本地服务器提交过去的表单,由于向远程服务器上传的文件是本地服务器上的缓存文件,所以要取得原始文件的文件名,在post变量里我给增加了一个叫做'filename'的变量,它是和files变量一一对应的*/
if(isset($_FILES))
{
foreach ($_FILES as $var => $val)
{
if (is_array($val['tmp_name']))
{
foreach ($val['tmp_name'] as $k=>$fname)
{
$this->postvars[$var."[$k]"]= "@".$fname;
$this->postvars['filename'."[$var][$k]"]= $val['name'][$k];
}
}
else
{
$this->postvars[$var] = "@".$val['tmp_name'];
$this->postvars['filename'."[$var]"] = $val['name'];
}
}
}
}/*
* post form to remote server
*/
public function post()
{
set_time_limit(0);
$ch = curl_init();
//设定远程地址
curl_setopt($ch, CURLOPT_URL, $this->remoteUrl );
//post方法
curl_setopt($ch, CURLOPT_POST, 1);
//表单变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postvars);
//设定是否直接显示返回的数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
//设定是否显示头信息
curl_setopt($ch, CURLOPT_HEADER, false);
//设定是否直接输出页面内容 false为输出
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_exec($ch);
//出错则显示错误并退出
curl_errno($ch) && die(curl_error($ch));
//关闭资源
curl_close($ch);
exit();
}
}
?>
server.php
<?php
print_r($_POST);
print_r($_FILES);
?>打包下载 curl.rar

