简体   繁体   English

Bottle Web Server-如何提供PHP文件?

[英]Bottle web server - how to serve PHP file?

I am working on a webapp made by someone else which uses Bottle routing. 我正在使用使用Bottle路由的其他人制作的Web应用程序。 I want to create a simple login page which requires some PHP. 我想创建一个需要一些PHP的简单登录页面。 If I return the PHP page as a static_file, any HTML will be executed but PHP won't, for obvious reasons. 如果我将PHP页面作为static_file返回,则出于显而易见的原因,将执行任何HTML,但不会执行PHP。 How should I serve the PHP file so that it is dynamic? 我应该如何提供PHP文件,使其动态化?

Not working: 无法运作:

@route('/login')
def serve():
   return static_file('login.php', root='.')

In order to server PHP files, you need to have PHP installed on the web server. 为了服务器PHP文件,您需要在Web服务器上安装PHP。 Additionally, the webserver needs to be configured to detect PHP files and execute them. 此外,需要将Web服务器配置为检测PHP文件并执行它们。

Serving PHP files from Python is kinda useless and not recommended. 从Python提供PHP文件是没有用的,因此不建议这样做。 I'd recommend you to take the time to translate this script from PHP to Python. 我建议您花些时间将此脚本从PHP转换为Python。

I wanted to do the same thing yesterday, but the answers I got to my question made it clear it was either impossible or extremely difficult. 我昨天想做同样的事情,但是我对问题的回答清楚地表明,这既不可能,也非常困难。 I came up with writing a small python program to run the PHP built in server. 我想到编写一个小的python程序来运行服务器内置的PHP。 NOTE: PHP needs to be able to run from the command line for this to work. 注意:PHP必须能够从命令行运行,才能正常工作。

#Import the os package so that this code can run commands
import os

#Get the port that the user wants to host on
port = str(input("What port would you like to host on?"))

#Add wanted port to the command that hosts the php server
cmd = "php -S localhost:" + port

#Actually run the command to host php server
os.system(cmd)

#Now the PHP server will take over until you
#use ctrl + C to quit hosting

Just remember that the port needs to be 4 numbers. 请记住,端口必须为4个数字。 When you host this, you can return any file from the folder you ran this code in by simply typing it in the browser. 托管此代码后,只需在浏览器中键入代码,即可从运行此代码的文件夹中返回任何文件。 Example: 例:

localhost:8080/login.php

Returns login.php (if it is there) on the localhost port that you asked for. 在您要求的localhost端口上返回login.php(如果存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM