php_begins Posted February 7, 2012 Share Posted February 7, 2012 I thought I had a decent bit of knowledge on PHP/MYSQL, but then I was asked these questions in an interview and I didn't know how to answer them. Googling them didnt give me a satisfactory question. What is one reason someone might choose to use $_SERVER['QUERY_STRING'] instead of $_REQUEST to retrieve URL parameters passed to a page? Let's say you have a page that is making a service call, parsing the response and producing HTML. Your manager is complaining that the page takes 3 seconds to come back. How might you go about troubleshooting this? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted February 8, 2012 Share Posted February 8, 2012 What is one reason someone might choose to use $_SERVER['QUERY_STRING'] instead of $_REQUEST to retrieve URL parameters passed to a page? If you were using the Model View Controller Pattern that uses the url parameters to autoload the required objects then you may want to use it in what is known as a dispatcher (analyses the url and takes the appropriate action). Lets say you had a url that looks like this: http://www.yourwebsite.com/blog/latest/a By taking apart the query string (/blog/latest/a) with $_SERVER['QUERY_STRING'] the displatcher knows to load the 'blog' object, use the 'latest' method and pass in the parameter 'a'. This hypothetical page would show all the latest blogs that start with the letter a. It is a bit of a daft question as there are other methods of obtaining the URL parameters using reserved variables in $_SERVER superglobal or $_REQUEST array. Let's say you have a page that is making a service call, parsing the response and producing HTML. Your manager is complaining that the page takes 3 seconds to come back. How might you go about troubleshooting this? This is not necessarily a problem. The web server that you are polling with an API call may be slow. There may be too many records that you are trying to parse, etc. Some solutions may be to split up the number of records, cache the results, obtain the data using an automated procedure and create a local database that is queried instead of a remote call, etc Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted February 8, 2012 Share Posted February 8, 2012 There are more examples of $_SERVER['QUERY_STRING'] here http://php.net/manual/en/reserved.variables.php Quote Link to comment Share on other sites More sharing options...
xylex Posted February 10, 2012 Share Posted February 10, 2012 Lets say you had a url that looks like this: http://www.yourwebsite.com/blog/latest/a By taking apart the query string (/blog/latest/a) with $_SERVER['QUERY_STRING'] the displatcher knows to load the 'blog' object, use the 'latest' method and pass in the parameter 'a'. This hypothetical page would show all the latest blogs that start with the letter a. results, obtain the data using an automated procedure and create a local database that is queried instead of a remote call, etc Little off there- $_SERVER['QUERY_STRING'] only returns the part of the URI after the "?" (e.g. http://www.phpfreaks.com/forums/index.php?topic=353390 query string is "topic=353390") It differs from $_REQUEST in that $_REQUEST returns cookie and post variables as well as the query string ($_GET) variables, and you might use $_SERVER['QUERY_STRING'] instead of $_GET to parse a request that has multiple parameters of the same name (horrible, but possible practice in REST services). And the the second question IMO was just to see what your background is of doing this. Personally, I like to use a similar question when I'm interviewing as people tend to jump to what they know best - network issues, PHP performance, server issues, DB performance, client rendering, etc - which is usually a way more honest answer than if you ask them what part of the stack they feel the most comfortable. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted February 10, 2012 Share Posted February 10, 2012 Little off there- $_SERVER['QUERY_STRING'] only returns the part of the URI after the "?" (e.g. http://www.phpfreaks.com/forums/index.php?topic=353390 query string is "topic=353390") It differs from $_REQUEST in that $_REQUEST returns cookie and post variables as well as the query string ($_GET) variables, and you might use $_SERVER['QUERY_STRING'] Thats all you require using MVC as all requests are made to index.php and handled by the dispatcher. You only require the query string to load the appropriate objects. $_REQUEST contains all reserved variables as you have stated. you might use $_SERVER['QUERY_STRING'] instead of $_GET to parse a request that has multiple parameters of the same name Yep, that is bad. Also you may struggle with $_GET when a parameter uses certain unicode characters, however if you have any sense you would avoid this. Quote Link to comment Share on other sites More sharing options...
trq Posted February 10, 2012 Share Posted February 10, 2012 Little off there- $_SERVER['QUERY_STRING'] only returns the part of the URI after the "?" (e.g. http://www.phpfreaks.com/forums/index.php?topic=353390 query string is "topic=353390") It differs from $_REQUEST in that $_REQUEST returns cookie and post variables as well as the query string ($_GET) variables, and you might use $_SERVER['QUERY_STRING'] Thats all you require using MVC as all requests are made to index.php and handled by the dispatcher. You only require the query string to load the appropriate objects. $_REQUEST contains all reserved variables as you have stated. I think there is still some confusion here, probably generated from this: Lets say you had a url that looks like this: http://www.yourwebsite.com/blog/latest/a By taking apart the query string (/blog/latest/a) with $_SERVER['QUERY_STRING'] the displatcher knows to load the 'blog' object, use the 'latest' method and pass in the parameter 'a'. This hypothetical page would show all the latest blogs that start with the letter a. Given the url http://www.yourwebsite.com/blog/latest/a, $_SERVER['QUERY_STRING'] will be empty, /blog/latest/a will show up in $_SERVER['REQUEST_URI'] which is (in simple terms) what most MVC's pass to the router for processing before handing the results to the dispatcher. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted February 10, 2012 Share Posted February 10, 2012 Thats correct $_SERVER['REQUEST_URI']. My bad Quote Link to comment Share on other sites More sharing options...
kicken Posted February 10, 2012 Share Posted February 10, 2012 There may be instances where you don't want/need to pass a name=value set in the query string, but instead just a value. In those cases you would need to use $_SERVER['QUERY_STRING'] to get it. For example: /error.php?404 or /error.php?500 switch ($_SERVER['QUERY_STRING']){ case '404': //404 error message case '500': //500 error message } Quote Link to comment Share on other sites More sharing options...
trq Posted February 10, 2012 Share Posted February 10, 2012 I'm pretty sure... if (isset($_GET['404'])) { works in that situation as well. Quote Link to comment Share on other sites More sharing options...
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.