What are the different ways to pass variables between pages in PHP without using sessions?
When passing variables between pages in PHP without using sessions, you can use methods like URL parameters, POST requests, GET requests, cookies, or hidden form fields. These methods allow you to transfer data from one page to another without relying on server-side sessions.
// Using URL parameters
// Page 1
$variable = 'value';
<a href="page2.php?var=<?php echo $variable; ?>">Go to Page 2</a>
// Page 2
$variable = $_GET['var'];
echo $variable;
// Using POST requests
// Page 1
<form method="post" action="page2.php">
<input type="hidden" name="var" value="<?php echo $variable; ?>">
<input type="submit" value="Submit">
</form>
// Page 2
$variable = $_POST['var'];
echo $variable;
// Using cookies
// Page 1
setcookie('var', $variable, time() + 3600, '/');
// Page 2
$variable = $_COOKIE['var'];
echo $variable;
// Using hidden form fields
// Page 1
<form method="post" action="page2.php">
<input type="hidden" name="var" value="<?php echo $variable; ?>">
<input type="submit" value="Submit">
</form>
// Page 2
$variable = $_POST['var'];
echo $variable;