简体   繁体   中英

What is the equivalent of ASP.NET's [Authorize] in PHP/CodeIgniter?

In ASP.NET MVC, we use the [Authorize] attribute on a controller and put

<authentication mode="Forms">
  <forms loginUrl="~/Accounts/Login" defaultUrl="~/Accounts/Login" />
</authentication>

on web.config to redirect requests to login page when not logged in.

Is there a similar way of doing redirects when not logged in on Codeigniter?

"Is there a similar way of doing redirects when not logged in on Codeigniter?"

You could just check for a session/cookie. If it's not found the user isn't logged in.

http://www.codeigniter.com/user_guide/libraries/sessions.html http://www.codeigniter.com/userguide3/libraries/sessions.html

if (!$this->session->userdata('user_id')) 
{
    redirect('login');
}

You could include this in the Controller constructor, to secure that controller. Or just put it in certain methods.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM