我试图建立一个PHP网站,我想测试我的PHP文件,而不是上传到我的主机。基本上是在上传之前先在我自己的机器上测试。我怎么做呢?
当前回答
您还可以使用代码在PHP中创建自己的服务器!
<?php
set_time_limit(0);
$address = '127.0.0.1';
$port =4444;
$server = '$address + $port';
// <-- Starts Server
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind to address');
echo "\n Server is running on port $port waiting for connection... \n\n";
while(1)
{
socket_listen($sock);
$client = socket_accept($sock);
$input = socket_read($client, 443);
$incoming = array();
$incoming = explode("\r\n", $input);
$fetchArray = array();
$fetchArray = explode(" ", $incoming[0]);
$file = $fetchArray[1];
if($file == "/"){
$file = "src/browser.php";//当服务器启动时,这个文件是打开的!
} else {
$filearray = array();
$filearray = explode("/", $file);
$file = $filearray[1];
}
echo $fetchArray[0] . " Request " . $file . "\n";
// <-- Control Header
$output = "";
$Header = "HTTP/1.1 200 OK \r\n" .
"Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" .
"Content-Type: text/html \r\n\r\n";
$Content = file_get_contents($file);
$output = $Header . $Content;
socket_write($client,$output,strlen($output));
socket_close($client);
}
print('server running..');
运行这段代码,然后打开浏览器到localhost:443或任何您选择的端口
其他回答
如果你有一台本地机器,有正确的软件:支持PHP的web服务器,没有理由你不能像你描述的那样做。
目前我正在一台Windows XP机器上使用XAMPP,(在家里)使用Kubuntu和LAMP堆栈。
安装并运行XAMPP: http://www.apachefriends.org/en/xampp.html
我用WAMP。一个简单的安装向导,为Apache和PHP预配置的大量模块,易于打开和关闭,以匹配您的远程配置。
这是一个简单的,确保本地运行php服务器的方法:
php -S 0.0.0.0:<PORT_NUMBER>
PORT_NUMBER为1024 ~ 49151的整数
例如:php -S 0.0.0.0:8000
注:
如果你使用localhost而不是0.0.0.0,你可能会碰到一个 拒绝连接错误。 如果想让任何接口都可以访问web服务器,请使用0.0.0.0。 如果URI请求没有指定 文件,则index.php或index.html在给定目录下都是 返回。
给定以下文件(router.php)
<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
return false; // serve the requested resource as-is.
} else {
echo "<p>Welcome to PHP</p>";
}
?>
运行这个…
php -S 0.0.0.0:8000 router.php
... 并在浏览器中导航到http://localhost:8000/,将显示如下:
Welcome to PHP
参考:
内置web服务器
AppServ是一个在Windows中运行的小程序:
Apache PHP MySQL phpMyAdmin
它还将为Apache提供启动和停止按钮。我觉得这很有用。
推荐文章
- 如何从一个查询插入多行使用雄辩/流利
- 在PHP单元测试执行期间,如何在CLI中输出?
- 在PHP中使用heredoc的优势是什么?
- PHP中的echo, print和print_r有什么区别?
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 更好的方法检查变量为null或空字符串?
- 当使用Composer的开发/生产开关时,如何正确部署?