Jump to content

[SOLVED] Another quiz qestion


ryy705

Recommended Posts

Hello,

 

Could someone kindly help me with the following question?  I ran it from the command and it does indeed hang.  But I don't know the reason.  How is the "request blocking" ?

What is wrong with the following code snippet? Assume default configuration values apply.
<?php

$fp = fsockopen('www.php.net', 80);
fwrite($fp, "GET / HTTP/1.0\r\nHost: www.php.net\r\n");
$data = fread($fp, 8192);

?>


Answer...
The request is blocking and may cause fread() to hang
The HTTP request is malformed
This script should be re-written using fgets() instead of fread()
The request is non-blocking and fread() may miss the response
You cannot use fwrite() with fsockopen() 

Link to comment
https://forums.phpfreaks.com/topic/133459-solved-another-quiz-qestion/
Share on other sites

when you make a http request you send headers in your case the headers are "GET / HTTP/1.0\r\nHost: www.php.net\r\n"

each header is separated with a \r\n (Windows new line) but to tell the server you have finished sending the headers you need to send a \r\n by its self to tell it you have finished and to start sending the web page.

your headers should be "GET / HTTP/1.0\r\nHost: www.php.net\r\n\r\n"

also be aware that is a very basic request and some servers may want you to provide more details in your request.

 

Scott.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.