3

I am trying to develop a PHP class which would enable me to get the query string appended into a url and process it according to the variables passed. How can this be done?

Eg

www.example.com?var1=a&var2=b&var3=c

now I want to get ?var1=a&var2=b&var3=c section and process it based on the variables.

Thanks for your responses

but I want a means by which I can get the whole query string since I wont be sure of the variable names that will be sent into the URL thus the $_GET method wont work properly.

Thanks

asked Jun 3, 2010 at 10:50
2
  • 1
    Are you talking about URLs with which you access your site or URLs contained in a string (from e.g. a database)? Commented Jun 3, 2010 at 10:58
  • I am talking about the URLs used to access your site. Commented Jun 3, 2010 at 11:10

6 Answers 6

5
// Current URL: www.example.com?var1=a&var2=b&var3=c
function handleUrl()
{
 $var1 = $_GET['var1']; // now contains 'a'
 $var2 = $_GET['var2']; // now contains 'b'
 $var3 = $_GET['var3']; // now contains 'c'
 return "Querystring now contains $var1 $var2 and $var3";
}

More information at http://php.net/manual/en/reserved.variables.get.php

answered Jun 3, 2010 at 10:52

1 Comment

but dont forget to htmlspecialchars() the $_GET, because otherwise people can use xss to "hack" your site or to use it for bad things :)
4

If you want access to the URL parameters as a string you can use this:

$_SERVER['QUERY_STRING']

But, I'm not sure how that would be any more useful than $_GET. You said:

I wont be sure of the variable names

You can iterate through the $_GET array with a foreach loop:

foreach ($_GET as $key => $val) {
 echo $key . ": " . $val;
}

However... if you're talking about an arbitrary URL (not necessarily the one which was requested), then the above methods won't work since they obviously won't be in $_GET. Instead you can use parse_url

$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_QUERY);

Output:

Array
(
 [scheme] => http
 [host] => hostname
 [user] => username
 [pass] => password
 [path] => /path
 [query] => arg=value
 [fragment] => anchor
)
arg=value
answered Jun 3, 2010 at 11:21

1 Comment

+1 for the awesome varieties presented. This should do everything the OP requested and more. I had forgotten about a couple of the options mentioned.
1

Use $_GET

answered Jun 3, 2010 at 10:52

Comments

0

If you have the parameters stored in a variable, then use parse_str. Example:

$str = 'var1=a&var2=b&var3=c';
parse_str($str);
echo $var1;
answered Jun 3, 2010 at 10:56

Comments

0

http_build_query is the function you're probably looking for

answered Jun 3, 2010 at 11:06

Comments

0

Here is a minimalistic class. It should work, but it is not tested.

<?php
class URLHandler 
{
 public $get;
 public function __construct( $get );
 {
 $this->get = $get;
 }
 public function get_var( $var )
 {
 return htmlspecialchars( $this->get[$var] );
 }
 public function check_var( $var, $is )
 {
 if( $this->get_var( $var ) == $is )
 {
 return true;
 }
 else
 {
 return false;
 }
 }
}
// Below is how you can use this class
$URL = new URLHandler( $_GET );
if( $URL->check_var( 'var1', 'a' ) )
{
 echo 'yes your first var is a';
 // Here you can add a function or functions which should be loaded if var1 = a
}
else
{
 echo 'No, your first var is not a, its '.$URL->get_var( 'var2' );
 // Here you can add a function or functions which should be loaded if var1 is not a
}
if( $URL->check_var( 'var2', 'a' ) )
{
 echo 'and your second var is a';
}
else
{
 echo 'your second var is not a, its '.$URL->get_var( 'var2' );
}
?>
halfer
20.1k19 gold badges110 silver badges207 bronze badges
answered Jun 3, 2010 at 11:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.