获得使用CodeIgniter的URL参数
-
22-07-2019 - |
题
我知道笨默认关闭GET参数。
但是,拥有一切在POST做,你不给被重新发送数据请求生气,如果你永远按一个表单提交后回来?
这让我很烦,但我不知道如果我想允许GET纯粹出于这个原因。
难道这么大的安全问题,让GET参数吗?
解决方案
当我第一次开始使用CodeIgniter工作,不使用GET真的把我摔下为好。但后来我意识到,你可以通过使用内置的 URI类操纵URI模拟GET参数一>。这太棒了,这让您的网址,更好看。
或者,如果你真的需要得到工作,就可以把这个到你的控制器:
parse_str($_SERVER['QUERY_STRING'], $_GET);
哪些将把变量放回GET阵列。
其他提示
这为我工作:
<?php
$url = parse_url($_SERVER['REQUEST_URI']);
parse_str($url['query'], $params);
?>
$params
数组包含后传递的参数?字符
现在它的工作原理确定从笨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;
此功能是相同的交函数,它仅取出得到的数据:
$this->input->get()
您只需要启用它在config.php文件,你可以使用$this->input->get('param_name');
得到的参数。
parse_str($_SERVER['QUERY_STRING'],$_GET);
只为我工作后,我加入以下行应用程序/配置/ config.php文件:
$config['uri_protocol'] = "PATH_INFO";
我发现$ _GET PARAMS不是真的在CI必要的,但Facebook等网站转储GET PARAMS以链接的一端将404我的CI网站!通过在config.php加入上面的线,这些网页的工作。我希望这可以帮助的人!
您可以启用查询字符串,如果你真的坚持。 在你的config.php文件,你可以启用查询字符串:
$config['enable_query_strings'] = TRUE;
有关更多信息,你可以看看这个Wiki页面的底部: http://codeigniter.com/user_guide/general/urls.html
不过,学习用干净的网址的工作是一个更好的建议。
“你不给被重新发送数据请求生气,如果你永远按一个表单提交后回来”
您可以通过执行从处理表单提交成功页面的网页的重定向,解决这个问题。最后一个“行动”是成功的页面加载,而不是表单提交,这意味着如果用户做一个F5它只会重新加载页面,而不是再次提交表单。
如果你对你的需要第一个参数使用它。
$this->uri->segment('3');
和您的需要第二个参数使用它
$this->uri->segment('4');
让你的许多参数提升参数
allesklar:即略微误导,因为脚本和机器人可以几乎一样容易POST数据与发送普通请求。这不是一个秘密,这是HTTP的一部分。
一点点出来的话题,但我一直在寻找一个笨get函数只是传递一些变量控制器之间以及遇到Flashdata。点击 请参阅: http://codeigniter.com/user_guide/libraries/sessions.html 点击 Flashdata允许你创建一个仅可用于下一个服务器请求一个快速的会话数据,然后被自动清零。
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:
<?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 */
我的参数为uid = 4,并用得到它:
$this->uid = $this->input->get('uid', TRUE);
echo $this->uid;
WIS
GET参数由网络浏览器高速缓存,POST不是。因此,与后不必担心缓存,所以这就是为什么它通常优先。
您可以试试这个
$this->uri->segment('');
即使更容易:
curl -X POST -d "param=value¶m2=value" http://example.com/form.cgi
这是插件的很酷,但。
执行此以下。为我工作。我从一个选择框和文本框的另一个值。然后点击链接我把整个数据在Javascript函数和使用javascript重定向。
//Search Form
$(document).ready (function($){
$("#searchbtn").click(function showAlert(e){
e.preventDefault();
var cat = $('#category').val();
var srch = $('#srch').val();
if(srch==""){
alert("Search is empty :(");
}
else{
var url = baseurl+'categories/search/'+cat+'/'+srch;
window.location.href=url;
}
});
});
上面的代码为我工作。