Barney Douglas Barney Douglas - 3 days ago 4
PHP Question

PHP filename parameter query

First of all I have checked the other suggested answers and I'm not certain whether they actually cover the question I've got. I'm very new to PHP so please forgive me if I am asking what sounds like a stupid question.

I have a php file which is called from another php file with a parameter

I understand how this works in the calling file.

I don't understand how to extract the parameter contents into a variable at the target end.

Let's say for a moment that in the address bar of the browser I get this:


  • targetfilename?parameter=Fred_hippy



I now want to pass "Fred" and "hippy" to a two-element array inside targetname.php. That's it, nothing else. (I said I was new to PHP.)

I think the way to do this is:


  • $file = substr($targetfilename, 13);

  • $name = explode("_", $file);



Is that correct please? If not could somebody tweak it please?

Thanks.

Answer

All parameters (everything after the ?) are returned as $_GET or $_POST array. If you are typing into the address bar (as opposed to using a FORM) then it is always GET. PHP makes it really easy:

$parameter = $_GET['parameter'];
$name = explode("_",$parameter);

That leaves $name[0] = 'Fred' and $name[1] = 'hippy'. In older versions of PHP, the $_GET to variable assignment was done automagically, which was very useful but also opened a lot of possible security issues, so that has been deprecated.