简体   繁体   中英

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

I have been connecting my android app to my wamp server but one day it just woke up giving this error BasicNetwork.performRequest: Unexpected response code 403 for http://192.168.43.71/database/login.php . I am sure its not my java code but there is something wrong with my server, i am not very familiar with configuring the server and i am drowning in my frustration. What i dont understand is one time it was working and it just decided not to.

This is what i tried 1. Running the script using the browser and it works fine 2. Uninstalling and reinstalling wamp server and it did not work 3. Moving my database from localhost to an online server and it did not work 3. I realised that only php scripts in which i have to post parameters give this error, in cases where i just have to retrieve data from the server without parameters it works

Below is my login script and my database connection. I am sure the database connection is working fine

<?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();
?>

Database Connection

<?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;
}
}
?>

The problem is that may be your ip address is dynamically and it is change. Sometimes it work because your current ip address is equals to your set (192.168.43.71). When you start the app should be sure that your ip address which you set is the same like your current. It is possible with ifconfig terminal command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM