I have a small application that is used to connect to several databases across multiple IPs, it takes input as the IP, UserName, Password. All three are free text fields and users can provide any value. Database is selected in the next screen.
The purpose of the this page is to validate if the credentials are valid.
var arrayInput = [ip, userName, password];
$.ajax({
url: 'login.php',
type: 'POST',
data: {
arrayInput
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
The PHP file is as below.
< ? php
mysqli_report(MYSQLI_REPORT_ALL | MYSQLI_REPORT_STRICT);
$ip=$_POST['arrayInput'][0];
$userName=$_POST['arrayInput'][1];
$password=$_POST['arrayInput'][2];
try {
$mysqli = mysqli_connect($ip,$userName,$password);
mysqli_close($mysqli);
$data = array('type' => 'success', 'message' => 'Connected');
header('Content-Type: application/json');
return json_encode($data);
} catch (Exception $e) {
mysqli_close($mysqli);
$data = array('type' => 'error', 'message' => 'Failure to Connect');
header('Content-Type: application/json');
return json_encode($data);
}
?>
Now the issue is that, it all works fine when the IP is correct. But in case when the IP is incorrect, the Mysqli_connect waits till the timeout and then goes to the exception.
Now the problem I am facing is that when the mysqli_connect is attempting connection I cannot launch the app in a new session to trigger a different connection, even though the IP is correct.
for e.g.
Valid IP is 127.0.0.1
and I tried 1.0.0.1
After this I cannot try 127.0.0.1 even though I close the browser and open a new browser. I have to wait all the way till timeout. Reducing the timeout is an option. But would want to see if there are other options by which I can close the connection.