开发者

Hide Navigation Links From Non-Members

I have an include file that s开发者_StackOverflow社区tores the navigation links for my website. I just built a members-only section and I would like to make it so that some of the links in the navigation bar are visible only if someone is logged in. What are some good ways of going about doing this?


It's all depending on how you check whether the user is logged in or a visitor, if you have a variable that tracks so

eg. $isLoggedIn to track whether the user is logged in, you can do something like the below

<a href='home'>Home</a> |
<?php if ($isLoggedIn) { ?>
<a href='member.php'>Member Area</a> |
<?php } ?>
<a href='about'>About Us</a>

as for the different set of users, you might have a field called member_group in database, which member_group is in number. you can then have

<?php include "nav_group_{$memberGroup}.php" ?>

To include appropriate links

Do remember to do additional checking of $isLoggedIn inside member.php as well!


Since you say that you have built a members-only section, I guess you're already using sessions to track if a user is logged in or not. In the include file, check if the user's session exists and display the appropriate links.

As a more robust solution, if you wanted to show different sets of links to different logged in users, you would set up a DB table with flags for each user or groups of users indicating what links should be displayed for them. In the include file, you would check the user's permissions and display the appropriate links.


If you have the html source in DB, you can use a preg_replace:

$html = preg_replace('#<a[^>]*>.*?</a[^>]*>#is','<span class="linknomember">Only members...</span>',$html);

If you have a bbcode:

$html = preg_replace('#\[url[^\]]*].*?\[/url[^\]]*\]#is','<span class="linknomember">Only members...</span>',$html);

Too, if you want ban a some url:

$html = preg_replace('#http[s]?://(.*?)+[\b]#is','<span class="linknomember">Only members...</span>',$html);


check if username/ userid exists in session and based on that display/hide the navigation block..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜