What is the difference between 123 and 123?

I noticed that some sites (including the old http://careers.stackoverflow.com 1.0) have query strings that look like this:

http://somewebapp.example/?123

compared with:

http://somewebapp.example/123 or http://somewebapp.example/id/123

What are the reasons developers prefer to use the URLs of their web applications using the first example instead of the second and third examples?

And as a bonus, how to implement the first example in PHP, given that 123 is the primary key of some row in the database table? (I just need to know how to extract 123 from the URL, I already know how to query the database for 123 primary key.)

EDIT [5/28]:. Oh, I forgot that everyone knows that I know what the last two URLs are, how they work and how to implement them. Thanks for the reminders, although I think I had some unrelated misconceptions that were clarified anyway!

+6
url php query-string
source share
7 answers

The first is easier to implement; all after? is part of the query string. The web server loads the page indicated before ?, and processes the query string separately (in PHP it is available through $_GET )

In the second example, the developer needs to configure the web server to redirect all requests to a special page (since there is no /123 page on the server), which will then analyze the URL to find out what was requested

As for your last question, 123 will appear in $_GET as the key, so key($_GET) will work if you assume that this is the only thing you pass in the query string

+9
source share

You can access it from php using

 $_SERVER['QUERY_STRING'] 
+1
source share

The first URL uses request parameters to send data. This is later a REST URL form that actually points to a resource with ID 123

+1
source share

The first example is a query string; the second and third examples are not. For more information on URLs, see What Every Developer Should Know About URLs . To use the identifier from the query string, you need to parse it using the appropriate library.

0
source share

What are the reasons developers prefer to use the URLs of their web applications using the first example instead of the second and third examples?

Aesthetics. Read the cool URIs for some helpful tips on how to create URIs.

how to implement the first example in PHP

You are looking for $_SERVER['PATH_INFO'] , but be careful: some hosting providers post dummy things there.

In these situations, you will need to use mod_rewrite or the like.

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(\d+) /index.php?id=$1 [L,QSA] 

Thus, you can get the identifier from $_GET['id']

0
source share

This is actually related to modRewrite rules or apache configurations. Some structures, such as Code Ignitor, use these routes to direct you to a page that does not actually technically exist in this physical location. It is analyzed and the structure determines which view is suitable for display. Some rules allow the web server to pass anything after the / parameter as a parameter to determine if a valid route is accessible from the framework. Some systems do not do this automatically, so a question mark is marked to make it pass to the infrastructure as a route

If you want to get 123 from the first instance, you need to pull out the query_string query

0
source share

If you use Apache (and you can do the same on other servers, but I don’t know the details), something in this direction will do an interesting trick:

 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ index.php/$1 </IfModule> 

Rule: if the requested page is not a !-f file or a !-d directory, then instead of giving a page not found error, load index.php and pass the requested path to this script in the form from $_SERVER['PATH_INFO'] ( it can also be retrieved from $_SERVER['REQUEST_URI'] .

Or you could tell Apache to rewrite it on index.php?$1 , in which case the path can be extracted from $_SERVER['QUERY_STRING'] . (In this case, you should use the QSA argument to add the current query string (if any), without any problems.

0
source share

All Articles