个人在Ubuntu上使用交叉编译工具链 arm-none-linux-gnueabi 按照 DroidPHP 的教程
构建了适用于Android(ARM架构)和树莓派Raspbian(ARM架构基于Debian的Linux发行版)的PHP解释器(cli,cli-server).下载地址: http://pan.baidu.com/s/1gfJcXbX
内容包括:
main : 这个目录里面是项目源代码,主要就是MainActivity.java和assets数据.
phpdroid.apk: 应用示例,大小约7MB,包含PHP和BusyBox.
这里需要说明的是,BusyBox并不是PHP必备的东西,
打包它只是为了方便PHP能够调用里面常用的GNU/Linux命令,
去掉BusyBox后APK包还能缩小600KB.
需要强调的是,包里的PHP是路径无关的,运行也不需要root权限,
只要维持assets/php/的目录结构,放到你的应用里也能正常运行.
PHP的版本是5.5.9.
PHPDroid基本工作原理:
Java启动PHP内置的HTTP服务器,然后开一个WebView访问这个PHP驱动的HTTP服务.
其中,WebView用于实现人机交互,可以用传统的HTML/CSS/jQuery技术进行图形界面编程.
PHP则负责跟本地文件系统,SQLite数据库,网络进行交互.
需要强调的是,PHPDroid追求的不是像Java App那样能够访问Android系统提供的API.
PHPDroid的优势在于用传统的Web开发技术HTML/CSS/JS/PHP/SQL就能开发基于WebView的本地WebApp.
PHPDroid内置的本地PHP不能访问Android提供给Java的API,但可以操作本地文件系统(应用目录)和SQLite以及进行网络交互.
比如获取一个新闻列表,WebView通过AJAX访问本地PHP,PHP再通过cURL等访问远程服务器.
远程服务器返回JSON,里面包含新闻的标题,摘要,缩略图网址,本地PHP转成数组后循环输出到WebView.
可见这个本地PHP既是WebView的服务器端,又是远程服务器的客户端,是WebView和远程服务器数据交互的中转站.
把WebView和本地PHP看做一个整体,那它就是一个不能调用Android API的本地WebApp.
毕竟Android是Linux内核,一切皆文件的思想还是在那里的,
只要有权限,PHP读取一些系统数据(比如/proc/cpuinfo)并没有问题.
PHPDroid详细工作原理:
phpdroid/app/src/main/java/net/php/phpdroid/MainActivity.java
MainActivity在onCreate首次启动时复制:
/data/app/net.php.phpdroid.apk/assets/php/
到:
/data/data/net.php.phpdroid/php/
然后Runtime.getRuntime().exec执行PHP服务启动脚本:
/data/data/net.php.phpdroid/php/bin/start.sh
#!/system/bin/sh
chmod 700 $1/php/bin/busybox
chmod 700 $1/php/bin/php
$1/php/bin/php $1/php/bin/ua.php #随机生成UserAgent
$1/php/bin/php $1/php/bin/port.php #获取可用端口
$1/php/bin/php \
-c $1/php/bin/php.ini \
-d app_dir="$1" \
-d upload_tmp_dir="$1/php/tmp" \
-d session.save_path="$1/php/tmp" \
-S 127.0.0.2:`cat $1/php/bin/port` \
-t $1/php/www \
$1/php/bin/auth.php \
>/dev/null 2>&1 &
echo $! > $1/php/bin/pid #记录PHP的PID
return 0
这个脚本的作用就是,随机生成用于标记WebView的UserAgent,获取127.0.0.2上的可用端口,
然后启动PHP服务器,记录其PID,用于在kill关闭.
关于PHP内置HTTP服务器的介绍,请看:
https://wiki.php.net/rfc/builtinwebserver
其中:
/data/data/net.php.phpdroid/php/bin/ua.php
file_put_contents(dirname(__FILE__).'/ua', sha1(uniqid(mt_rand(), true)));
/data/data/net.php.phpdroid/php/bin/port.php
//PHP用 fsockopen 检测端口是否被占用,返回可用端口.
$port = 8181;
while ( $fp = @fsockopen('127.0.0.2', $port, $errno, $errstr, 1) ) {
fclose($fp);
$port++;
}
file_put_contents(dirname(__FILE__).'/port', $port);
/data/data/net.php.phpdroid/php/bin/auth.php
//触发PHP服务进程打开resolv_php.conf,要求resolv_php.conf跟auth.php同在一个目录
gethostbyname('localhost');
$ua = file_get_contents(dirname(__FILE__).'/ua');
if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']===$ua) {
return false;
} else {
exit('Auth Failed');
}
PHP服务在处理每个请求之前,都会执行auth.php文件,
如果ua(UserAgent)不匹配,程序就会exit退出.
Android上一个应用对应一个用户,每个应用目录只允许应用所属用户进行访问,
所以除非手机被root,否则其他应用是没法读取PHPDroid应用目录里的数据的.
应用MainActivity.java里读取ua文件并设置为WebView的UserAgent,所以能够访问PHP服务.
手机上的其他应用,比如浏览器,因为没有读取其他应用目录比如 /data/data/net.php.phpdroid 的权限,
也就无法读取PHPDroid生成的ua,自然也就无法访问PHP服务.
MainActivity.java
webview.getSettings().setUserAgentString(ua);
webview.loadUrl("http://127.0.0.2:" + port);
关于DNS解析,glibc默认访问的是/etc/resolv.conf
#define _PATH_RESCONF "/etc/resolv.conf"
在编译glibc时,我改成了相对路径:
#define _PATH_RESCONF "./resolv_php.conf"
/data/data/net.php.phpdroid/php/bin/resolv_php.conf
# 百度公共DNS http://dudns.baidu.com/
nameserver 180.76.76.76
# CNNIC公共DNS http://www.sdns.cn/
nameserver 1.2.4.8
nameserver 210.2.4.8
静态链接了glibc库的PHP,在执行auth.php里的gethostbyname('localhost')操作时,
会触发访问auth.php所在目录下的resolv_php.conf,从而进行DNS.
更好的方法应该是调用Android的getprop net.dns1获取本地DNS,然后加入到resolv_php.conf里.
但奇怪的是,在adb shell里执行 getprop net.dns1 能正确输出,
一套在PHP的 echo shell_exec('getprop net.dns1'); 就没有输出了.
执行 echo shell_exec('vmstat'); 调用其他命令是能正常输出的.
这里不知道是什么问题,权限问题么?请高人指点.
关于glibc的编译,我还把调用命令的/bin/sh改成了Android的/system/bin/sh,
这样PHP的shell_exec等函数才能正常运行.
sed -i "s{/bin/sh{/system/bin/sh{" ./libio/oldiopopen.c
sed -i "s{/bin/sh{/system/bin/sh{" ./libio/iopopen.c
sed -i "s{/bin/sh{/system/bin/sh{" ./posix/tst-vfork3.c
sed -i "s{/bin/sh{/system/bin/sh{" ./posix/bug-regex9.c
sed -i "s{/bin/sh{/system/bin/sh{" ./sysdeps/posix/system.c
sed -i "s{/bin/sh{/system/bin/sh{" ./sysdeps/generic/paths.h
sed -i "s{/bin/sh{/system/bin/sh{" ./sysdeps/unix/sysv/linux/paths.h
位于PHP里的proc_open函数也要进行类似修改:
sed -i "s{/bin/sh{/system/bin/sh{" ext/standard/proc_open.c
这样PHP就可以愉快地调用Android和BusyBox里提供的GNU/Linux常用命令了.
MainActivity在onKeyDown按下返回键KEYCODE_BACK退出应用时:
会调用stop.sh关闭PHP服务,stop.sh内容如下:
#!/system/bin/sh
ua=$1/php/bin/ua
if [ -r $ua ]; then
rm $ua
fi
port=$1/php/bin/port
if [ -r $port ]; then
rm $port
fi
pid=$1/php/bin/pid
if [ -r $pid ]; then
kill `cat $pid`
rm $pid
fi
return 0
就是把ua,port这两个文件删掉,并且关闭PHP进程.
其实MainActivity在启动时也会调用stop.sh清理上次应用可能意外退出遗留下来的东西.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software