What are the potential pitfalls in implementing a system where names change color based on a status in PHP?
One potential pitfall in implementing a system where names change color based on a status in PHP is not properly sanitizing user input, which can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To solve this issue, make sure to validate and sanitize any user input before using it to dynamically change the color of names.
// Sanitize user input before using it to change name color
$status = isset($_GET['status']) ? htmlspecialchars($_GET['status']) : '';
// Define colors based on status
if ($status == 'active') {
$color = 'green';
} elseif ($status == 'inactive') {
$color = 'red';
} else {
$color = 'black';
}
// Output the name with the dynamically assigned color
echo '<span style="color: ' . $color . ';">John Doe</span>';