如何在CakePHP中使用Cookie进行身份验证?
我正在尝试使用域中其他页面设置的cookie来验证用户。 假设我使用cakephp编写了needpassword.example.com, cookie由auth.example.com生成(使用Perl CGI程序) 要登录needpassword.example.com,我需要重定向到auth.example.com以设置cookie,然后使用CakePHP解析cookie 如何解析此cookie?我如何修改Auth组件来实现这些功能 我如何重写Auth类以转到Auth.example.com进行身份验证,而不使用用户模型?通过重写Auth.php中的identify方法如何在CakePHP中使用Cookie进行身份验证?,cakephp,cookies,authentication,Cakephp,Cookies,Authentication,我正在尝试使用域中其他页面设置的cookie来验证用户。 假设我使用cakephp编写了needpassword.example.com, cookie由auth.example.com生成(使用Perl CGI程序) 要登录needpassword.example.com,我需要重定向到auth.example.com以设置cookie,然后使用CakePHP解析cookie 如何解析此cookie?我如何修改Auth组件来实现这些功能 我如何重写Auth类以转到Auth.example.co
非常感谢。假设我理解你的问题。。。只要auth.example.com使用域“.example.com”设置cookie,用户浏览器就会将cookie与请求一起发送到needpassword.example.com,您就可以通过以下方式在PHP脚本中访问cookie:
$auth = $_COOKIE['auth'];
setcookie( "auth", "value", time() + 300, "/", ".example.com" );
然后,您可以使用以下内容对cookie进行更改:
$auth = $_COOKIE['auth'];
setcookie( "auth", "value", time() + 300, "/", ".example.com" );
(注意:time()+300将Cookie的到期日期设置为5分钟,您可能需要更改此设置)由于您的需求与AuthComponent最初的预期设计不符,因此您有两个选择 首先,如果它确实不适合您的需要,您可以创建并维护自己的AuthComponent。通过将
/cake/libs/controller/components/auth.php
复制到/app/controller/components/auth.php
来实现这一点
这将允许您完全重写组件,但缺点是升级时您将不再收到AuthComponent的更新
其次,您可以使用以下模式扩展CakePHP中的任何内容:
// save as: /app/controllers/components/app_auth.php
App::import('Component', 'Auth');
class AppAuthComponent extends AuthComponent {
function identify($user = null, $conditions = null) {
// do stuff
return parent::indentify($user, $conditions);
}
}
。。并用AppAuthComponent
替换控制器中的所有AuthComponent
实例
- 您只需要定义要替换的方法
- 您可以在方法使用期间的任何时候从原始AuthComponent(甚至是您重新定义的AuthComponent)运行方法
- 方法参数应保持一致性
- 如果希望添加更多方法参数,请将它们放在API参数之后,例如:
函数标识($user=null,$conditions=null,$custom=array()){…}
这种方法允许您进行特定于应用程序的定制,同时在必要时仍然使用core中定义的最新方法。Hi,它看起来非常简单,这正是我想要的。那么,我如何重写Auth类以转到Auth.example.com进行身份验证,而不使用用户模型呢?最好使用控制器或自定义AuthComponent中的CookieComponent,而不是PHP的setcookie()方法。是否检查过使用绝对URL(ie.)设置AuthComponent::loginAction参数是否有效?