What is the best way to display profile pictures only on the user's profile page in PHP?
To display profile pictures only on the user's profile page in PHP, you can check if the current user is viewing their own profile by comparing the user ID in the URL with the logged-in user's ID. If they match, display the profile picture. Here is a code snippet to implement this:
<?php
session_start();
// Check if user is logged in
if(isset($_SESSION['user_id'])) {
$loggedInUserId = $_SESSION['user_id'];
// Get user ID from URL
$urlUserId = $_GET['user_id'];
// Display profile picture if user is viewing their own profile
if($loggedInUserId == $urlUserId) {
echo "<img src='profile_picture.jpg' alt='Profile Picture'>";
}
}
?>