Jump to content

When using $_SERVER['REQUEST_URI']...


9three

Recommended Posts

Hello,

 

Now that I'm using htaccess to access my pages and using $_SERVER['REQUEST_URI'] to strip the URI and grab the pages it's requesting, I am no longer able to use $_GET variables.

 

I used to use GET variables as a way to show dynamic information.

 

Example:

$ref = $_GET['ref'];

 

if ($ref == 'home')

//Show Home information

 

Now because I'm using the entire URI when I do something like domain.com/?ref=home, "?ref=home" is treated as a string. Which is expected because I'm exploding everything within each forward slash.

 

My question is, how can I continue to use $_GET variables or something close to what I used before? I've noticed some sites, such as, last.fm which have in their url last.fm/?artist=some_variable_here

 

That's the same effect I'm trying to accomplish.

 

thanks

Link to comment
Share on other sites

I can't do that because it will take the whole thing as a string.

 

IE. domain.com/home?ref=variable

 

The whole string would be "home?ref=variable" and it will return a 404 (In my case, I created a check, if a file is not found it throws a 404 error).

 

Basically what I'm looking for is still the same functionality that GET globals bring.

Link to comment
Share on other sites

Hm, seems that works out ok for every other page that is not the index.

 

For example,

 

If the URI is www.domain.com/

 

The code knows that the user is on the main page, so it will output the index.php page for them.

 

But what if I wanted to add a link AND keep them on the main page without having to do a www.domain.com/index/?ref=variable ?

 

I wouldn't be able to do www.domain.com/?ref=variable because it would take it in as a whole string. I would need to do www.domain.com/index/?ref=variable in order for it to work correctly.

 

But now that I think about it, I may have to do a work around, if I want to implement this.

 

Maybe doing something like:

 

if "?" was found

use parse_str and use the index controller

else

explode the URI and continue as normal

 

What do you think?

Link to comment
Share on other sites

Here's an example of how you could use this function:

 

<?php

// grab url
$string = 'domain.com/?ref=variable&ref2=variable2';

// just get the part of the string that includes the variables ("?" to the end of the string)
$string = substr($string, strrpos($string, "?") +1);

// parse
parse_str($string, $output);

// print 
print_r($output);

?>

 

It will output

 

Array ( [ref] => variable [ref2] => variable2 )

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...