Reputation: 5636
I have a range of similar pages that have a URL along the lines of www.mydomain.com/group?id=1
.
From each of these pages there is a form that posts it's values to the server. I need to work out what the id
of the group was when the form is posted. I'm aware of being able to use $_SERVER['HTTP_REFERER']
and then maybe I could use a regex to get the id
. However, I wondered if there was anything in PHP that would allow you to get the previous $_GET variables?
Alternatively, do people think it is a much better idea to store the current group id as a session variable?
Upvotes: 0
Views: 3364
Reputation: 78971
Yes, Session is the way to go to . Store them and get the groups on every other page, using session . This is a proper way.
Despite, it is also possible to make $_GET available for every page. Using two ways (AFAIK).
Create the exact same URL String with the parameters and send them along, as you are redirecting from page to page.
parse_url()
to get only the query string and pass them alongUse Session to back up the $_GET and reassign it to $_GET on every page. Put the below snippet or every page you redirect to.
if(isset($_SESSION['GET_BACKUP']) { //Check if there was a backup before
$_GET = $_SESSION['GET_BACKUP']; //if yes use it
}
if(isset($_GET) && count($_GET)) { //if not and GET value is sent
$_SESSION['GET_BACKUP'] = $_GET; //backup it
}
// Now use the get as you used to via $_GET
Following this way, you will not get an attached data in the URL, which might be undesirable.
In case you are going with the second option, you should remember that the solution I provided is an demo and will not fit for more than one $_GET
group. For multiple pages and storing their SESSIONS, you have to define separate keys to identify the backup. Kinda like
$_SESSION['mypage.php']['GET_BACKUP'] = $_GET;
Upvotes: 3
Reputation: 10529
I wondered if there was anything in PHP that would allow you to get the previous $_GET variables
There isn't. However, you can use SESSION to save all GET parameters manually.
//first page
$_SESSION["pages"][] = $_GET;
//second page, before setting GET parameters for current page
$last = count($_SESSION["pages"]) - 1;
if ($_SESSION["pages"][$last]["whatever"]) {
}
Upvotes: 1
Reputation: 32145
A multi-page form is commonly termed a 'wizard'. If the following pages depend on subsequent values the most common solution is to store the form-pieces in $_SESSION
.
Upvotes: 2