簡體   English   中英

使用volley和php將android連接到mysql時禁止錯誤403

[英]error 403 forbidden when connecting android to mysql using volley and php

我一直在將我的Android應用程序連接到我的wamp服務器,但有一天它剛剛醒來就給出了這個錯誤BasicNetwork.performRequest: http://192.168.43.71/database/login.php的意外響應代碼403。 我確定它不是我的java代碼,但是我的服務器出了問題,我對配置服務器並不是很熟悉,而且我很沮喪。 我不明白的是,有一次它正在工作,它決定不這樣做。

這是我試過的 1.運行腳本使用瀏覽器,它工作正常2.卸載並重新安裝wamp服務器,它沒有工作3.將我的數據庫從localhost移動到在線服務器,它沒有工作3.我意識到只有我必須發布參數的PHP腳本才會出現此錯誤,如果我只需要從服務器檢索數據而沒有參數它可以工作

下面是我的登錄腳本和我的數據庫連接。 我確信數據庫連接正常

<?php
$response = array();
$array = array();
$details = array();

// check for required fields
if (!empty($_POST)) {
$phone_number = $_POST['user_phone_number'];
$password = md5($_POST['user_password']);
include 'database.php';
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql2 = "SELECT * FROM users WHERE user_phone_number OR  user_email = ? AND user_password = ?";
$result2 = $pdo->prepare($sql2);
$result2->execute(array($phone_number,$password));

while ($row = $result2->fetch(PDO::FETCH_ASSOC)){
    $array["user_phone"] = $row["user_phone"];
    $array["user_id"] = $row["user_id"];
    $array["user_imei"] = $row["user_imei"];
    $array["user_name"] = $row["user_name"];
    $array["user_surname"] = $row["user_surname"];
    $array["user_phone_number"] = $row["user_phone_number"];
    $array["user_email"] = $row["user_email"];
    $array["user_password"] = $row["user_password"];
 }

if($result2){
  if($array == null){
    $array["user_phone"] = "error";
    $array["message"] = "Wrong phone number or password";
    $json = json_encode($array);
    echo $json;

}else{
     $json = json_encode($array);
     echo $json;
  }


}


} else {
  $array["user_phone"] = "0";
  $array["message"] = "Required field(s) is missing";
  echo json_encode($array);

}
 Database::disconnect();
?>

數據庫連接

<?php
class Database{
private static $dbName = 'testdb';
private static $dbHost = 'localhost';
private static $dbUsername = 'root';
private static $dbPassword = '';

private static $cont = null;

public function __construct() {
    die('Init not allowed');
}
public static function connect(){
    //Open connection through the who;e application
    if(null == self::$cont){
        try {
            self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbPassword); 
        } catch (PDOException $ex) {
            die($ex->getMessage());
        }
    }
    return self::$cont;
}
public static function disconnect(){
    self::$cont == null;
}
}
?>

問題是可能是你的ip地址是動態的,它是變化的。 有時它會起作用,因為您當前的IP地址等於您的設置(192.168.43.71)。 當您啟動應用程序時,應確保您設置的IP地址與當前的IP地址相同。 使用ifconfig terminal命令是可能的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM