There is a script in JS?

which performs asynchronous actions?
March 23rd 20 at 19:01
2 answers
March 23rd 20 at 19:03
Solution
Line _username_, _password_, _database_, _Primer_, _row_ replace your actual values/parameters.
$DB = mysqli_connect("localhost", "_username_", "_password_", "_database_") or die(mysqli_error($DB));

function ping($IP)
{
global $DB;
if (!preg_match("/^(\d{1,3})(\.\d{1,3}){3}$/", $IP)) return false;
exec('ping -c 2-i 0.2' .$IP,$ping);
$nachalo = substr($ping[1],0,4);
if ($nachalo !== "From")
{
 mysqli_query($DB, "INSERT INTO _Primer_ (_row_) VALUES(1)") or die(mysqli_error($DB));
}
else 
{
 mysqli_query($DB, "INSERT INTO _Primer_ (_row_) VALUES(0)") or die(mysqli_error($DB));
}
}
Yes, anywhere! :-D
For example, after each mysqli_query(). - Eldridge61 commented on March 23rd 20 at 19:06
Comes out what? And how set?
In General, what code was the result? - Eldridge61 commented on March 23rd 20 at 19:09
Inside public function ping($phone_ip) string global $link; not needed, does not make sense.
In SQL queries backslashes inside VALUES() is also not needed.

And in the browser after the "1/0" some error is thrown? Our database entry appears?

By the way, if run on Windows, according to the code, no real test of the ping and will not, simply returns TRUE. - Eldridge61 commented on March 23rd 20 at 19:12
@Eldridge61,
And in the browser after the "1/0" some error is thrown?

No
Our database entry appears?

No
By the way, if run on Windows, according to the code, no real test of the ping and will not, simply returns TRUE.

MacOS
I was thinking, maybe I'm wrong the table created in phpMyadmin. Corrected, as you said, nothing has changed - Alice_Wal commented on March 23rd 20 at 19:15
See logs WEB-server for errors in PHP.

Or temporarily, TEMPORARILY! make at the beginning of the script:
error_reporting(E_ALL); ini_set("display_errors", 1);

Then on the page you should see all PHP errors. - Eldridge61 commented on March 23rd 20 at 19:18
The variable $link is not defined, does not happen full $link = mysqli_connect()... - Eldridge61 commented on March 23rd 20 at 19:21
mysqli_stmt is prepared to perform queries.
And you need to do some kind of if() with conditions when you need/don't need to add a record to the database.
So we dance from the necessary conditions... - Eldridge61 commented on March 23rd 20 at 19:24
But where does the initiation of a record in the database?
In other words, what causes the script to start?
Here in this place, and it is necessary to put restriction. - Eldridge61 commented on March 23rd 20 at 19:27
No. Run this PHP file, where/how is it made? He times a second is run from the console? Or times per second in the browser? Or every 5 seconds in the browser? Or every XX seconds via some API?
Here on this launch and worth a look, maybe it can be configured to time-in-30-seconds?

But if run every second, and do it on the side of the script is the most primitive way:
if (time() % 30 == 8)
{
 // code running in the 08-th and 38-th second of each minute.
 // change to '== 3' - will be every 03-th and 33-th second of every minute...
}
- Eldridge61 commented on March 23rd 20 at 19:30
In this if() checks, not Windows th is the operating system, and if so return always true, without running the ping-a:
if ($this->my OS is()) {
 return true;
}


The argument run ping -c1 ping makes only one ICMP request and stop work.
if (!exec("ping -q-c1" . $phone_ip . ">/dev/null 2>&1 ; echo $?")) {


And run the script only once, when the direct user is connected to Wi-Fi - it is necessary to understand where and how you get the IP, why and how often it occurs, and why it should be done every 30 seconds?
In General this is not a question, and little work... - Eldridge61 commented on March 23rd 20 at 19:33
The easiest thing in another table `Computer_Check` to store the list of IP that have already been added (record '1' in table `First_Computer`). Then add in the `First_Computer` changes:
if ((new Monitoring())->ping($phone_ip)) {
 echo 1; // in network
 if (mysqli_fetch_assoc(mysqli_query($link, "SELECT `ip` FROM `Computer_Check` WHERE `ip`='".mysqli_real_escape_string($link, $phone_ip)."'")) === NULL) {
 mysqli_query($link, "INSERT INTO `First_Computer` (`decor`) VALUES ('1')") or die(mysqli_error($link));
 mysqli_query($link, "REPLACE INTO `Computer_Check` (`ip`) VALUES ('".mysqli_real_escape_string($link, $phone_ip)."')") or die(mysqli_error($link));
}
} else {
 echo 0; // offline
 if (mysqli_fetch_assoc(mysqli_query($link, "SELECT `ip` FROM `Computer_Check` WHERE `ip`='".mysqli_real_escape_string($link, $phone_ip)."'")) !== NULL) {
 mysqli_query($link, "INSERT INTO `First_Computer` (`decor`) VALUES('0')") or die(mysqli_error($link));
 mysqli_query($link, "DELETE FROM `Computer_Check` WHERE `ip`='".mysqli_real_escape_string($link, $phone_ip)."'") or die(mysqli_error($link));
}
}
mysqli_close($link);


But if you do not need to store the entire history of the "connected - disconnected - connected - disconnected", and only the current value "Yes or no" - we can do a table `Computer_Check`. - Eldridge61 commented on March 23rd 20 at 19:36
March 23rd 20 at 19:05
@Alice_Wal, it's not just looks - it is exactly what you need to study - Leopoldo_Stiedema commented on March 23rd 20 at 19:08
on the Internet a lot of information, but haven't found something similar...

@Alice_Wal, and if so or even so?
What You do not understand the official documentation? - gavin_Russel87 commented on March 23rd 20 at 19:11
@Leopoldo_Stiedema, corrected. - gavin_Russel87 commented on March 23rd 20 at 19:14

Find more questions by tags JavaScript