在php中使用ajax方法时,需要获取当前页面的URL


Need to get the current page URL while using ajax method in php

我需要获取当前页面的URL。

事实上,我在Codeigner框架中单击当前页面中的链接时使用了弹出框在这里,我使用ajax响应方法在弹出框中显示了一些内容。在该内容中,我已编码为

$this->uri->segment(2)

在这里,我只得到了ajax方法中给出的url,但我需要得到我保存弹出框的页面的url。

在代码点火器中:

获取完整的URL:

通过使用current_url()方法,您可以获得页面的当前页面URl。

示例:

$page_url=current_url();
echo $page_url;

用于获取URI段:

通过使用以下语句,您可以获得URI中的段。

语法:

$this->uri->segment(n);
       where n=1 for controller
                 n=2 for method
                 n=3,4,5,6........ for parameters.

示例:

URL:   **http://example.com/project_name/login/home**
echo $this->uri->segment(1); //It will returns login as output
echo $this->uri->segment(2); //It will returns home as output

用于获取当前控制器和方法名称:

$this->router->fetch_class(); //It will returns controller name
$this->router->fetch_method(); //It will return current method name.

在jQuery中:
您可以使用jQuery获取当前页面的url,如:

var pathname = window.location.pathname;

在codeigniter中,您可以使用URI类来获取当前的URI段。

$currentUrl = base_url().$this->uri->uri_string();

在这种情况下,您需要在配置文件中设置base_url,并加载url助手来使用base_url()函数。

获取当前页面URL:例如:

www.domain.com/index.php/controller/function
        $this->load->helper("url");
        $current_uri = uri_string(); 
echo $current_uri; // this would echo '/controller/function'

您需要查看referrer来获得弹出窗口的url(而不是目标AJAX url)。这可以使用UserAgent类($this->agent->referrer())来完成

请参阅https://ellislab.com/codeigniter/user-guide/libraries/user_agent.html