Html 使用 CodeIgniter 获取 URL 中的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/334708/
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
GET parameters in the URL with CodeIgniter
提问by Jon Winstanley
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 参数是一个很大的安全问题吗?
回答by Jelani Harris
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 Class操作 URI 来模拟 GET 参数。这太棒了,它使您的 URL 看起来更好。
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 数组中。
回答by Murtaza Baig
This function is identical to the post function, only it fetches get data:
此函数与 post 函数相同,只是它获取 get 数据:
$this->input->get()
回答by Roberto Gerola
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
数组包含在 ? 之后传递的参数。特点
回答by almix
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;
回答by Sumit
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');
来获取参数。
回答by Benjamin Sussman
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 参数在 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/)
回答by Tomas
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.
尽管如此,学习使用干净的 url 是一个更好的建议。
回答by Md.Jewel Mia
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
有你的多参数增强参数
回答by stef
"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,它只会重新加载该页面,而不会再次提交表单。
回答by Rwahyudi
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 允许您创建快速会话数据,该数据仅可用于下一个服务器请求,然后自动清除。