PHP Get URL Parameter and its Value

20,579

Solution 1

Why not using $_GET global variable?

foreach($_GET as $key => $value)
{  
  // do your thing.
}

Solution 2

$queryArray = array();
parse_str($_SERVER['QUERY_STRING'],$queryArray);

var_dump($queryArray);

Solution 3

php has a predefined variable that is an associative array of the query string ... so you can use $_GET['<name>'] to return a value, link to $_GET[] docs

You really need to have a look at the Code Injection page on wikipedia you need to ensure that any values sent to a PHP script are carefully checked for malicious code, PHP provides methods to assist in preventing this problem, htmlspecialchars

Solution 4

You should use the $_GET array.

If your query string looks like this: ?foo=bar&fuz=baz

Then you can access the values like this:

echo $_GET['foo']; // "bar"
echo $_GET['fuz']; // "baz"
Share:
20,579
tonoslfx
Author by

tonoslfx

Updated on July 09, 2022

Comments

  • tonoslfx
    tonoslfx almost 2 years

    How do I split the URL and then get its value and store the value on each text input?

    URL:

    other.php?add_client_verify&f_name=test&l_name=testing&dob_day=03&dob_month=01&dob_year=2009&gender=0&house_no=&street_address=&city=&county=&postcode=&email=&telp=234&mobile=2342&newsletter=1&deposit=1
    

    PHP:

    $url = $_SERVER['QUERY_STRING'];
    $para = explode("&", $url); 
    foreach($para as $key => $value){   
        echo '<input type="text" value="" name="">';    
    } 
    

    above code will return:

    l_name=testing  
    dob_day=3  
    doby_month=01  
    ....
    

    and i tried another method:

    $url = $_SERVER['QUERY_STRING'];
    $para = explode("&", $url); 
    foreach($para as $key => $value){   
        $p = explode("&", $value);
        foreach($p as $key => $val) {
           echo '<input type="text" value="" name="">';
        }   
    } 
    
  • Manse
    Manse over 12 years
    Just for me ... why would you use parse_str() over $_GET ? i can understand the nerd to use it if the string you want to parse if not the url used to call the executing php ...
  • Mark Baker
    Mark Baker over 12 years
    How would $_GET handle ?arr[]=foo+bar&arr[]=baz
  • Manse
    Manse over 12 years
    $_GET['arr']; <- would be an array
  • Mark Baker
    Mark Baker over 12 years
    In that case, it's just me being nerdy