Possible to put if else on request_uri

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sfumato1002
Forum Newbie
Posts: 8
Joined: Wed May 28, 2008 3:58 pm

Possible to put if else on request_uri

Post 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:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Possible to put if else on request_uri

Post by Celauran »

What is it you're trying to accomplish here? Why is $_GET not adequate?
sfumato1002
Forum Newbie
Posts: 8
Joined: Wed May 28, 2008 3:58 pm

Re: Possible to put if else on request_uri

Post 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?
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Possible to put if else on request_uri

Post 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);
Post Reply