views:

28

answers:

7

Hello everyone,

I am very curious on how to do this. I want a PHP script to look at the string after the URL link and echo the value.

For example, if I entered:

"http://mywebsite.com/script.php?=43892"

the script will echo the value 43892. I have seen this in most websites, and I think it will be a very useful to have in my application.

Thanks,

Kevin

A: 
echo filter_var($_SERVER['QUERY_STRING'], FILTER_SANITIZE_NUMBER_INT);

The sanitation is because in your example the query string is =43892, not 43892. The filter used "remove[s] all characters except digits, plus and minus sign".

Artefacto
See my comment on @Frxstrem solution
Colin Hebert
@Colin The question did not include other data in the query string, so I don't think it's relevant.
Artefacto
A: 

The way you usualy use query parameters is by assigning them like http://mywebsite.com/script.php?var1=123&var2=234

Then you will be able to access them by $_GET['var1'] and $_GET['var2'] in your PHP script

JochenJung
A: 

Don't you mean http://mywebsite.com/script.php?43892 ?

You can either use apache URL rewriting or try to extract all entries from $_GET and look a the one which looks like a number or simply doesn't have a value.

Colin Hebert
+2  A: 

You mean, something like

http://mywebsite.com/script.php?MyVariable=43892

? Variables provided at the end of the URL like that are available in the $_GET array. So if you visited the above URL and there was a line on the page that said

echo $_GET['MyVariable'];

then 43892 would be echoed.

Do be aware that you shouldn't trust user input like this - treat any user input as potentially malicious, and sanitise it.

Hammerite
Is it possible to have two variables in the link? Something link http://mywebsite.com/script.php?MyVariable=14?MyVariable2=21
Kevin
Frxstrem
Alright thanks! And thanks to everyone else who responded!
Kevin
A: 

Try manually parsing the URL like this

$geturl = $_SERVER['REQUEST_URI'];

$spliturl = explode("?",$geturl);

$get = explode("=",$spliturl[0]);

echo $get[1];

:)

Pete Herbert Penito
A: 

Before I really answer your question, I just have to say that most sites - at least that I have seen - actually use ?43892, with the =. This is also much easier than using it with = in my opinion.

So, now to the actual answer. You can simply extra the query string using $_SERVER['QUERY_STRING'].

An example:

User requests index.php?12345:

<?php
echo $_SERVER['QUERY_STRING'];
?>

Output:

12345

Note that you can also use something like

<?php
if(substr($_SERVER['QUERY_STRING'], 0, 1) == '=') {
  $query_string = substr($_SERVER['QUERY_STRING'], 1);
}else{
  $query_string = $_SERVER['QUERY_STRING'];
}
echo $query_string;

to support ?=12345 as well as 12345, with the same result. Note also that ?=12345 would not be available as $_GET[''] either.

Frxstrem
Colin Hebert
When talking about what most sites do, I'm quite sure it will be ?var=43892, so you don't have to parse the variables your own, if there are multiple
JochenJung
@JochenJung: yes, you're actually right, but I was talking about the way he used it (with it as the only parameter)
Frxstrem
@Colin: but you can't use `$_GET` either in that case, as you wouldn't no the name of the variable (i.e. `$_GET['43892']`) in advance, only its value (`''`)
Frxstrem
Yep, you only know that and the fact that it's a number.
Colin Hebert
A: 

I'de recommand parse-url for this. The documentation contains all you (I think) need.

Aif