我在Visual Basic中编写了一小段代码,用于在本地托管的login.php上发布用户名和密码,这种工作方式很有用,但由于某种原因,PHP脚本完全跳过if语句检查是否设置了参数username
,并返回-1
。
PHP代码:
$connection = mysqli_connect('127.0.0.1', 'root', 'root', 'default');
if(mysqli_connect_errno()) stop('2');
if(isset($_POST['username']) == true) {
if(isset($_POST['password']) == true) {
$username = mysqli_escape_string($connection, $_POST['username']);
$password = mysqli_escape_string($connection, $_POST['password']);
$query = mysqli_query($connection, "SELECT username,password,usersalt FROM Users WHERE username='$username';");
if($result = $query) {
while($row = mysqli_fetch_row($result)) {
if($username == $row[0]) {
$password = sha1($password.$row[2]);
if($password == $row[1]) {
stop('5');
}
stop('4');
} else { stop('3'); }
}
}
} else { stop('1'); }
} else { stop('0'); }
stop('-1'); // Returns at this no matter what.
function stop($m) { global $connection;
mysqli_close($connection);
exit($m);
}
VB代码:
Public Function Login(Username As String, Password As String) As Int32
Try
If Username Is Nothing Then Return 0
If Password Is Nothing Then Return 1
Console.WriteLine("Establishing connection to login server")
Dim Request As WebRequest = WebRequest.Create(Server & APIRequest.Login)
Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Dim Content As Byte() = Encoding.UTF8.GetBytes(String.Format("username={0}&password={1}", Username, Password))
Request.ContentLength = Content.Length
Console.WriteLine("Passing credentials to server")
Dim DataStream As Stream = Request.GetRequestStream()
DataStream.Write(Content, 0, Content.Length)
DataStream.Close()
Dim Response As WebResponse = Request.GetResponse()
DataStream = Response.GetResponseStream()
Console.WriteLine("Getting server response")
Dim Reader As New StreamReader(DataStream)
Dim ResponseString As String = Reader.ReadToEnd
Reader.Close()
DataStream.Close()
Response.Close()
If ResponseString IsNot Nothing Then
Dim int As Int32 = -1
Integer.TryParse(ResponseString, int)
Console.WriteLine("Server response: {0}", int)
Return int
End If
Catch Exception As Exception
End Try
Console.WriteLine("Unknown error occurred.")
Return -1
End Function
VB输出:
Establishing connection to login server
Passing credentials to server
Getting server response
Server response: -1
更新
我已经解决了发生在线上的错误:
while($row = mysqli_fetch_row($result)) {
我用以下代码替换了这行代码:
$row = mysqli_fetch_row($result);
答案 0 :(得分:0)
更改此
$connection = mysqli_connect('127.0.0.1', 'root', 'root', 'default');
if($connection->connect_errno()) stop('2');//add connection name
简要
$connection->
connect_errno()
答案 1 :(得分:0)
我已经解决了发生在线上的错误:
while($row = mysqli_fetch_row($result)) {
我用以下代码替换了这行代码:
$row = mysqli_fetch_row($result);