使用CodeIgniter在URL中获取参数

时间:2021-12-23 08:15:18

I know that codeIgniter turns off GET parameters by default.

我知道codeIgniter默认关闭GET参数。

But by having everything done in POST, don't you get annoyed by the re-send data requests if ever you press back after a form submission?

但是通过在POST中完成所有操作,如果您在表单提交后再次按下,那么重新发送数据请求是否会让您感到烦恼?

It annoys me, but I'm not sure if I want to allow GET purely for this reason.

这让我很烦,但我不确定我是否真的想让GET纯粹出于这个原因。

Is it such a big security issue to allow GET parameters too?

允许GET参数也是一个很大的安全问题吗?

16 个解决方案

#1


56  

When I first started working with CodeIgniter, not using GET really threw me off as well. But then I realized that you can simulate GET parameters by manipulating the URI using the built-in URI Class. It's fantastic and it makes your URLs look better.

当我第一次开始使用CodeIgniter时,不使用GET确实让我失望了。但后来我意识到你可以通过使用内置的URI类操纵URI来模拟GET参数。这太棒了,它会让你的网址看起来更好。

Or if you really need GETs working you can put this into your controller:

或者如果你真的需要GET工作,你可以把它放到你的控制器:

parse_str($_SERVER['QUERY_STRING'], $_GET); 

Which will put the variables back into the GET array.

这将把变量放回GET数组中。

#2


13  

This worked for me :

这对我有用:

<?php
$url = parse_url($_SERVER['REQUEST_URI']);
parse_str($url['query'], $params);
?>

$params array contains the parameters passed after the ? character

$ params数组包含后传递的参数?字符

#3


11  

Now it works ok from CodeIgniter 2.1.0

现在它可以从CodeIgniter 2.1.0运行

    //By default CodeIgniter enables access to the $_GET array.  If for some
    //reason you would like to disable it, set 'allow_get_array' to FALSE.

$config['allow_get_array']      = TRUE; 

#4


10  

This function is identical to the post function, only it fetches get data:

此函数与post函数相同,只是它获取get数据:

$this->input->get()

http://ellislab.com/codeigniter/user-guide/libraries/input.html

http://ellislab.com/codeigniter/user-guide/libraries/input.html

#5


8  

You simply need to enable it in the config.php and you can use $this->input->get('param_name'); to get parameters.

你只需要在config.php中启用它,你就可以使用$ this-> input-> get('param_name');获取参数。

#6


6  

parse_str($_SERVER['QUERY_STRING'],$_GET); ONLY worked for me after I added the following line to applications/config/config.php:

parse_str($ _ SERVER [ 'QUERY_STRING'],$ _ GET);在将以下行添加到applications / config / config.php之后,仅对我有用:

$config['uri_protocol'] = "PATH_INFO";

$ config ['uri_protocol'] =“PATH_INFO”;

I found $_GET params not to really be necessary in CI, but Facebook and other sites dump GET params to the end of links which would 404 for my CI site!! By adding the line above in config.php, those pages worked. I hope this helps people!

我发现$ _GET params在CI中并不是真的有必要,但Facebook和其他网站将GET参数转储到链接的末尾,这对我的CI网站来说是404!通过在config.php中添加上面的行,这些页面起作用了。我希望这有助于人们!

(from http://www.maheshchari.com/work-to-get-method-on-codeigniter/)

(来自http://www.maheshchari.com/work-to-get-method-on-codeigniter/)

#7


4  

You can enable query strings if you really insist. In your config.php you can enable query strings:

如果你真的坚持,你可以启用查询字符串。在config.php中,您可以启用查询字符串:

$config['enable_query_strings'] = TRUE;

For more info you can look at the bottom of this Wiki page: http://codeigniter.com/user_guide/general/urls.html

有关更多信息,请查看此Wiki页面的底部:http://codeigniter.com/user_guide/general/urls.html

Still, learning to work with clean urls is a better suggestion.

尽管如此,学习使用干净的网址是一个更好的建议。

#8


2  

"don't you get annoyed by the re-send data requests if ever you press back after a form submission"

“如果您在提交表单后再次按下,请不要因重新发送数据请求而烦恼”

you can get around this by doing a redirect from the page that processes your form submission to the success page. the last "action" was the loading of the success page, not the form submission, which means if users do an F5 it will just reload that page and not submit the form again.

您可以通过从处理表单提交的页面到成功页面进行重定向来解决这个问题。最后一个“动作”是加载成功页面,而不是表单提交,这意味着如果用户执行F5,它将只重新加载该页面而不再提交表单。

#9


2  

If your your need to first parameter use it.

如果您需要第一个参数使用它。

$this->uri->segment('3');

And your need second parameter use it

你需要第二个参数使用它

$this->uri->segment('4');

Have your many parameter enhance parameter

有你的很多参数增强参数

#10


1  

allesklar: That is slightly misleading, as scripts and bots can POST data nearly as easily as sending a normal request. It's not a secret, it's part of HTTP.

allesklar:这有点误导,因为脚本和机器人可以像发送正常请求一样轻松地发布数据。这不是秘密,它是HTTP的一部分。

#11


1  

A little bit out of topic, but I was looking for a get function in CodeIgniter just to pass some variables between controllers and come across Flashdata.
see : http://codeigniter.com/user_guide/libraries/sessions.html
Flashdata allows you to create a quick session data that will only be available for the next server request, and are then automatically cleared.

有点偏离主题,但我在CodeIgniter中寻找一个get函数只是为了在控制器之间传递一些变量并遇到Flashdata。请参阅:http://codeigniter.com/user_guide/libraries/sessions.html Flashdata允许您创建仅对下一个服务器请求可用的快速会话数据,然后自动清除。

#12


1  

MY_Input.php :

MY_Input.php:

<?php
// this class extension allows for $_GET access
class MY_Input extends CI_input {

    function _sanitize_globals()
    {
        // setting allow_get_array to true is the only real modification
        $this->allow_get_array = TRUE;

        parent::_sanitize_globals();
    }

}
/* End of file MY_Input.php */
/* Location: .application/libraries/MY_Input.php */

MY_URI.php :

MY_URI.php:

<?php
/*
 | this class extension allows for $_GET access by retaining the
 | standard functionality of allowing query strings to build the 
 | URI String, but checks if enable_query_strings is TRUE
*/
class MY_URI extends CI_URI{

    function _fetch_uri_string()
    {
        if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
        {
            // If the URL has a question mark then it's simplest to just
            // build the URI string from the zero index of the $_GET array.
            // This avoids having to deal with $_SERVER variables, which
            // can be unreliable in some environments
            //
            //  *** THE ONLY MODIFICATION (EXTENSION) TO THIS METHOD IS TO CHECK 
            //      IF enable_query_strings IS TRUE IN THE LINE BELOW ***
            if ($this->config->item('enable_query_strings') === TRUE && is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
            {
                $this->uri_string = key($_GET);
                return;
            }

            // Is there a PATH_INFO variable?
            // Note: some servers seem to have trouble with getenv() so we'll test it two ways
            $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
            if (trim($path, '/') != '' && $path != "/".SELF)
            {
                $this->uri_string = $path;
                return;
            }

            // No PATH_INFO?... What about QUERY_STRING?
            $path =  (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
            if (trim($path, '/') != '')
            {
                $this->uri_string = $path;
                return;
            }

            // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
            $path = str_replace($_SERVER['SCRIPT_NAME'], '', (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO'));
            if (trim($path, '/') != '' && $path != "/".SELF)
            {
                // remove path and script information so we have good URI data
                $this->uri_string = $path;
                return;
            }

            // We've exhausted all our options...
            $this->uri_string = '';
        }
        else
        {
            $uri = strtoupper($this->config->item('uri_protocol'));

            if ($uri == 'REQUEST_URI')
            {
                $this->uri_string = $this->_parse_request_uri();
                return;
            }

            $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
        }

        // If the URI contains only a slash we'll kill it
        if ($this->uri_string == '/')
        {
            $this->uri_string = '';
        }
    }

}
/* End of file MY_URI.php */
/* Location: .application/libraries/MY_URI.php */

#13


1  

my parameter is ?uid=4 and get it with:

我的参数是?uid = 4并得到它:

$this->uid = $this->input->get('uid', TRUE);
  echo $this->uid;

wis

WIS

#14


0  

GET parameters are cached by the web browser, POST is not. So with a POST you don't have to worry about caching, so that is why it is usually prefered.

GET参数由Web浏览器缓存,POST不是。所以使用POST你不必担心缓存,所以这就是为什么它通常是首选。

#15


0  

You can Try this

你可以试试这个

$this->uri->segment('');

#16


0  

Even easier:

更容易:

curl -X POST -d "param=value&param2=value" http://example.com/form.cgi

that plugin's pretty cool though.

这个插件非常酷。

#1


56  

When I first started working with CodeIgniter, not using GET really threw me off as well. But then I realized that you can simulate GET parameters by manipulating the URI using the built-in URI Class. It's fantastic and it makes your URLs look better.

当我第一次开始使用CodeIgniter时,不使用GET确实让我失望了。但后来我意识到你可以通过使用内置的URI类操纵URI来模拟GET参数。这太棒了,它会让你的网址看起来更好。

Or if you really need GETs working you can put this into your controller:

或者如果你真的需要GET工作,你可以把它放到你的控制器:

parse_str($_SERVER['QUERY_STRING'], $_GET); 

Which will put the variables back into the GET array.

这将把变量放回GET数组中。

#2


13  

This worked for me :

这对我有用:

<?php
$url = parse_url($_SERVER['REQUEST_URI']);
parse_str($url['query'], $params);
?>

$params array contains the parameters passed after the ? character

$ params数组包含后传递的参数?字符

#3


11  

Now it works ok from CodeIgniter 2.1.0

现在它可以从CodeIgniter 2.1.0运行

    //By default CodeIgniter enables access to the $_GET array.  If for some
    //reason you would like to disable it, set 'allow_get_array' to FALSE.

$config['allow_get_array']      = TRUE; 

#4


10  

This function is identical to the post function, only it fetches get data:

此函数与post函数相同,只是它获取get数据:

$this->input->get()

http://ellislab.com/codeigniter/user-guide/libraries/input.html

http://ellislab.com/codeigniter/user-guide/libraries/input.html

#5


8  

You simply need to enable it in the config.php and you can use $this->input->get('param_name'); to get parameters.

你只需要在config.php中启用它,你就可以使用$ this-> input-> get('param_name');获取参数。

#6


6  

parse_str($_SERVER['QUERY_STRING'],$_GET); ONLY worked for me after I added the following line to applications/config/config.php:

parse_str($ _ SERVER [ 'QUERY_STRING'],$ _ GET);在将以下行添加到applications / config / config.php之后,仅对我有用:

$config['uri_protocol'] = "PATH_INFO";

$ config ['uri_protocol'] =“PATH_INFO”;

I found $_GET params not to really be necessary in CI, but Facebook and other sites dump GET params to the end of links which would 404 for my CI site!! By adding the line above in config.php, those pages worked. I hope this helps people!

我发现$ _GET params在CI中并不是真的有必要,但Facebook和其他网站将GET参数转储到链接的末尾,这对我的CI网站来说是404!通过在config.php中添加上面的行,这些页面起作用了。我希望这有助于人们!

(from http://www.maheshchari.com/work-to-get-method-on-codeigniter/)

(来自http://www.maheshchari.com/work-to-get-method-on-codeigniter/)

#7


4  

You can enable query strings if you really insist. In your config.php you can enable query strings:

如果你真的坚持,你可以启用查询字符串。在config.php中,您可以启用查询字符串:

$config['enable_query_strings'] = TRUE;

For more info you can look at the bottom of this Wiki page: http://codeigniter.com/user_guide/general/urls.html

有关更多信息,请查看此Wiki页面的底部:http://codeigniter.com/user_guide/general/urls.html

Still, learning to work with clean urls is a better suggestion.

尽管如此,学习使用干净的网址是一个更好的建议。

#8


2  

"don't you get annoyed by the re-send data requests if ever you press back after a form submission"

“如果您在提交表单后再次按下,请不要因重新发送数据请求而烦恼”

you can get around this by doing a redirect from the page that processes your form submission to the success page. the last "action" was the loading of the success page, not the form submission, which means if users do an F5 it will just reload that page and not submit the form again.

您可以通过从处理表单提交的页面到成功页面进行重定向来解决这个问题。最后一个“动作”是加载成功页面,而不是表单提交,这意味着如果用户执行F5,它将只重新加载该页面而不再提交表单。

#9


2  

If your your need to first parameter use it.

如果您需要第一个参数使用它。

$this->uri->segment('3');

And your need second parameter use it

你需要第二个参数使用它

$this->uri->segment('4');

Have your many parameter enhance parameter

有你的很多参数增强参数

#10


1  

allesklar: That is slightly misleading, as scripts and bots can POST data nearly as easily as sending a normal request. It's not a secret, it's part of HTTP.

allesklar:这有点误导,因为脚本和机器人可以像发送正常请求一样轻松地发布数据。这不是秘密,它是HTTP的一部分。

#11


1  

A little bit out of topic, but I was looking for a get function in CodeIgniter just to pass some variables between controllers and come across Flashdata.
see : http://codeigniter.com/user_guide/libraries/sessions.html
Flashdata allows you to create a quick session data that will only be available for the next server request, and are then automatically cleared.

有点偏离主题,但我在CodeIgniter中寻找一个get函数只是为了在控制器之间传递一些变量并遇到Flashdata。请参阅:http://codeigniter.com/user_guide/libraries/sessions.html Flashdata允许您创建仅对下一个服务器请求可用的快速会话数据,然后自动清除。

#12


1  

MY_Input.php :

MY_Input.php:

<?php
// this class extension allows for $_GET access
class MY_Input extends CI_input {

    function _sanitize_globals()
    {
        // setting allow_get_array to true is the only real modification
        $this->allow_get_array = TRUE;

        parent::_sanitize_globals();
    }

}
/* End of file MY_Input.php */
/* Location: .application/libraries/MY_Input.php */

MY_URI.php :

MY_URI.php:

<?php
/*
 | this class extension allows for $_GET access by retaining the
 | standard functionality of allowing query strings to build the 
 | URI String, but checks if enable_query_strings is TRUE
*/
class MY_URI extends CI_URI{

    function _fetch_uri_string()
    {
        if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
        {
            // If the URL has a question mark then it's simplest to just
            // build the URI string from the zero index of the $_GET array.
            // This avoids having to deal with $_SERVER variables, which
            // can be unreliable in some environments
            //
            //  *** THE ONLY MODIFICATION (EXTENSION) TO THIS METHOD IS TO CHECK 
            //      IF enable_query_strings IS TRUE IN THE LINE BELOW ***
            if ($this->config->item('enable_query_strings') === TRUE && is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
            {
                $this->uri_string = key($_GET);
                return;
            }

            // Is there a PATH_INFO variable?
            // Note: some servers seem to have trouble with getenv() so we'll test it two ways
            $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
            if (trim($path, '/') != '' && $path != "/".SELF)
            {
                $this->uri_string = $path;
                return;
            }

            // No PATH_INFO?... What about QUERY_STRING?
            $path =  (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
            if (trim($path, '/') != '')
            {
                $this->uri_string = $path;
                return;
            }

            // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
            $path = str_replace($_SERVER['SCRIPT_NAME'], '', (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO'));
            if (trim($path, '/') != '' && $path != "/".SELF)
            {
                // remove path and script information so we have good URI data
                $this->uri_string = $path;
                return;
            }

            // We've exhausted all our options...
            $this->uri_string = '';
        }
        else
        {
            $uri = strtoupper($this->config->item('uri_protocol'));

            if ($uri == 'REQUEST_URI')
            {
                $this->uri_string = $this->_parse_request_uri();
                return;
            }

            $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
        }

        // If the URI contains only a slash we'll kill it
        if ($this->uri_string == '/')
        {
            $this->uri_string = '';
        }
    }

}
/* End of file MY_URI.php */
/* Location: .application/libraries/MY_URI.php */

#13


1  

my parameter is ?uid=4 and get it with:

我的参数是?uid = 4并得到它:

$this->uid = $this->input->get('uid', TRUE);
  echo $this->uid;

wis

WIS

#14


0  

GET parameters are cached by the web browser, POST is not. So with a POST you don't have to worry about caching, so that is why it is usually prefered.

GET参数由Web浏览器缓存,POST不是。所以使用POST你不必担心缓存,所以这就是为什么它通常是首选。

#15


0  

You can Try this

你可以试试这个

$this->uri->segment('');

#16


0  

Even easier:

更容易:

curl -X POST -d "param=value&param2=value" http://example.com/form.cgi

that plugin's pretty cool though.

这个插件非常酷。