Skip to content
PHP Logo

What are the potential pitfalls of redirecting to an external page while still checking which link the user clicked on in PHP?

When redirecting to an external page in PHP while still needing to check which link the user clicked on, the potential pitfall is that the user's browser may not follow the redirect if the script outputs any content before the redirect header is sent. To solve this issue, you can use output buffering to capture any output before the redirect header is sent.

php
<?php
ob_start(); // Start output buffering

// Check which link the user clicked on
$linkClicked = $_GET['link'];

// Redirect to external page based on the link clicked
if ($linkClicked == 'link1') {
    header('Location: https://www.externalpage1.com');
    exit();
} elseif ($linkClicked == 'link2') {
    header('Location: https://www.externalpage2.com');
    exit();
}

ob_end_flush(); // Flush the output buffer
?>