Welcome to WebmasterWorld Guest from 54.198.69.193

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

www.mysite.com?bar

how to read variables from this kind of link

     
5:51 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



I know how to $_GET variable from
www.mysite.com?foo=bar

but how can I get it from
www.mysite.com?bar

I know it is possible, because I know a script which uses this method, but I am not able to reverse engineer it.

5:53 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



How about $_SERVER['QUERY_STRING']?

JK

5:57 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



An easy way to figure stuff like that out is to dump the variable to the browser. Create yourself a little script that shows you how it works:

myscript.php

<?php 
if (isset($_GET) && $_GET) {
print '<pre>';
print_r($_GET);
print '</pre>';
}
exit;
?>

Now open your browser and type this into the address bar:
http://example.com/myscript.php?bar
and you will see that it will print out:
Array 
(
[bar] =>
)
8:49 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



Thanks, I came up with:

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] =>
)
8:53 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It's telling you that with PHP you are getting an index in your $_GET superglobal with an empty string value. Use var_dump instead of print_r and you will see that it is an empty string value being returned in your 'bar' index:
// using var_dump instead: 
// var_dump($_GET);
array(1) {
["bar"]=>
string(0) ""
}

In contrast, to really grasp what I was trying to explain, look at what happens when we append to the query string. Let's say the url you use looked like this

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) ""
}
11:08 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



Coopster - would there be some simple way of setting the value of bar as 0 and then using array_flip to turn it around and then extract the value "bar" from the index 0?

I may be way off track...

1:32 am on Mar 22, 2006 (gmt 0)

5+ Year Member



hm... and why does this output "Array" instead of Key name?


if ($_GET) {
$playlist = array_keys($_GET) ;
echo $playlist ;
}
1:35 am on Mar 22, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



because array_keys returns an array

[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>;
}

6:22 am on Mar 22, 2006 (gmt 0)

5+ Year Member



I have a function that modifies the get.

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
}
 

Featured Threads

Hot Threads This Week

Hot Threads This Month