• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

parse get parameters?

I need to parse with php the arguments of a GET request.
For example:
 http://mycode/code.php?par1=1&par2=a&par3=3

I need to parse for the values before and after each "="

How can I do this?
0
pillmill
Asked:
pillmill
  • 2
1 Solution
 
Rik-LeggerCommented:
print_r(array_keys($_GET));

Open in new window

0
 
pillmillAuthor Commented:
Thanks. The data is a string from the server log, not a $_GET array.
0
 
Rik-LeggerCommented:
Ok,

than you could use this php function (http://php.net/manual/en/function.parse-url.php).
And then you can use explode further on the query to get your parameters.
0
 
Ray PaseurCommented:
I have a teaching example that shows how to do this, and also something to watch out for in the way these functions work.  Install this and run it to see what is going on.  Hopefully you can use this with your logs.  HTH, ~Ray
<?php // RAY_parse_str_parse_url.php
error_reporting(E_ALL);
echo "<pre>";


// DEMONSTRATE HOW TO LOOK AT A URL AND PULL OUT ONE OF THE 'GET' ARGUMENTS
// MAN PAGE http://php.net/manual/en/function.parse-url.php
// MAN PAGE http://php.net/manual/en/function.parse-str.php


// TEST DATA - WE WANT TO FIND THE GET["v"] ARGUMENT
$url = 'http://www.youtube.com/watch?v=_PP5JuqXPqA&url=MUNGDATA';



// DOING IT WRONG: INJECT A VARIABLE WITH THE GET ARGUMENT FOR "v="
parse_str(parse_url($url, PHP_URL_QUERY));
echo PHP_EOL . $v;

// GAACK! LOOK AT THE OTHER INJECTED VARIABLE. WE DESTROYED $url
echo PHP_EOL . $url;



// RESET - SHOW HOW TO DO IT WITHOUT INJECTING UNKNOWN VARIABLES INTO THE SCOPE
$url = 'http://www.youtube.com/watch?v=_PP5JuqXPqA&num=42&spirit=willing&flesh=weak&url=MUNGDATA';

// CAPTURE THE INJECTED VARIABLES IN AN ARRAY
parse_str(parse_url($url, PHP_URL_QUERY), $arr);
echo PHP_EOL . $arr["v"];

// THIS TIME, THERE IS NO DAMAGE DONE!
echo PHP_EOL . $url;

// BECAUSE WE TRAPPED ALL THE INJECTED VARS IN THE ARRAY
echo PHP_EOL;
print_r($arr);

Open in new window

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now