tags:

views:

74

answers:

7

hi just wondering. i've seen sites with this kind of url http://www.something.com/?somedata with the 'somedata' is the value of some 'unmentioned' variable

how can i do something like that? all I know is the traditional http://www.something.com/index.php?arg=somedata

thanks a lot

+1  A: 

You can usually access the full query string using $_SERVER["QUERY_STRING"].

I think every main-stream web server provides that variable, I'm 100% sure about Apache and pretty sure about IIS.

Pekka
PHP allows direct access to GET variables via the `$_GET` associative array. There's no need to parse the query string.
Álvaro G. Vicario
@Álvaro G. Vicario, Pekka never metioned parsing anything: this answer is referring to getting the full query string.
salathe
A: 

They probably use some kind of URL rewriting (see Apache mod_rewrite) where the url is transformed from http://www.something.com/?somedata to http://www.something.com/index.php?arg=somedata

Snake
Not in this case - this is a perfectly normal query string, no rewriting needed.
Pekka
URL rewriting for clean URLs would produce something like "http://www.something.com/index/somedata"
Techpriester
+4  A: 

This is just a variable without value. You can get that string using list($value) = array_keys($_GET); - assuming you have ensured there is exactly one value in the $_GET array (count($_GET) === 1), otherwise you will get an error (if $count === 0) or unwanted behavior (if $count > 1).

soulmerge
Nothing prevents you from just reading `$_GET['somedata']`
Álvaro G. Vicario
No, it doesn't, *if you know the exact string you're expecting*. I thought of a scenario where you don't know the key, as in `search.php?mysearchstring`. Besides: the question's title is 'variable-less argument'
soulmerge
This is the best solution IMO. Why does it get downvoted? Any explanation?
Pekka
thanks a lot for this answer :D
imin
@Pekka: I believe that the question is not clear enough and every understands something different from it. IMHO, the "unmentioned" variable was only an hypothesis of the OP, who was astonished for the lack of `=` sign and didn't even know you can normally omit "index.php" from URLs.
Álvaro G. Vicario
A: 

For your given URL you can just iterate over the $_GET array:

foreach ($_GET as $key => $value) {
    if ($key == 'somedata') {
        //do something
    }
}

You'll find the parameter in the keys of $_GET.

You also could extract the keys from $_GET with array_keys($_GET).

Techpriester
A: 

Most web servers, since the beginning of the web, allow to define a default file name to be delivered for directories. So if you load http://example.com/ it serves such file (typically index.html). In PHP enabled systems, you normally use index.php for such purposes, although the exact name can be changed. In Apache:

http://httpd.apache.org/docs/2.2/en/mod/mod_dir.html#directoryindex

As about ?somedata, it's just a variable without a value (or, more exactly, its value is an empty string). You can use it if you only need to know whether the variable is set or not:

<?php

if( isset($_GET['somedata']) ){
    // Do some stuff
}

?>
Álvaro G. Vicario
+1  A: 

It is simply a shortcut for a boolean value.

somedata is equivalent to somedata=1 or somedata=true

When it's checked server side, the presence itself of the variable is enough to write a condition.

if ( isset($_GET['somedata']) ) { 
    //do something
}
Boris Guéry
+1  A: 

What you see as "the value of some 'unmentioned' variable" is more generally considered as a query string parameter without a value. So for foo=bar&baz, foo has the value bar and baz has no value (in PHP its value will be an empty string).

Since the other answers are providing different methods of accessing that parameter name, here are my two cents. You can get the first key of the $_GET array by using the key function. If no key is available, key will return NULL.

Visiting ?somedata=somevalue

var_dump(key($_GET), $_GET);
/*
string(8) "somedata"
array(1) {
  ["somedata"]=>
  string(9) "somevalue"
}
*/

Visiting ?somedata

var_dump(key($_GET), $_GET);
/*
string(8) "somedata"
array(1) {
  ["somedata"]=>
  string(0) ""
}
*/

Visiting ?

var_dump(key($_GET), $_GET);
/*
NULL
array(0) {
}
*/
salathe