我一直在尝试发送(通过表单(输入,提交按钮))并通过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天学到的) 我该怎么办?有没有更好的方法呢? 我希望连接只能在本地网络上使用!