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();
Keywords
Related Questions
- What best practices should be followed when incorporating PHP scripts to manage CSS file selection on a website?
- What alternative approach can be used to assign the variable TextBadword without searching for the string "[...]" in the filtered text?
- What are common issues when creating email templates with PHP?