Showing part of a page only for logged in users
I"m trying开发者_如何学C to knock up a very a way to show part of a page, only when a user is logged in, using cookies. Any idea on how to proceed?
Sessions? http://www.php.net/manual/en/book.session.php
Remeber that cookies can be disabled in the browser. Use the $_SESSION variable instead of cookies.
In my Authentication class all users are stored in a database with an access level. On each page the Authentication class uses the $_SESSION variable to get the loged in user and retrieve him/her from the database into a user object. If no one is loged in a default user object for guests are used instead.
In the HTML generation I only check access level before generating "protected" data.
if ($User->AccessLevel >= 30) {
// stuff only available to users with access level 30 and higher
}
if(isset($_COOKIE['example-cookie']))
show_stuff_for_logged_in_users();
And to set cookie use setcookie()
.
Although keep in mind that user can manually set cookies so can't trust them. Using sessions would be better.
精彩评论