Forum Moderators: coopster
myscript.php
<?php
if (isset($_GET) && $_GET) {
print '<pre>';
print_r($_GET);
print '</pre>';
}
exit;
?>
Array
(
[bar] =>
)
if ($_SERVER['QUERY_STRING']) {
$playlist = $_SERVER['QUERY_STRING'] ;
$playlist = "/demoreel?playlist=$playlist" ;
header("Location:$_SERVER[HTTP_REFERER]$playlist") ;
exit ;
};
and it works of course, however I'd like to learn what the test that was suggested by Coopster tells me.
Array
(
[bar] =>
)
// using var_dump instead:
// var_dump($_GET);
array(1) {
["bar"]=>
string(0) ""
}
http://example.com/myscript.php?bar=foo&baz
We know that QUERY_STRINGs are passed in name=value pairs. Well, in the original example, '?bar' is merely the name, no value associated with it. Now we have added a value and appended a new 'name' but again, no value. print_r and var_dump will show us this:
Array
(
[bar] => foo
[baz] =>
)
array(2) {
["bar"]=>
string(3) "foo"
["baz"]=>
string(0) ""
}
[php.net...]
array array_keys ( array input [, mixed search_value [, bool strict]] )
try
if ($_GET) {
$playlist = array_keys($_GET) ;
echo '<pre>';
print_r($playlist);
echo ''</pre>;
}
function fix_get() {
foreach($_GET as $key => $value) {
if (empty($value))
$_GET[$key] = true;
}
}
It goes through every GET key and if there's no value for the key, it sets to true.
That way I can do things like
if ($_GET['somekey']) {
//do something
}
Although without the function, it's just a matter of doing
if (isset($_GET['somekey'])) {
//do something
}