Sometimes, we want to redirect with POST data with PHP.
In this article, we’ll look at how to redirect with POST data with PHP.
How to redirect with POST data with PHP?
To redirect with POST data with PHP, we can store the data in sessions.
For instance, we write
session_start();
$_SESSION["email"] = "awesome@email.com";
header("Location: page_b.php");
on page_a.php
to call session_start
to start the session.
Then we set $_SESSION["email"]
to the value we want.
Next, we call header
with a string to redirect to page_b.php
.
Then on page_b.php
, we write
session_start();
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
unset($_SESSION["email"]);
session_destroy();
to start the session with session_start();
.
Then we print the raw $_SESSION
data with print_r
.
We then remove the session entry with key email
with unset
.
And we call sesion_destroy
to destroy the session.
Conclusion
To redirect with POST data with PHP, we can store the data in sessions.