How can PHP variables be passed along with a redirect using header()?

When using the header() function to redirect in PHP, you can pass variables along by using query parameters in the redirect URL. This allows you to retrieve the variables on the redirected page using $_GET. Make sure to properly encode the variables to prevent injection attacks.

// Set the variables to pass along
$var1 = 'value1';
$var2 = 'value2';

// Redirect with variables
header("Location: newpage.php?var1=" . urlencode($var1) . "&var2=" . urlencode($var2));
exit();