php 在 Codeigniter 中获取 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11830779/
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 url in Codeigniter
提问by Zoha Ali Khan
I am using codeigniter for my project. To get the uri segments I know I can use
我正在为我的项目使用 codeigniter。要获取我知道可以使用的 uri 段
$this->uri->segment();
but my case is a bit different
但我的情况有点不同
My url looks like
我的网址看起来像
localhost/mediabox/home/box/21
but once I go to this url a popup form apears in which the user provide a key to access this page and I validate the key using ajax method which is inside my home controller validate_key function
但是一旦我转到这个 url,就会出现一个弹出表单,其中用户提供了访问此页面的密钥,然后我使用家庭控制器 validate_key 函数中的 ajax 方法验证了该密钥
when I echo the url it gives me localhost/home/validate_key
当我回显 url 时,它给了我 localhost/home/validate_key
while calling valiate_key of the home controller how can I get the 21 from the url wrritten in the url bar?
在调用家庭控制器的 valiate_key 时,如何从 url 栏中写入的 url 中获取 21?
Any ideas?
有任何想法吗?
Thanks
谢谢
回答by Abdulaziz
The problem:
问题:
It's not a bug, it's a natural behavior.
这不是错误,这是一种自然行为。
Consider the following:
考虑以下:
you request the
validate_keyfunction from the server by typing the URL in your address bar.current_url()returnslocalhost/blabla/validate_key. No AJAX involved.requesting
validate_keywith AJAX. The same PHP code will be executed.
thecurrent_url()will change tolocalhost/blabla/validate_key
even though your browser's address bar is showinglocalhost/blabla/box/21.
您可以
validate_key通过在地址栏中键入 URL 来从服务器请求该功能。current_url()返回localhost/blabla/validate_key。不涉及 AJAX。validate_key用 AJAX请求。将执行相同的 PHP 代码。即使浏览器的地址栏显示
,current_url()也会更改为。localhost/blabla/validate_keylocalhost/blabla/box/21
So, what does this means? It means the Codeigniter base_url()doesn't care about your address bar, it cares about the function it is in, whether it was called via ajaxor normal request.
so as long this function is being executed, the URL is pointing to it.
那么,这意味着什么?这意味着 Codeigniterbase_url()不关心您的地址栏,它关心它所在的功能,无论它是通过ajax还是正常请求调用的。
所以只要这个函数正在执行,URL 就指向它。
The solution:
解决方案:
My favorite solution to such a case, is to simply create a hidden input.
对于这种情况,我最喜欢的解决方案是简单地创建一个隐藏的输入。
Simply, when a user requests the boxfunction. you're showing him a popup form. so add a hidden_inputfield, give it a name and a value of 21(depends).
简单地说,当用户请求该box功能时。你向他展示了一个弹出式表单。所以添加一个hidden_input字段,给它一个名称和值 21(取决于)。
For example(you should tailor this to your specific needs):
例如(您应该根据您的特定需求进行调整):
Add this to your form in the view that get displayed by the boxfunction:
在box函数显示的视图中将此添加到您的表单中:
form_hidden("number", $this->uri->segment(3));;
form_hidden("number", $this->uri->segment(3));;
Now these data will be sent to your validate_keyfunction. How do we access it? It's simple!
现在这些数据将被发送到您的validate_key函数。我们如何访问它?这很简单!
function validate_key(){
$this->input->post("number");//returns 21 or whatever in the URL.
//OR if the form sends GET request
$this->input->get("number");//return 21 or whatever in the URL.
/*
*Or , you can do the following it's considered much safer when you're ONLY
*expecting numbers, since this function(intval) will get the integer value of
*the uri segment which might be a destructive string, so if it's a string
*this function will simply return 0.
*/
$number = intval($this->input->post("number"));//returns 21 or whatever in the URL.
//Or if it it GET request:
$number = intval($this->input->get("number"));//returns 21 or whatever in the URL.
}
回答by peacemaker
It looks like you've used .htaccess to remove the index.phppart of the url. So, when you navigate to localhost/mediabox/home/box/21you're passing the value 21 to the function named boxin the controller named home
看起来您已经使用 .htaccess 删除index.php了 url的一部分。因此,当您导航到时,localhost/mediabox/home/box/21您将值 21 传递给名为box的控制器中命名的函数home
If you want to keep that value within the validate_keyfunction, just pass it through when calling it:
如果您想将该值保留在validate_key函数中,只需在调用它时传递它:
function box($param)
{
//$param = 21
$this->validate_key($param);
}
回答by Gautam3164
Its better and suggetsed to use hidden field and post the value when it is needed.
最好使用隐藏字段并在需要时发布值。

