php 在 codeigniter 中启用 $_GET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2043070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enabling $_GET in codeigniter
提问by Nir Gavish
I've been trying to figure out how to enable $_GET in CI.
我一直在试图弄清楚如何在 CI 中启用 $_GET。
It appears the framework deliberately destroys the $_GET array, and that enabling it requires serious tinkering with the core classes. can anyone say why this is, and how to overcome it?
框架似乎故意破坏了 $_GET 数组,启用它需要对核心类进行认真的修改。谁能说这是为什么,以及如何克服它?
mind you, i'm looking to keep URI parsing and routing the way they are, just simply have the $_GET available as well.
请注意,我希望保持 URI 解析和路由的方式,只需让 $_GET 可用。
回答by Stephen Curran
Add the following library to your application libraries. It overrides the behaviour of the default Input library of clearing the $_GET array. It allows for a mixture of URI segments and query string.
将以下库添加到您的应用程序库中。它覆盖了清除 $_GET 数组的默认输入库的行为。它允许混合使用 URI 段和查询字符串。
application/libraries/MY_Input.php
应用程序/库/MY_Input.php
class MY_Input extends CI_Input
{
function _sanitize_globals()
{
$this->allow_get_array = TRUE;
parent::_sanitize_globals();
}
}
Its also necessary to modify some configuration settings. The uri_protocol setting needs to be changed to PATH_INFO and the '?' character needs to be added to the list of allowed characters in the URI.
还需要修改一些配置设置。uri_protocol 设置需要更改为 PATH_INFO 和 '?' 需要将字符添加到 URI 中允许的字符列表中。
application/config/config.php
应用程序/配置/config.php
$config['uri_protocol'] = "PATH_INFO";
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?';
It is then possible to access values passed in through the query string.
然后就可以访问通过查询字符串传入的值。
$this->input->get('x');
回答by Gordon
From the CodeIgniter's manual about security:
GET, POST, and COOKIE Data
GET data is simply disallowed by CodeIgniter since the system utilizes URI segments rather than traditional URL query strings (unless you have the query string option enabled in your config file). The global GET array is unset by the Input class during system initialization.
GET、POST 和 COOKIE 数据
CodeIgniter 根本不允许 GET 数据,因为系统使用 URI 段而不是传统的 URL 查询字符串(除非您在配置文件中启用了查询字符串选项)。全局 GET 数组在系统初始化期间由 Input 类取消设置。
Read through this forum entry for possible solutions(gets interesting from halfway down page 1).
通读此论坛条目以获取可能的解决方案(从第 1 页的中途开始变得有趣)。
回答by David Xia
I don't have enough reputation to comment, but Phil Sturgeon's answer aboveis the way to go if switching to Codeigniter Reactoris easy for you.
我没有足够的声誉来发表评论,但是如果切换到Codeigniter Reactor对您来说很容易,那么上面的 Phil Sturgeon 的回答就是您要走的路。
You can access the query string by using $_GET or $this->input->get() without having needing the MY_Input override or even altering the config.php file.
您可以使用 $_GET 或 $this->input->get() 访问查询字符串,而无需覆盖 MY_Input 甚至更改 config.php 文件。
回答by Andreas Gohr
I had success using this single line in my controller. It basically reparses the request URL without relying on any special CodeIgniter settings:
我在我的控制器中使用这一行取得了成功。它基本上重新解析请求 URL,而不依赖于任何特殊的 CodeIgniter 设置:
parse_str(array_pop(explode('?',$_SERVER['REQUEST_URI'],2)),$_GET);
parse_str(array_pop(explode('?',$_SERVER['REQUEST_URI'],2)),$_GET);
回答by matriphe
On server, without PATH_INFO(just like mine) try this:
在服务器上,没有PATH_INFO(就像我的一样)试试这个:
parse_str(substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],'?')+1,strlen($_SERVER['REQUEST_URI'])-strpos($_SERVER['REQUEST_URI'],'?')),$_GET);
You can put it just like this:
你可以这样说:
class Your_controller extends Controller {
function Your_controller()
{
parent::Controller();
date_default_timezone_set('Asia/Jakarta'); // set my timezone
parse_str(substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],'?')+1,strlen($_SERVER['REQUEST_URI'])-strpos($_SERVER['REQUEST_URI'],'?')),$_GET);
}
function test()
{
print_r($_GET); // here your $_GET vars
}
}
回答by itsme
Never used $_GET with CI, better to change script logic to use POST or $this->uri->segment() , then to active $_GET params for me
从未将 $_GET 与 CI 一起使用,最好将脚本逻辑更改为使用 POST 或 $this->uri->segment() ,然后为我激活 $_GET 参数
回答by Rob Vanders
From post: CodeIgniter PHP Framework - Need to get query string
来自帖子:CodeIgniter PHP Framework - 需要获取查询字符串
Here's a full working example of how to allow querystrings in Codeignitor, like on JROX platform. Simply add this to your config.php file located at:
这是一个完整的工作示例,说明如何在 Codeignitor 中允许查询字符串,例如在 JROX 平台上。只需将其添加到位于以下位置的 config.php 文件中:
/system/application/config/config.php
And then you can simply get the querystrings like normal using $_GET or the class below
然后你可以简单地使用 $_GET 或下面的类像平常一样获取查询字符串
$yo = $this->input->get('some_querystring', TRUE);
$yo = $_GET['some_querystring'];
Here's the code to make it all work:
这是使其全部工作的代码:
/*
|--------------------------------------------------------------------------
| Enable Full Query Strings (allow querstrings) USE ALL CODES BELOW
|--------------------------------------------------------------------------*/
/*
|----------------------------------------------------------------------
| URI PROTOCOL
|----------------------------------------------------------------------
|
| This item determines which server global should
| be used to retrieve the URI string. The default
| setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of
| the other delicious flavors:
|
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/
if (empty($_SERVER['PATH_INFO'])) {
$pathInfo = $_SERVER['REQUEST_URI'];
$index = strpos($pathInfo, '?');
if ($index !== false) {
$pathInfo = substr($pathInfo, 0, $index);
}
$_SERVER['PATH_INFO'] = $pathInfo;
}
$config['uri_protocol'] = 'PATH_INFO'; // allow all characters
$config['permitted_uri_chars'] = ''; // allow all characters
$config['enable_query_strings'] = TRUE; // allow all characters
parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
Enjoy :-)
享受 :-)

