我在我的 Main 类login=root&password=root
中authenthication.php
使用Authorize(String login, String password)
来自AuthMediator
(扩展 PHPMediator)的方法发送 POST 请求。
问题是 - 我的 PHP 脚本从来没有收到任何东西($_SERVER['QUERY_STRING'
] 是空的,但是当我尝试手动执行 GET 时 - 它不是)。当所有代码都在一个类(主类)文件中时,它起作用(即 PHP 正在获取请求并处理它们)。但是在我把它分成不同的文件之后,它就停止了工作。
也许有一些访问修改器在玩一些有趣的把戏?我是 Java 新手,但我仍然不完全了解它们会产生哪些副作用。
这是我的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;
}
}
它是父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();
}
}