Passing multiple variables to another page in url
I'm passing a variable to another page in a url using sessions like this but it seems that I can't concatenate another variable to the same url and retrieve it in the next page successfully
Page 1
session_start();
$event_id = $_SESSION['event_id'];
echo $event_id;
$url = "http://localhost/main.php?email=" . $email_address . $event_id;
Page 2
if (isset($_GET['event_id'])) {
$event_id = $_GET['event_id'];}
echo $event_id;
echo $event_id
shows an error Undefined variable
on page 2 but if I use just the event_id
in the $url
like here
$url = "http://localhost/main.php?event_id=" . $event_id;
That works fine, but I need to be able to use both variables in the url so that page 2 can retrieve get them.
Solution 1:
Use the ampersand &
to glue variables together:
$url = "http://localhost/main.php?email=$email_address&event_id=$event_id";
// ^ start of vars ^next var
Solution 2:
Short answer:
This is what you are trying to do but it poses some security and encoding problems so don't do it.
$url = "http://localhost/main.php?email=" . $email_address . "&eventid=" . $event_id;
Long answer:
All variables in querystrings need to be urlencoded to ensure proper transmission. You should never pass a user's personal information in a url because urls are very leaky. Urls end up in log files, browsing histories, referal headers, etc. The list goes on and on.
As for proper url encoding, it can be achieved using either urlencode()
or http_build_query()
. Either one of these should work:
$url = "http://localhost/main.php?email=" . urlencode($email_address) . "&eventid=" . urlencode($event_id);
or
$vars = array('email' => $email_address, 'event_id' => $event_id);
$querystring = http_build_query($vars);
$url = "http://localhost/main.php?" . $querystring;
Additionally, if $event_id
is in your session, you don't actually need to pass it around in order to access it from different pages. Just call session_start()
and it should be available.
Solution 3:
Your first variable declartion must start with a ?
while any additional must be concatenated with a &
-
single variable URL
http://www.example.com/page?var=foo
-
multiple variable URL
http://www.example.com/page?var=foo&var2=bar
Solution 4:
Pretty simple but another said you dont pass session variables through the url bar
1.You dont need to because a session is passed throughout the whole website from header when you put in header file
2.security risks
Here is first page code
$url = "http://localhost/main.php?email=" . urlencode($email_address) . "&eventid=" . urlencode($event_id);
2nd page when getting the variables from the url bar is:
if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['eventid']) && !empty($_GET['eventid'])){
////do whatever here
}
Now if you want to do it the proper way of using the session you created then ignore my above code and call the session variables on the second page for instance create a session on the first page lets say for example:
$_SESSION['WEB_SES'] = $email_address . "^" . $event_id;
obvious that you would have already assigned values to the session variables in the code above, you can call the session name whatever you want to i just used the example web_ses , the second page all you need to do is start a session and see if the session is there and check the variables and do whatever with them, example:
session_start();
if (isset($_SESSION['WEB_SES'])){
$Array = explode("^", $_SESSION['WEB_SES']);
$email = $Array[0];
$event_id = $Array[1]
echo "$email";
echo "$event_id";
}
Like I said before the good thing about sessions are they can be carried throughout the entire website if this type of code in put in the header file that gets called upon on all pages that load, you can simple use the variable wherever and whenever. Hope this helps :)