当前位置: 首页 > 工具软件 > PHP Web Stat > 使用案例 >

Start-web.php,start_web.php

宰父飞翼
2023-12-01

/**

* This file is part of workerman.

*

* Licensed under The MIT License

* For full copyright and license information, please see the MIT-LICENSE.txt

* Redistributions of files must retain the above copyright notice.

*

* @author walkor

* @copyright walkor

* @link http://www.workerman.net/

* @license http://www.opensource.org/licenses/mit-license.php MIT License

*/

use \Workerman\Worker;

use Workerman\Protocols\Http\Request;

use Workerman\Protocols\Http\Response;

use Workerman\Connection\TcpConnection;

/**

* WebServer可以用nginx+php-fpm代替

*/

require_once __DIR__ . '/vendor/autoload.php';

require_once __DIR__ . '/config.php';

if (isset($SSL_CONTEXT)) {

$web = new Worker("http://0.0.0.0:9988", $SSL_CONTEXT);

$web->transport = 'ssl';

} else {

// WebServer

$web = new Worker("http://0.0.0.0:9988");

}

// WebServer进程数量

$web->count = 2;

define('WEBROOT', __DIR__ . DIRECTORY_SEPARATOR . 'web');

$web->onMessage = function (TcpConnection $connection, Request $request) {

$path = $request->path();

if ($path === '/') {

$connection->send(exec_php_file(WEBROOT.'/index.html'));

return;

}

$file = realpath(WEBROOT. $path);

if (false === $file) {

$connection->send(new Response(404, array(), '

404 Not Found

'));

return;

}

// Security check! Very important!!!

if (strpos($file, WEBROOT) !== 0) {

$connection->send(new Response(400));

return;

}

if (\pathinfo($file, PATHINFO_EXTENSION) === 'php') {

$connection->send(exec_php_file($file));

return;

}

$if_modified_since = $request->header('if-modified-since');

if (!empty($if_modified_since)) {

// Check 304.

$info = \stat($file);

$modified_time = $info ? \date('D, d M Y H:i:s', $info['mtime']) . ' ' . \date_default_timezone_get() : '';

if ($modified_time === $if_modified_since) {

$connection->send(new Response(304));

return;

}

}

$connection->send((new Response())->withFile($file));

};

function exec_php_file($file) {

\ob_start();

// Try to include php file.

try {

include $file;

} catch (\Exception $e) {

echo $e;

}

return \ob_get_clean();

}

// 如果不是在根目录启动,则运行runAll方法

if(!defined('GLOBAL_START'))

{

Worker::runAll();

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

 类似资料: