摘要:url编码,thinkphp重定向,http GET请求。
环境:thinkphp,windows wamp
一、这里有个坑
配置项:
URL模式为:pathinfo
class IndexController extends Controller {
public function index(){
$params = array(
'q' => 'q',
'w' => 'w',
'u' => 'http://www.baidu.com'
);
$this->redirect('test',$params);
}
public function test(){
dump(I('get.'));
}
}
如果访问index方法,会报错:
Not Found
The requested URL /tpProject/tpTest/index.php/Home/Index/test/q/q/w/w/u/http://www.baidu.com.html was not found on this server.
Apache/2.4.23 (Win64) PHP/5.6.25 Server at localhost Port 80
这是为什么呢,下来就来讨论这个。
二、http请求
有没有人特意对比过这样几个url的区别呢?
- http://localhost/test/index.php
- http://localhost/test/index.php/c/c
- http://localhost/test/index.php/c/c?u=http%3A%2F%2Fwww.baidu.com
http GET header
http GET body
parse_url()
$_SERVER[‘REQUEST_URI’]
$_SERVER[‘QUERY_STRING’]
$_GET
三、thinkphp 页面跳转方法的处理过程
$this->redirect('test',$params);
U()函数生成要跳转的url为:
/tpProject/tpTest/index.php/Home/Index/test/q/q/w/w/u/http%3A%2F%2Fwww.baidu.com.html
再302重定向,查看apache 的access.log可以看到请求的URL是:
“GET /tpProject/tpTest/index.php/Home/Index/test/q/q/w/w/u/http%3A%2F%2Fwww.baidu.com.html HTTP/1.1” 404 361
apache 找不到目标文件
所以,如果GET参数里面有url的话,及时是经过url编码过的,如果是pathinfo 模式的url还是会有问题,保险起见的方式是:
public function index(){
$params = array(
'q' => 'q',
'w' => 'w',
'u' => 'http://www.baidu.com'
);
$url = U('test') . '?' . http_build_query($params);
header('location:' . $url);
}