I have been trying the last days to send(through form(input, submit button)) and receive data through a php script to an android application. I managed to do that but I didn't liked the solution. What I did was: Created a form:
<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>
and when submit button was clicked:
<?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
");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener
");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection
");
$output = $cmd;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output
");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input
");
// clean up input string
$input = trim($input);
echo "I have read :</br> $input";
}
?>
But then I realized that want to open a connection once(until socket_accept()) and send data(socket_write()) when pressing the submit button.
So I did this:
<?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
");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener
");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection
");
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
");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input
");
// clean up input string
$input = trim($input);
echo "I have read :</br> $input";
}
?>
So now i make the connection and then my php script ends! And when I send something via Submit button $spawn is not known.
I tried also $_SESSION[''] but socket_write() says that $_SESSION[''] variable is integer and not resource variable.
The android code is really simple, just opening a connection(via sockets), reads and writes.
I am new in PHP coding(last 3 days learned) What should I do? Is there a better way to do it? I want the connection to be only on local network!