How can a PHP page automatically check if a user is logged in and display the appropriate menu?
To automatically check if a user is logged in and display the appropriate menu in a PHP page, you can use session variables to track the user's login status. When a user logs in, set a session variable to indicate that they are logged in. Then, on each page where you want to display the menu, check if the session variable is set to determine if the user is logged in and display the appropriate menu accordingly.
<?php
session_start();
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
// Display menu for logged in users
echo '<a href="profile.php">Profile</a> | <a href="logout.php">Logout</a>';
} else {
// Display menu for non-logged in users
echo '<a href="login.php">Login</a> | <a href="register.php">Register</a>';
}
?>
Related Questions
- What are the advantages and disadvantages of using a database for storing images in a PHP-based image gallery?
- In what ways can PHP be used to create a user interface for selecting and sending postcards with selected images?
- How does the compatibility of locales on different operating systems, such as Windows 7, affect the functionality of gettext in PHP?