session_start if _SESSION auth true echo error Пользователь не авториз

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?
session_start();
if ($_SESSION["auth"] != true) {
echo "{error:'Пользователь не авторизован!'}";
exit();
}
function get_info($user, $pass, $ip) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://".$ip."/protect/leds.xml");
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
$result = curl_exec($ch);
$st = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($st != 200)
return false;
return $result;
}
function check_status($user, $pass, $ip, $id) {
$raw_xml = get_file($user, $pass, $ip);
# Если нет ответа от контроллера
if (!$raw_xml) {
$query = "UPDATE blocks WHERE id = $id SET (status, status_date) = (0, now()); UPDATE rele WHERE id_block = $id SET status = NULL";
pg_query($query);
return flase;
}
# Считываем данные от контроллера
$xml = simplexml_load_string($raw_xml);
$xml = $xml->xpath('//response');
$xml = (array)$xml[0];
# Проверка работоспособности реле у контроллера
$query = "UPDATE rele WHERE id_block = $id SET status = 0";
pg_query($query);
for ($i = 0; $i < 6; $i++) {
if ($xml['led'.$i] == 1) {
$query = "UPDATE rele WHERE id_block = $id AND out_n = $i SET status = 1";
pg_query($query);
}
}
# Прописываем статус для контроллера
$query = "UPDATE blocks WHERE id = $id SET (status, status_date) = (0, now())";
pg_query($query);
return true;
}
$db = pg_connect("host=localhost dbname=equipment user=postgres password=*****") or die();
# Бесконечный цикл
while (True) {
$query = "SELECT id,ip,blocks.user,pass FROM blocks ORDER BY status_date ASC LIMIT 1";
$row = pg_fetch_assoc(pg_query($query));
check_status($row['user'], $row['pass'], $row['ip'], $row['id']);
}