9three Posted April 13, 2010 Report Posted April 13, 2010 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 Quote
falkencreative Posted April 13, 2010 Report Posted April 13, 2010 Rather than using $_GET, could you do something like this: say your URL looks like this: http://www.website.com/home And you just grab all the ending characters off the end of the URL until you hit your first forward slash? Quote
9three Posted April 13, 2010 Author Report Posted April 13, 2010 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. Quote
falkencreative Posted April 13, 2010 Report Posted April 13, 2010 After you take the entire URL as a string, could you use http://www.php.net/manual/en/function.parse-str.php ? Looks like that will do what you want. Quote
9three Posted April 13, 2010 Author Report Posted April 13, 2010 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? Quote
falkencreative Posted April 13, 2010 Report Posted April 13, 2010 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 ) Quote
9three Posted April 13, 2010 Author Report Posted April 13, 2010 Yep, seems pretty clear to me this is the route I need to take. Thanks. Quote
Recommended Posts
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.