简体   繁体   中英

what do I call the trailing value after last slash in a url and how to get it in php

I'm new to PHP and web, and I was wondering, in some URLs what do we call the last string value after the last slash. for example "abc123" in this url

URL 1

 http://www.example.com/folder1/abc123


I know about the URL parameters that is exist after a ? question mark and how to get it inside my PHP scripts, like

URL 2

$name = $_GET['name'];
$age= $_GET['age'];

when the browser requests this resource from the web server, the web server (Apache for example) are going to invoke the php module which will try to parse phpscript1.php giving him the data he needs to work. in this example I can see the name and age parameters inside my phpscript1.php file like this

 $name = $_GET['name']; $age= $_GET['age']; 


Q1 - In URL1 what do I call "abc123" ? parameters ?

Q2 In URL1 how the web server is going to deal with this request? will it try to find a resource that is called "/folder1/abc123" and just send it blindly to the browser ? for example if I have a file on the server that has the name "abc123" inside the folder "/folder1/" , is it going to be sent to the browser ?

Q3 in URL1 how do I get the value "abc123" inside my PHP scripts? or even which is more important, which PHP script are going to be invoked by the PHP module to handle this request?

Q1- In URL1 what do I call "abc123" ? parameters ?

This is simply part of the path.

Q2 In URL1 how the web server is going to deal with this request? will it try to find a resource that is called "/folder1/abc123" and just send it blindly to the browser ? for example if I have a file on the server that has the name "abc123" inside the folder "/folder1/", is it going to be sent to the browser ?

It depends on how your server is configured. Apache will look for the default document, which is defined in the httpd.conf file. It will likely look first for a file called index.html or index.php, but you can set it to look for other things, too. It will likely assume that the part after the last / is the filename, unless it's a folder name.

Q3 in URL1 how do I get the value "abc123" inside my PHP scripts? or even which is more important, which PHP script are going to be invoked by the PHP module to handle this request?

You should look up the $_SERVER superglobal. It will likely have what you're looking for:

http://php.net/reserved.variables.server

Q1) Default that URL is pointing at the folder /folder1 and a file without extension abc123 .

If you have Mod_rewrite enabled, you can change the url structure via .htaccess so that GET parameters are shown like that. See this post Reference: mod_rewrite, URL rewriting and "pretty links" explained

Q2) Default it will try to open that file, abc123, if it does not exist - it will must likely return 404 error.

Q3) I think this is answered in Q1 and Q2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM