Welcome Guest, Not a member yet? Register   Sign In
How do I restrict access to the controller?
#1

[eluser]puffnstuff[/eluser]
The users and public can access a profile by going to example.com/user/username but I would like it to redirect to the home page if they go to example.com/user/

my controller

Code:
public function index($user)
    {
    
$data['url'] = $this->user_model->url($user);

  
if ($this->user_model->url($user) == TRUE ) {

// show the view

}
     else {
    
     redirect ('/');
     }
   }
#2

[eluser]puffnstuff[/eluser]
Figured out a solution by doing this $route['user'] = "user/main"; and doing a redirect in the main function. I am sure I should use htaccess but this works so I will go with it.
#3

[eluser]boltsabre[/eluser]
This is easier, saves keeping track of this stuff in your $routes file

Code:
public function index(){
   $user = $this->uri->segment(2);
   if( !isset ($user)){
      // redirect them where ever you want
   }else{
      // everything is fine, lets continue
   }
}

I personally don't like to uri segments as method arguments, if for whatever reason they are missing CI throws a "argument missing" (cannot remember the exact text off the top of my head) php error. The above method lets you handle missing arguments gracefully (in my humble opinion).
#4

[eluser]puffnstuff[/eluser]
Thanks it worked with a few modifications instead of
Code:
if( !isset ($user))
I changed it to
Code:
if($user == NULL )




Theme © iAndrew 2016 - Forum software by © MyBB