I'm sending POST request login=root&password=root
to authenthication.php
by using Authorize(String login, String password)
method from AuthMediator
(which extends PHPMediator) in my Main class.
The problem is - my PHP script never receives anything($_SERVER['QUERY_STRING'
] is empty, but when I try to execute GET manually - it isn't). It worked(i.e. PHP was getting requests and processing them) when all the code was in one class(Main class) file. But after I've splitted it into different files, it stopped working.
Maybe there's some access modificators playing some funny tricks? I'm new to Java, and I'm still not fully understanding, which side effects they can have.
Here is my authentication.php:
<?php
if(isset($_POST['login']) && isset($_POST['password']) )
{
$login=$_POST['login'];
$password=$_POST['password'];
$userfile_path="../users/".$login;
if(!file_exists($userfile_path))
{
echo "ERR_USER_INVALID";
}
else
{
$hash=md5($login.$password);
$userfile=fopen($userfile_path,"r");
if($userfile!=FALSE)
{
if(fgets($userfile)==$hash)
{
echo "OK_USER_VALID";
}
else
{
echo "ERR_USER_INVALID";
}
fclose($userfile);
}
else
{
echo "ERR_OPENING_USER_FAILED";
}
}
}
else
{
echo "ERR_AUTH_MALFORMED_POST_REQUEST(".$_SERVER['QUERY_STRING'] .")";
}
?>
AuthMediator.class:
package WebActivity;
import Constants.MagicConstants;
public class AuthMediator extends PHPMediator {
public AuthMediator() throws Exception {
super(MagicConstants.AUTH_URL);
}
public String Authorize(String login, String password) {
String _data="login="+login+"&password="+password;
try {
this.sendData(_data);
this.receiveData();
} catch (Exception ex) {
return "ERR_EXCEPTION";
}
return this._response;
}
}
And it's parent PHPMediator.class:
package WebActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class PHPMediator {
private URL _phpUrl;
private URLConnection _urlConn;
private OutputStreamWriter _outWrite;
private BufferedReader _bufRead;
protected String _response;
PHPMediator(String url) throws Exception {
this._response="";
//opens connection
this._phpUrl=new URL(url);
this._urlConn=_phpUrl.openConnection();
this._urlConn.setDoOutput(true);
//initializes writer and reader
this._outWrite=new OutputStreamWriter(_urlConn.getOutputStream());
this._bufRead=new BufferedReader(new InputStreamReader(_urlConn.getInputStream()));
}
protected void setUrl(String url) throws Exception{
//opens connection
this._phpUrl=new URL(url);
this._urlConn=_phpUrl.openConnection();
this._urlConn.setDoOutput(true);
//initializes writer and reader
this._outWrite=new OutputStreamWriter(_urlConn.getOutputStream());
this._bufRead=new BufferedReader(new InputStreamReader(_urlConn.getInputStream()));
}
protected void sendData(String data) throws IOException {
String _data;
_data=URLEncoder.encode(data, "UTF-8");
this._outWrite.write(_data);
this._outWrite.flush();
}
protected void receiveData() throws IOException {
this._response+=this._bufRead.readLine();
this._outWrite.close();
this._bufRead.close();
}
}