What are the best practices for passing variables between PHP pages using GET method?
When passing variables between PHP pages using the GET method, it is important to properly sanitize and validate the data to prevent security vulnerabilities. One common practice is to use the `urlencode()` function to encode the variables before passing them in the URL. Additionally, always check for the existence of the variable before using it to avoid errors.
// Sending page
$var1 = 'value1';
$var2 = 'value2';
$url = 'page2.php?var1=' . urlencode($var1) . '&var2=' . urlencode($var2);
header('Location: ' . $url);
exit;
// Receiving page (page2.php)
$var1 = isset($_GET['var1']) ? $_GET['var1'] : '';
$var2 = isset($_GET['var2']) ? $_GET['var2'] : '';
echo 'Var1: ' . $var1 . '<br>';
echo 'Var2: ' . $var2;