php写web服务器端,如何用php实现一个web服务器

①实现一个回显服务器

客户端发来一个请求,我们把请求包发回去显示。

创建监听套接字

新建start_web.php

//创建监听套接字

$web = stream_socket_server('0.0.0.0:8088');

接收请求,并回显

$conn = @stream_socket_accept($web);

if($conn){

fwrite($conn,fgets($conn));

fclose($conn);

}

启动服务

php start_web.php //启动服务

浏览器访问

http://0.0.0.0:8088/?id=1

显示结果

GET /?id=1 HTTP/1.1

上面的例子在接收客户端连接后,会回显消息。

但是服务端会中断服务。

我们改进一下代码如下:

$web = stream_socket_server('0.0.0.0:8088');

while(1){

$conn = @stream_socket_accept($web);

if($conn){

fwrite($conn,fgets($conn));

fclose($conn);

}

}

注意 ctrl+c 可以中断服务器运行

②解析请求报文

要求如下

区分GET和POST

获取请求变量

$_SERVER = array();

//创建一个tcp套接字,并监听8088端口

if($web = stream_socket_server('0.0.0.0:8088',$errno,$errstr)){

while(true){

$conn = @stream_socket_accept($web);

if($conn){

$_SERVER = array();

decode(fgets($conn));

fwrite($conn,encode("访问方法是:".$_SERVER['REQUEST_METHOD']."\n请求变量是:".$_SERVER['QUERY_STRING']));

fclose($conn);

}

}

}else{

die($errstr);

}

//http协议解码

function decode($info){

global $_SERVER;

list($header,) = explode("\r\n\r\n",$info);

//将请求头变为数组

$header = explode("\r\n",$header);

list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = explode(' ', $header[0]);

$_SERVER['QUERY_STRING'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);

}

//http协议加密

function encode($str){

$content = "HTTP/1.1 200 OK\r\nServer: vruan_web/1.0.0\r\nContent-Length: " . strlen($str )."\r\n\r\n{$str}";

return $content;

}

启动服务

php start_web.php //启动服务

浏览器访问

http://0.0.0.0:8088/?id=1&age=19

显示结果

访问方法是:GET

请求变量是:id=1&age=19

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YACS 是一个强大的 PHP 脚本,可以让你维护一个动态的 Web 服务器。特性:- Runs on your own server, or on a shared web site- Post articles with web forms, by e-mail, or remotely (w:bloggar)- Embed images and photos in articles --automatic resize- Each section can be a weblog, a discussion board, a book of cooking recipes,etc, or even a plain list of articles- Overlay interface for PHP developers, to add extra functionality to articles,such as polls or cooking recipes- Display the content tree in Freemind- Comments, with quoting- Archives per week and per month- The home page is updated automatically on article publishing- Categories, sub-categories, etc. --Build your own Yahoo! or DMOZ...- Real-time meetings with community members- Private discussions and messages- Search on any word --text of articles is fully indexed- Multiple authors --actually, a community of contributors- Articles are visible only on publication after review by editors- Articles and sections can have dead-line to limit visibility over time- A straightforward control panel, and a set of configuration panels- File upload to articles , sections or categories- Attach links to articles, sections or categories- A comprehensive set of UBB-like codes are available to beautify your posts- Integrated support of TinyMCE and of FCKEditor- Fully customizable skins- Easy integration of Google Maps- Add a comprehensive web interface to existing collections of files- Support audio-on demand and video-on demand- Automatic web slideshow for shared photos- RSS syndication- Easy installation- XML-RPC interface (implementing the Blogger API and metaWeblog API) 标签:YACS

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值