tags:

views:

72

answers:

6

Hi guys, I need to pass information via a URL to a PHP function that parses the received data.

So basically when they click on the specific link, it will go to the specific page, but also send through the arguments and values for the waiting PHP function to parse them and depending on what the values are do specific functions.

eg: http://www.mydomain.com/arg=val&arg=val&arg=val (not sure about how I built that URL either)

+1  A: 

You can access url parameters that follow the ? in at the end of the URL using the $_GET superglobal, e.g.

// url: http://mydomain.com/mypage.php?arg=Hello
echo $_GET['arg'];
Andy E
Ah, I see! Thanx!
+1  A: 

Parameters passed in the query string are available in the $_GET superglobal array.

Ignacio Vazquez-Abrams
+3  A: 
  • Your can access the parameters via the array $_GET.
    Note: in your example, you basically send just one paramter. PHP cannot handle multiple parameters with the same name.

  • You can build URLs that contain data from variables using string concatenation or string parsing.
    If you browse the web, you will recognize, that the query string (the parameters) always follow a ? in the URL. Read about URLs and the URI syntax.


Probably also interesting for you to read: Variables From External Sources which describes how to deal with data sent via POST or GET or cookies.

Felix Kling
A: 

You can build the URL like the following (note the ? )

http://www.domain.com/page.php?arg1=val&arg2=val

Then in your PHP using the following code by using REQUEST you can get the data from either GET or POST parameters.

$arg1 = $_REQUEST['arg1']; 
Jeff Beck
And `$_COOKIE` if not excluded on php.ini: http://php.net/manual/en/reserved.variables.request.php
Rui Carneiro
Great point that I always forget about.
Jeff Beck
A: 

parse_str is your friend here (pass it the $_GET and it's happy)

Also, i would stay away from using $_REQUEST

M4rk
Thanx, will look into it!
+2  A: 

Generate URL:

<?php

$data = array(
    'first_name' => 'John',
    'last_name' => 'Doe',
    'motto' => 'Out & About',
    'age' => 33
);

$url = 'http://example.com/?' . http_build_query($data);

?>

Pick data:

<?php

$data = array();

if( isset($_GET['first_name']) ){
    $data['first_name'] = $_GET['first_name'];
}
// Etc.

?>
Álvaro G. Vicario
Thank you for taking the time to post the sample code, it worked!