Page 1 of 1

Possible to put if else on request_uri

Posted: Tue May 01, 2012 1:32 pm
by sfumato1002
Hello, I have this:

<?php
$request_uri = $_SERVER['REQUEST_URI'];

// find position of '?' in a string
$position = strpos($request_uri, 0, '?');

// take out part of string
$path = substr($request_uri, 0, $position);

// form url
$url = 'http://www.mysite.com'.$path.'?';
?>

This is my url:
<?php echo $url?>jr_listings=bed-and-breakfasts

The problem probably is that strpos() return FALSE when it doesn't find '?' in $request_uri. and it returns empty string.

I was wondering if there it was possible to put and if and }else{ so that if there is no '?' in the request_uri, then I still get the request_uri string.

I have been working on this all day and just cannot get it to work :banghead:

Re: Possible to put if else on request_uri

Posted: Tue May 01, 2012 2:06 pm
by Celauran
What is it you're trying to accomplish here? Why is $_GET not adequate?

Re: Possible to put if else on request_uri

Posted: Tue May 01, 2012 2:16 pm
by sfumato1002
Hello, I am creating a city guide with many city tags and categories, basically I am trying to do this:

site.com/local/tag/state/california

So if I click on:
<?php echo $url?>jr_listings=bed-and-breakfasts

I get this now:

site.com/local/tag/state/california?jr_listings=bed-and-breakfasts

here is my code with the else if:

<?php
$request_uri = $_SERVER['REQUEST_URI'];

if($position = strpos($request_uri, '?') !== FALSE) {
$path = substr($request_uri, 0, $position);
} else {
$path = $request_uri;
}

$url = 'http://www.mysite.com'.$path.'?';
?>
.....
But now I get empty string after clicking on category.... I just cant get this to work. How is the $_GET different?

Re: Possible to put if else on request_uri

Posted: Wed May 02, 2012 8:38 am
by x_mutatis_mutandis_x
sfumato1002 wrote:Hello, I am creating a city guide with many city tags and categories, basically I am trying to do this:

site.com/local/tag/state/california

So if I click on:
<?php echo $url?>jr_listings=bed-and-breakfasts

I get this now:

site.com/local/tag/state/california?jr_listings=bed-and-breakfasts

here is my code with the else if:

<?php
$request_uri = $_SERVER['REQUEST_URI'];

if($position = strpos($request_uri, '?') !== FALSE) {
$path = substr($request_uri, 0, $position);
} else {
$path = $request_uri;
}

$url = 'http://www.mysite.com'.$path.'?';
?>
.....
But now I get empty string after clicking on category.... I just cant get this to work. How is the $_GET different?

Code: Select all

$url = 'http://www.mysite.com' . $_SERVER['REQUEST_URI'];
$path = parse_url($url, PHP_URL_PATH);