Jump to content

questions on query string,service call


php_begins

Recommended Posts

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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.