开发者

How can I show different links based on whether a user is logged in or not?

My k开发者_StackOverflow社区nowledge of PHP is limited yet growing, though I am stuck with this:

<a href="index.php">Home</a> 
<a href="about.php">About</a>
<a href="member_search.php">Members</a> 
<a href="profile.php">View Profile</a>
<a href="login.php">Login</a>

I have played with it, though every attempt has not worked. I want it to check whether a user is logged in or not and show the relevant links.

So "Logged In" would have "Home", "Members", "View Profile", "Edit Profile" and "Logout". "Logged Out" would have "Home", "Register", "Login", etc.


It's hard to answer without seeing how your users are logged in, but I normally use something like:

if (isset($_SESSION['user']))
{
  // do / display stuff for logged in users
}

Based on your comment:

if (isset($_SESSION['id']))
{
  // do / display stuff for logged in users
}



if($userid > 0) //or whatever criteria
{
  echo "members, view profile, etc"; //put in the actual links
}
else
{
  echo "home, register, login, etc"; //put in the actual links
}


Using sessions in php (see session_start) you can keep a state on the server about the user.

When logging in, for example, you can set $_SESSION['username'] to the username; meaning you can ask in other parts if there is a username.
For example by using if (!empty($_SESSION['username'])).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜