PDA

View Full Version : [PHP] URL variables ?



chunk
March 6th, 2006, 10:51 AM
Hi,

This is mostly a post out of curiosity…

I know when the URL of a website is something like: www.somesite.com/file.php?a=hello (http://www.somesite.com/file.php?a=hello)

The variable a is set to “hello”

Example code:


if (isset($_GET['a'])) {
$a = $_GET['a']; }


So now $a would now be, "hello".



But I have seem some sites that use www.somesite.com/file.php?home (http://www.somesite.com/file.php?home) or /file.php?contact

What exactly is this? Is it setting a blank variable “home” / “contact” or is it telling the file.php to execute the function “home” / “contact”.

Just wondering.

Thanks!

mickshake
March 6th, 2006, 11:22 AM
Haven't tested it but maybe php reads that var as something more than null, so even though contact equals nothing, it's still declared in the $_GET vars

antizip
March 6th, 2006, 03:45 PM
well anything in the URL can be caught as a string and parsed for whatever info you'd like ... for example


$s = split('?','www.somesite.com/file.php?home');
echo $s[1]


would spit out 'home'

$_SERVER['SCRIPT_URI'] (i think) returns the url for whatever page you're on

SlowRoasted
March 6th, 2006, 03:47 PM
cool, didn't know that one.

chunk
March 7th, 2006, 07:18 AM
So...


$s = split('?','www.somesite.com/file.php?home');
echo $s[1];
Would display, home?

So...


$s = split('?','www.somesite.com/something.php?worlf?hello');
echo $s[1];
echo $s[2];
Would display:

World
Hello

?

When I try:


$s = split("?",'$_SERVER["PHP_SELF"]');
echo $s[1];

I get this error:

Warning: split(): REG_BADRPT in public_html/page.php on line 3

Line 3 is:


$s = split("?",'$_SERVER["PHP_SELF"]');

antizip
March 7th, 2006, 09:39 AM
ok did some diggin around ... first of all you gotta escape the ? and 2nd there is no "SCRIPT_URI" ... and "PHP_SELF" only returns the application not the query string ... but there is a "QUERY_STRING"

So ... all you would really need to do is

echo $_SERVER['QUERY_STRING'];
and it outputs everything after the 1st ?

But split still works for the query string something like ...


$s=split('/?',$_SERVER['QUERY_STRING'])
echo $s[1]


when the url is something like: http://www.somesite.com/?home?page?system

should output: [b]page

ironikart
March 7th, 2006, 05:15 PM
Don't construct your links like this:

http://www.somesite.com/?home?page?system

Very bad practise!

If you want to do something like this then I'd suggest:



<?php
$url = 'http://www.somesite.com/file.php?home,page,system';
$queryString = explode( ',', $_SERVER['QUERY_STRING'] );

// array ( '0' => 'home', '1' => 'page', '2' => 'system' );
print_r( $queryString );
?>


If you're running on apache, look into mod_rewrite. You can construct nice friendly urls by parsing the REQUEST_URI rather than just the query string.