How to get the a URL using uri_string() codeignite

2019-06-13 16:12发布

问题:

I want to implement and login authentication function that check every URL when a controller is loaded like this ?

 //get the current URL
        $url = $this->uri->uri_string();

     if(  ($this->ion_auth->logged_in()== false )&& 
          !( in_array($url,$public_allowed_urls) ) ) {
                 redirect('users/login');
            }

I have also tried changing this from AUTO $config['uri_protocol'] = 'REQUEST_URI';

Please how do you do it ..i use latest CI ?

回答1:

You know what ..you will spend your time looking to make work for URI_STRING THING?

But , i have made a solution to you folks ...

get actions and controller first

like "Weclome/Home" ,"users/login",'sell/sell','sell/index','sell/','/'

   //get the current URL
    $controller= $this->uri->segment(1); // controller
    $action = $this->uri->segment(2); // action
    $url = $controller . '/' . $action; 
    //'this returns / sometimes and  also  like sell/ returned'


 if(  ($this->ion_auth->logged_in()== false )&& (in_array($url,$public_allowed_urls) ) )
 {                  
  redirect('users/login');
  }

done men!

Edited:

$this->uri->segment(1); cannot be controller name all time.To get controller name use $this->router->class and for method $this->router->method