简体   繁体   中英

Using HTTP GET Method to Retrieve PHP script

(I'm a beginner. This is to understand PHP better.)

While experimenting with HTTP I was trying to GET a PHP-file from my server and I didn't get anything. Neither the HTTP-Header Content-Length nor any content is available for reading in the response.

string url = "http://php.net/sites.php";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.Method = "GET";
Stream stream = request.GetRequestStream(); // "ProtocolViolationException" is thrown here
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("test 3");

So with that C# code above and with my local IIS i can't GET any php files right? I can GET only html, js, css, images and a few_other right? What I found out as well is if I want the PHP script to be executed I need to change "GET" to "POST" and then I will be getting the PHP output.

What I would like to understand:

  • There is no way for me with common Web-Servers to retrieve PHP code?
  • Webbrowsers do POST-requests automatically when retrieving php output?
  • Why is it like this?

FYI: (not important here) I have to write C# app later that is going to communicate to a WebServer to retrieve some data and do actions that Webbrowsers can't do.

So with that C# code above and with my local IIS i can't GET any php files right?

If you statically host the php files you can get them from your server but this would be unusual indeed.

I can GET only html, js, css, images and a few_other right?

You can get whatever content that your server is sending, in this case IIS is receiving your GET request and deciding what to do with it.

There is no way for me with common Web-Servers to retrieve PHP code?

If you set up your own server to host the php files as static content then you can see your own php files. There is no way to see other's PHP source files unless their server is configured similarly.

Webbrowsers do POST-requests automatically when retrieving php output?

Browsers do get and post. Browsers GET when you type something into the address bar and POST when you submit a form.

Why is it like this?

PHP is a server side language, exposing the code is a huge security risk. This is also code that must execute on the server so there is no point in the client side (browser) seeing the code that is on the server except for potential debugging purposes.

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