Reputation: 5
I am trying to detect whether a url contains a string or a string with querystring, then give it a different function.
The code below is all I can think of and it's doesn't work.
What is the best way to check this?
url 1 = http://example.com/index.php
url 2 = http://example.com/index.php?some-test-page
if (stripos($_SERVER['REQUEST_URI'], 'index')){
$xurl="http://example2.com/photos/";
} else {
$req_url = $_SERVER['QUERY_STRING'];
$xurl=('http://example2.com/photos/' . $req_url . '');
}
Upvotes: 0
Views: 103
Reputation: 2488
Split URL into pieces with parse_url, then examine closer what you need... http://php.net/manual/de/function.parse-url.php
Upvotes: 0
Reputation: 42770
If there's a query string, it will populate the $_GET
superglobal. Just check for that and add it back using http_build_query()
if needed (this will ensure any escaping is taken care of.)
$xurl = "http://example2.com/photos/";
if (!empty($_GET)) {
$xurl .= "?" . http_build_query($_GET);
}
Upvotes: 1
Reputation: 848
You should check for the '?' in requesturi rather than index.
url 1 = http://example.com/index.php
url 2 = http://example.com/index.php?some-test-page
if (stripos($_SERVER['REQUEST_URI'], '?') === false ){
$xurl="http://example2.com/photos/";
}
else{
$req_url = $_SERVER['QUERY_STRING'];
$xurl=('http://example2.com/photos/' . $req_url . '');
}
Upvotes: 0