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.
$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.
To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.
Try htmlspecialchars(http_build_query(array('choice' => 'search', 'cat' => $cat, 'subcat' => $subcat, 'srch' => $srch, 'page' => $next))) to generate a well-formed query string.
Use the ampersand &
to glue variables together:
$url = "http://localhost/main.php?email=$email_address&event_id=$event_id"; // ^ start of vars ^next var
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With