Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 996
zadubz Avatar asked Oct 27 '12 17:10

zadubz


People also ask

How do I pass multiple variables in URL?

$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.

How do you pass a variable in a link?

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.

How pass multiple values by link in PHP?

Try htmlspecialchars(http_build_query(array('choice' => 'search', 'cat' => $cat, 'subcat' => $subcat, 'srch' => $srch, 'page' => $next))) to generate a well-formed query string.


Video Answer


2 Answers

Use the ampersand & to glue variables together:

$url = "http://localhost/main.php?email=$email_address&event_id=$event_id"; //                               ^ start of vars      ^next var 
like image 122
JvdBerg Avatar answered Sep 19 '22 22:09

JvdBerg


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.

like image 27
Asaph Avatar answered Sep 20 '22 22:09

Asaph