Display Admin Link On Every Page
My question is about how do I place a link on the side of every page that leads to the admin page once someone has logged on to my application successfully? I have a sample site I just built, and I would like a link to the admin page available in the navigation column to the right of the page which is displayed site-wide. But if the person is not logged in, they don't see the link, but will continue to see the usual links.
My background is totally different from web devel开发者_StackOverflow社区opment, so forgive my stupid question. I'm using PHP and MySQL for the application.
Without seeing how you display your menu or what key is used for the session, Ill assume some things:
<?php
session_start();
// do your login stuff and set session as logged in
$_SESSION['logged_in'] = true;
?>
Then in your menu or how ever you display it:
<?php
//navigation column
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true){echo '<a href="admin.php">Admin</a>';}
//navigation column continue with rest of links
?>
Or the ternary operator assign link to a variable
<?php
$adminLink = (isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true)?'<a href="admin.php">Admin</a>':'';
echo $adminLink;
?>
You should use a session variable to track the user's session and see if they are logged in.
if(isset($_SESSION['id'])) echo '<a href="/admin">Admin Area</a>';
精彩评论