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'>";
}
}
?>
Related Questions
- How can the PHP code be modified to ensure that only the relevant categories and subcategories are displayed based on the user's selection?
- What best practices should be followed when passing form data to PHP scripts for image generation?
- In the context of Joomla form fields, what are some key considerations when converting an array to a JSON string and ensuring the correct structure for compatibility with Joomla's repeatable field functionality?