homepage Welcome to WebmasterWorld Guest from 54.198.224.121
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
www.mysite.com?bar
how to read variables from this kind of link
wizpl




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

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.

 

JollyK




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

How about $_SERVER['QUERY_STRING']?

JK

coopster




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

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] =>
)

wizpl




msg:1273883
 8:49 pm on Mar 21, 2006 (gmt 0)

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] =>
)

coopster




msg:1273884
 8:53 pm on Mar 21, 2006 (gmt 0)

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) ""
}

slade7




msg:1273885
 11:08 pm on Mar 21, 2006 (gmt 0)

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...

wizpl




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

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


if ($_GET) {
$playlist = array_keys($_GET) ;
echo $playlist ;
}

jatar_k




msg:1273887
 1:35 am on Mar 22, 2006 (gmt 0)

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

Mr_Fern




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

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
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved