How can PHP developers ensure that user-specific content is displayed correctly based on login status?
PHP developers can ensure that user-specific content is displayed correctly based on login status by implementing a session management system. This involves setting session variables upon successful login and checking these variables to determine the user's login status. By using conditional statements to display different content based on the user's login status, developers can ensure that only authenticated users can access certain content.
<?php
session_start();
// Check if user is logged in
if(isset($_SESSION['user_id'])) {
// Display user-specific content
echo "Welcome, ".$_SESSION['username']."!";
} else {
// Display login form
echo "<form action='login.php' method='post'>
<input type='text' name='username' placeholder='Username'>
<input type='password' name='password' placeholder='Password'>
<button type='submit'>Login</button>
</form>";
}
?>