Passing a value in the URL to a form: this link will pass a value via the URL to the contact form page for processing http://domain.com/contact-form?name=john it will make the variable name available even if the tab is closed until the session is destroyed when the form is submitted
<?php
// start the session
session_start();
// and then make sure it's already not stored in the session
// (if it's passed in the $_GET) and if not already saved in the session then store it:
if (isset($_GET["name"])) {
if (!isset($_SESSION["name"])) {
$_SESSION["name"] = $_GET["name"];
}
}
// Check if the form was submitted:
if (isset($_POST['submit'])){
// Unset all of the session variables.
$_SESSION = array();
// Finally, destroy the session.
session_destroy();
echo "Session was destroyed";
}
?>
<!-- echo the session variable if it was set otherwise print empty value -->
<h1>Session Value: <?php echo (isset($_SESSION['name']) ) ? $_SESSION['name'] : ''; ?> </h1>
<!-- This is our form -->
<form method="post" >
<input type="submit" name="submit" value="Delete session">
</form>