codeIgniter我用过一下,觉得比较的理想,很适合做开发,一个很小巧的php的MVC框架。
由于是一个框架模式,所以样例代码拿过来是很容易看清楚程序架构的,也很容易参考,这比自己写的程序要好很多,至少你很容易找到每个代码的功能和位置。
碰到一个问题:
apache里面可以这样做rewrite
两种写法都可以让index.php处理此链接,而第一种是最好的,用户输入查询也没事,比如 /topic?test=ds
第二种写法就不行了,会出现错误,地址匹配有特殊字符
但对于nginx的服务器,是不能那样写的,如果按第一种写法,会出现404错误,当作目录了
如果是lighttpd,那应该和apache类似,没有问题。
出现的错误是:
出自(系统库文件URI.php):
看代码,我们可以设置config里面 enable_query_strings 为TRUE
这次会得到什么结果:
这说明已经过了检查地址,到了路由这一部分了,这个错误是找不到相应的class/method所导致的
重要的URI处理是在 _fetch_uri_string 函数里面,调用这个过滤器是在路由控制里面:
从Router.php里面找到(大概90行)
我们可以看到路由取出来的地址是什么,比较两种方式
设置为:
测试地址为: /topic/test?site=aslibra.com
理想状态应该是进入 /topic/test
第一种情况,取得的uri为 site,这错的比较离谱
第二种为 /topic,检查server变量
都发生了错误,修改auto为REQUEST_URI应该可以有救
对于这个是支持,因为有正常的class和method,但如果是一级参数,那就出错了,比如
/topic/?site=aslibra.com
这种情况最好的解决办法是钩子处理,在系统开始的时候把查询字符都忽略了
或者,在index.php调用CI之前,就去掉,比如
钩子方式的解决办法:
新建 hooks/Uri.php
那修改auto为REQUEST_URI应该可以,查询字符支持可以不支持了
原创内容如转载请注明:来自 阿权的书房
由于是一个框架模式,所以样例代码拿过来是很容易看清楚程序架构的,也很容易参考,这比自己写的程序要好很多,至少你很容易找到每个代码的功能和位置。
碰到一个问题:
apache里面可以这样做rewrite
<VirtualHost *:80>
ServerName www.aslibra.com
DocumentRoot F:\WebsiteLocal\www.aslibra.com
RewriteEngine On
RewriteRule ^/(topic|user) /index.php$0
#RewriteRule ^/(topic|user) /index.php?$0
</VirtualHost>
ServerName www.aslibra.com
DocumentRoot F:\WebsiteLocal\www.aslibra.com
RewriteEngine On
RewriteRule ^/(topic|user) /index.php$0
#RewriteRule ^/(topic|user) /index.php?$0
</VirtualHost>
两种写法都可以让index.php处理此链接,而第一种是最好的,用户输入查询也没事,比如 /topic?test=ds
第二种写法就不行了,会出现错误,地址匹配有特殊字符
但对于nginx的服务器,是不能那样写的,如果按第一种写法,会出现404错误,当作目录了
如果是lighttpd,那应该和apache类似,没有问题。
location ~^/(topic|user) {
rewrite ^/(.*) /index.php?/$1 last;
}
rewrite ^/(.*) /index.php?/$1 last;
}
出现的错误是:
引用
An Error Was Encountered
The URI you submitted has disallowed characters.
The URI you submitted has disallowed characters.
出自(系统库文件URI.php):
function _filter_uri($str)
{
if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
{
// preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
// compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str))
{
show_error('The URI you submitted has disallowed characters.', 400);
}
}
// Convert programatic characters to entities
$bad = array('$', '(', ')', '%28', '%29');
$good = array('$', '(', ')', '(', ')');
return str_replace($bad, $good, $str);
}
{
if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
{
// preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
// compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str))
{
show_error('The URI you submitted has disallowed characters.', 400);
}
}
// Convert programatic characters to entities
$bad = array('$', '(', ')', '%28', '%29');
$good = array('$', '(', ')', '(', ')');
return str_replace($bad, $good, $str);
}
看代码,我们可以设置config里面 enable_query_strings 为TRUE
这次会得到什么结果:
引用
404 Page Not Found
The page you requested was not found.
The page you requested was not found.
这说明已经过了检查地址,到了路由这一部分了,这个错误是找不到相应的class/method所导致的
重要的URI处理是在 _fetch_uri_string 函数里面,调用这个过滤器是在路由控制里面:
从Router.php里面找到(大概90行)
// Fetch the complete URI string
$this->uri->_fetch_uri_string();
echo $this->uri->uri_string."<hr>"; //检查
$this->uri->_fetch_uri_string();
echo $this->uri->uri_string."<hr>"; //检查
我们可以看到路由取出来的地址是什么,比较两种方式
设置为:
$config['uri_protocol'] = "AUTO";
$config['enable_query_strings'] = TRUE;
$config['enable_query_strings'] = TRUE;
测试地址为: /topic/test?site=aslibra.com
理想状态应该是进入 /topic/test
第一种情况,取得的uri为 site,这错的比较离谱
第二种为 /topic,检查server变量
引用
[QUERY_STRING] => /topic
[REQUEST_URI] => /topic/test/?site=aslibra.com
[SCRIPT_NAME] => /topic/test/
[PHP_SELF] => /topic/test/
[REQUEST_TIME] => 1259512731
[argv] => Array
(
[0] => /topic
)
[argc] => 1
)
[REQUEST_URI] => /topic/test/?site=aslibra.com
[SCRIPT_NAME] => /topic/test/
[PHP_SELF] => /topic/test/
[REQUEST_TIME] => 1259512731
[argv] => Array
(
[0] => /topic
)
[argc] => 1
)
都发生了错误,修改auto为REQUEST_URI应该可以有救
对于这个是支持,因为有正常的class和method,但如果是一级参数,那就出错了,比如
/topic/?site=aslibra.com
这种情况最好的解决办法是钩子处理,在系统开始的时候把查询字符都忽略了
或者,在index.php调用CI之前,就去掉,比如
//code from http://aslibra.com/
$tmp = explode('?',$_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = $tmp[0];
$tmp = explode('?',$_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = $tmp[0];
钩子方式的解决办法:
//配置里 $config['enable_hooks'] = TRUE;
//config/hooks.php
$hook['pre_system'] = array(
'class' => 'Uri',
'function' => 'fix',
'filename' => 'Uri.php',
'filepath' => 'hooks',
'params' => ''
);
//config/hooks.php
$hook['pre_system'] = array(
'class' => 'Uri',
'function' => 'fix',
'filename' => 'Uri.php',
'filepath' => 'hooks',
'params' => ''
);
新建 hooks/Uri.php
<?php
//code from http://aslibra.com/
class Uri{
function fix(){
$tmp = explode('?',$_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = $tmp[0];
}
}
?>
//code from http://aslibra.com/
class Uri{
function fix(){
$tmp = explode('?',$_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = $tmp[0];
}
}
?>
那修改auto为REQUEST_URI应该可以,查询字符支持可以不支持了
原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
手机影像:三里屯的礼物
史上最全的电子杂志列表,在线免费看
