Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to get the values out of a URL query string like this:

http://exmaple.com/?xyz.123

And assign them to variables inside index.php running on example.com, so that:

$name = xyz;
$number = 123;

How do I code this?

Thanks!!

share|improve this question
Could just get the query value from $_SERVER["QUERY_STRING"] and then explode the value with the period which is seperating both values in your example – John Jul 14 '11 at 21:45
Do you mean ?xyz=123 ? This will form a key/value pair that you can access using $_GET. – George Cummins Jul 14 '11 at 21:48

4 Answers

up vote 4 down vote accepted
list($name,$number) = explode('.',$_SERVER['QUERY_STRING']);
share|improve this answer

You'd need to setup a mod_rewrite rule first like this (untested)...

RewriteRule ^([a-zA-Z]+)\.([0-9]+)$ index.php?name=$1&number=$2

Then you could pull them out from $_GET in PHP.

share|improve this answer

What you want to do is take a look at $_SERVER['QUERY_STRING']. Explode the query string by . to get an array of values. You can then set up the appropriate variables. Keep in mind you'll also probably want to do some validation on the data to ensure it's in the format you need.

share|improve this answer
Thanks for the tip. :) – Dustin Jul 14 '11 at 23:20

You can parse it with the following, though it is ripe for injection unless you perform some validation/sanitization afterwards:

list($name, $number) = explode('.', $_SERVER['QUERY_STRING']);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.