CodeIgniter -> Get current URL relative to base url

后端 未结 10 1132
情话喂你
情话喂你 2020-12-29 05:08

Tried URI::uri_string() but can\'t get it to work with the base_url.

URL: http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi
Returns: dr

相关标签:
10条回答
  • 2020-12-29 05:24

    you can use the some Codeigniter functions and some core functions and make combination to achieve your URL with query string. I found solution of this problem.

    base_url($this->uri->uri_string()).strrchr($_SERVER['REQUEST_URI'], "?");
    

    and if you loaded URL helper so you can also do this current_url().strrchr($_SERVER['REQUEST_URI'], "?");

    0 讨论(0)
  • 2020-12-29 05:37
    // For current url
    echo base_url(uri_string());
    
    0 讨论(0)
  • 2020-12-29 05:37

    Try to use "uri" segments like:

    $this->uri->segment(5);   //To get 'ahahaha'
    $this->uri->segment(6);   //To get 'hihihi
    

    form your first URL...You get '' from second URl also for segment(5),segment(6) also because they are empty.

    Every segment function counts starts form localhost as '1' and symultaneous segments

    0 讨论(0)
  • 2020-12-29 05:40

    Running Latest Code Igniter 3.10

    $this->load->helper('uri'); // or you can autoload it in config
    
    print base_url($this->uri->uri_string());
    
    0 讨论(0)
  • 2020-12-29 05:41

    In CI v3, you can try:

    function partial_uri($start = 0) {
        return join('/',array_slice(get_instance()->uri->segment_array(), $start));
    }
    

    This will drop the number of URL segments specified by the $start argument. If your URL is http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi, then:

    partial_uri(3);  # returns "ahahaha/hihihi"
    
    0 讨论(0)
  • 2020-12-29 05:44

    For the parameter or without parameter URLs Use this :

    Method 1:

     $currentURL = current_url(); //for simple URL
     $params = $_SERVER['QUERY_STRING']; //for parameters
     $fullURL = $currentURL . '?' . $params; //full URL with parameter
    

    Method 2:

    $full_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    

    Method 3:

    base_url(uri_string());
    
    0 讨论(0)
提交回复
热议问题