最近几天我一直在尝试发送(通过表单(输入,提交按钮))并通过 php 脚本将数据接收到 android 应用程序。我设法做到了,但我不喜欢这个解决方案。我所做的是:创建了一个表单:
<form action="index.php" method="get">
<strong>Please give me a shell command:</strong> <br><input type="text" name="cmd" onmouseover="this.focus(); this.select();"><br>
<input type="submit" value="Submit">
</form>
当点击提交按钮时:
<?php
if ( isset($_GET['cmd']) ){
$cmd = $_GET['cmd'];
if (empty($cmd)){
echo 'Please fill in a command first!';
}else{
echo "You gave me : \"$cmd\", right?<br>";
}
// don't timeout!
set_time_limit(0);
// set some variables
//$host = $_SERVER['HTTP_HOST'];
$host = '10.16.5.191';
$port = 5000;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$output = $cmd;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "I have read :</br> $input";
}
?>
但后来我意识到想在按下提交按钮时打开一个连接(直到 socket_accept())并发送数据(socket_write())。
所以我这样做了:
<?php
if ( isset($_GET['cmd']) ){
main($_GET['cmd'],$spawn);
return;
}else{
// don't timeout!
set_time_limit(0);
// set some variables
//$host = $_SERVER['HTTP_HOST'];
$host = '192.168.10.21';
$port = 5000;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "spawn: $spawn</br>";
echo '
<form action="" method="get">
<strong>Please give me a shell command:</strong> <br><input type="text" name="cmd" onmouseover="this.focus(); this.select();"><br>
<input type="submit" value="Submit">
</form>
';
}
function main($cmd,$spawn){
// 1. Checks if the form is set
if (empty($cmd)){
echo 'Please fill in a command first!';
}else{
echo "You gave me : \"$cmd\", right?<br>";
}
$output = $cmd;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "I have read :</br> $input";
}
?>
所以现在我建立连接,然后我的 php 脚本结束!当我通过提交按钮发送东西时,$spawn 是未知的。
我也试过 $_SESSION[''] 但 socket_write() 说 $_SESSION[''] 变量是整数而不是资源变量。
android 代码非常简单,只需打开一个连接(通过套接字),读取和写入。
我是 PHP 编码新手(最近 3 天学习)我该怎么办?有更好的方法吗?我希望连接仅在本地网络上!