0

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

http://www.example.com/folder1/phpscript1.php?name=mike&age=24

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?

Accountant م
  • 4,969
  • 2
  • 30
  • 49
  • 1
    Those are not parameters or such thing. The request simply names a `path` that is requested. How that is interpreted is up to _you_. Typically a php application relies on "rewriting" of incoming requests. _How_ that rewriting is implemented depends, as already said, on _you_. – arkascha Nov 14 '16 at 13:50
  • @arkascha I'm sorry what do you mean by "rewriting of incoming requests" ? – Accountant م Nov 14 '16 at 14:15
  • A simple google search for "rewrite request" would have answered that... – arkascha Nov 14 '16 at 17:43
  • @arkascha I wasn't know that it is a famous "term" that a search for it would give me information. I thought It is a part from your comment that only you know what does it mean, but thanks. now I know there is a known thing in web called "rewrite request" and I'm already in this moment reading every thing about it. thanks – Accountant م Nov 14 '16 at 19:40
  • 1
    Sorry if I sounded arrogant. My bad. Rewriting is a basic thing when it comes to web page design and operation. – arkascha Nov 14 '16 at 19:59

2 Answers2

1

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

raphael75
  • 2,051
  • 1
  • 21
  • 34
1

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.

Community
  • 1
  • 1
Bolli
  • 4,714
  • 5
  • 29
  • 45
  • thanks. for Q2 does it mean that in web applications that apply these URLS like `user/65421` , `user/13456` , `user/9852` they have a file called `65421` and a file called `13456` and `9852` inside the user folder ? – Accountant م Nov 14 '16 at 14:13
  • 1
    @user56489870 No, they have created a .htaccess rule that takes user=65421 and translates it to user/65421. Check my reference in the post or this link https://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049 or simply google "htaccess pretty urls" :) – Bolli Nov 14 '16 at 14:30
  • thank you very much that is exactly what I needed to know. I guess you mean translate `user/65421` to `user=65421` not the other way, right? – Accountant م Nov 14 '16 at 14:40
  • I'm sorry, Is this what arkascha did mean by saying "rewriting incoming requests" in his comment ? – Accountant م Nov 14 '16 at 14:42
  • 1
    @user56489870 Yea - basically your code will work and look the same way as when GET parameters are user=65421, but the web-server will also understand user/65421 if you define it like that in .htaccess. Yes that is what he ment - some frameworks does it differently, using routing classes. Wordpress uses the .htaccess method with mod_rewrite to rewrite the requests. – Bolli Nov 14 '16 at 14:44
  • 1
    So yes if you define that rule in .htaccess it will rewrite user/65421 to user=65421 so your script understands it correct. – Bolli Nov 14 '16 at 14:46
  • 1
    thaaaank you very much, now I got the picture very well, I hope I can give you 50000 helpful vote ups, thank you so much for your time – Accountant م Nov 14 '16 at 14:54
  • Haha no problem mate :) – Bolli Nov 14 '16 at 14:55