CakePHP, file uploading, and inconsistent Auth
I have an application built in CakePHP that is doing file uploads. For each file upload, I know the user's id, so I grab it from $this->Auth->user()
Now, what I have found is that when I am on th开发者_开发问答e page, I will upload files while logged in but sometimes when I print_r the $this->Auth->user() it returns nothing back, and then the next time I try a file upload it will come back, all without e logging in or out. It seems extremely inconsistent, in that sometimes it is in there but other times it doesn't see it.
What am I missing? Thanks!
Why don't set a var in your app_controller to $this->Auth->userModel
so it's accessible by the rest of the application.
In my app_controller I call the below in before_filter
to set $current_user
.
/**
* Sets a value for current user $current_user.
* @return boolean
*/
function __setCurrentUser() {
$user = null;
if ($user = $this->Auth->user()) {
$this->set('current_user', $user[$this->Auth->userModel]);
return true;
} else {
return false;
}
}
Elsewhere in my app, I can access $current_user
's id via $current_user['id']
.
You can also always grab userinfo from the Session that is created by AuthComponent. If $this->Auth->user('id') is empty.. I'm wondering if there is actually a valid session! Are you sure you haven't been logged out in the meantime and the page isn't requiring a login for some reason?
精彩评论