简体   繁体   中英

Bottle web server - how to serve PHP file?

I am working on a webapp made by someone else which uses Bottle routing. I want to create a simple login page which requires some PHP. If I return the PHP page as a static_file, any HTML will be executed but PHP won't, for obvious reasons. How should I serve the PHP file so that it is dynamic?

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. Additionally, the webserver needs to be configured to detect PHP files and execute them.

Serving PHP files from Python is kinda useless and not recommended. I'd recommend you to take the time to translate this script from PHP to 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. NOTE: PHP needs to be able to run from the command line for this to work.

#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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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