0

Normally when I'm making GET requests I do something like this, for example:

Link: http://example.com?color=blue

Code:

if(isset($_GET['color'])) {
    echo $_GET['color'];
    //Outputs "blue"
}

What I'd like is for the link to not have to include ?color=blue, just the ending "blue" without having to make a new folder or file named blue.html or so. Like Pastebin for example, ending with an identifying string https://pastebin.com/blue. So for example:

Link: http://example.com/blue

With that link I'd like to be able to get those 4 last letters so that I can initiate a database search for "blue", or any string. PHP, js (with/without jQuery) are the languages I'm comfortable using.

I do realise this is a newb question, but I do not know where to start.

j08691
  • 204,283
  • 31
  • 260
  • 272
Algernop K.
  • 477
  • 2
  • 19
  • 2
    You'll need an htaccess rewrite for this. – reformed Jun 28 '17 at 15:05
  • this can be done with .htaccess as @reformed mentioned, it also depends on how you set your page up e.g. in an mvc environment, the majority of /example/blue would actually be pulled in by $_POST – treyBake Jun 28 '17 at 15:05
  • Didn't know one could make $_POST requests from just the url, though that would be fine too. – Algernop K. Jun 28 '17 at 15:06
  • @ThisGuyHasTwoThumbs *"in an mvc environment, the majority of /example/blue would actually be pulled in by $_POST"* - you sure? Most PHP MVC frameworks I've encountered just have a *not file (`-f`) not directory (`-d`)* router in the base `.htaccess` that just punts everything to `/index.php`... which then uses `$_SERVER['REQUEST_URI']` to determine the controller/action. – CD001 Jun 28 '17 at 15:26
  • @CD001 very - because of how it works, you go to a controller, anything passed as a param, lets say `id (public function dynamic($id = 0)` the `id` is the last part of the url e.g. /controller/action/parameter1 - which is post because of how you call the function - however this all being said - it is 100% up to how the MVC was originally developed – treyBake Jun 28 '17 at 15:37
  • @ThisGuyHasTwoThumbs ... sorry, I'm not convinced, it's POST if you specify it as POST in the HTTP header (and set the parameters in the request body) - the sort of thing you'd do with Ajax or cURL. If the parameters are going in the address bar, no matter the format, it's *probably* GET - just did a couple of `var_dump($_GET)` checks with Phalcon and Magento (Zend Framework)... I've dropped Yii and Symfony on this dev box but I'd be interested to see what they do. – CD001 Jun 28 '17 at 15:50
  • @CD001 very familiar with Magento - used to be my job :D haha not heard of Phalcon :S and only if the .htaccess is set up as such for $_GET otherwise it'll return null / error – treyBake Jun 28 '17 at 15:55
  • @ThisGuyHasTwoThumbs - "used to be" ... aah, you escaped then ;) Yeah, Magento maps the REQUEST_URI to the controller/action (both `$_GET` and `$_POST` are blank) - Phalcon is a framework (written in C and compiled as a PHP extension so it's fast) that by default maps the REQUEST_URI to `$_GET['_url']` and uses that. – CD001 Jun 28 '17 at 16:02
  • @CD001 ahaha that I did ;) though to be honest, I was really excited when Magento 2 was announced and then made redundant was like oh, finally found a fun thing and I'm gone xD hahaha and interesting and good to know - maybe it's just my luck that I've used $_POST based controllers (if that's good or bad - I'm not to say) - but I can remove my comment if you would think it would be more beneficial? :) – treyBake Jun 28 '17 at 16:06
  • @ThisGuyHasTwoThumbs - nah, I was just curious since the frameworks I've used either use the REQUEST_URI or GET - you might well be right about others I've not used a huge number of frameworks beyond the sort of evaluation stage; once I'd discovered Phalcon I kinda stuck with it just because it's got such a small overhead compared to the others (especially Zend). – CD001 Jun 28 '17 at 16:13
  • @CD001 ah yes Zend is heavy haha I might look into Phalcon, if I like it - I thank you in advance ;) – treyBake Jun 28 '17 at 16:14
  • At least my duplicate sparked a little conversation. Thanks you guys :-) – Algernop K. Jun 28 '17 at 18:02

0 Answers0