ASP.NET 在 PHP/CodeIgniter 中的 [Authorize] 相当于什么?

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

在 ASP.NET MVC 中,我们在控制器上使用 [Authorize] 属性并将

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

在 web.config 上,在未登录时将请求重定向到登录页面。

是否有类似的方法在未登录 Codeigniter 时进行重定向?

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

您可以检查 session/cookie。如果未找到,则用户未登录。

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');
}

您可以将其包含在控制器构造函数中,以保护该控制器。或者只是放在某些方法中。

希望对您有所帮助。