+1 vote
by
Hello!
There is a form for sending requests from the site to telegram chat.
Everything works.
Please suggest the possibility to get the ip-address of the person who sends a request from the form on the site. Thank you.
Here is the handler for this form.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$args = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if (!empty($args['uname'])){
$arr = [
"Заявка"=>$args['utheme'],
"Имя"=>!empty($args['uname']) ? $args['uname'] : '',
"Телефон: "=>!empty($args['uphone']) ? $args['uphone'] : '',
];
$token = "";
$chat_id = "";
$txt="";
foreach($arr as $key => $value) {
$txt .= "<b>".$key."</b> ".$value."%0A";
};
$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");
if ($sendToTelegram) {
echo '<p class="success">Ваша заявка принята.</p>';
return true;
} else {
echo '<p class="fail"><b>Ошибка.</b></p>';
}
} else {
echo '<p class="fail">Ошибка. Заполните обязательные поля.</p>';
}
} else {
header ("Location: /");
}
by
So pass the usual one.$_SERVER['REMOTE_ADDR']; into the handler along with other data

2 Answers

0 votes
by
 
Best answer
Apparently, it's something like this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$args = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if (!empty($args['uname'])){
$arr = [
"Заявка"=>$args['utheme'],
"Имя"=>!empty($args['uname']) ? $args['uname'] : '',
"Телефон: "=>!empty($args['uphone']) ? $args['uphone'] : '',
"IP адрес:" => $_SERVER['REMOTE_ADDR']
];
$token = "";
$chat_id = "";
$txt="";
foreach($arr as $key => $value) {
$txt .= "<b>".$key."</b> ".$value."%0A";
};
$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");
if ($sendToTelegram) {
echo '<p class="success">Ваша заявка принята.</p>';
return true;
} else {
echo '<p class="fail"><b>Ошибка.</b></p>';
}
} else {
echo '<p class="fail">Ошибка. Заполните обязательные поля.</p>';
}
} else {
header ("Location: /");
}
+2 votes
by
I'm not an expert, of course, and I may get spit on, but:
<input type="hidden" id="ip" name="ip" value="<?$_SERVER['REMOTE_ADDR']?>">

That should help you.
by
You shouldn't think so, even if you're not an expert, you shared your opinion, and that's important, thank you very much. But for some reason the handler doesn't want to accept this data, a blank message comes to telegram chat. It seemed to me too that this option would be the right one, it was the first one used before the question was posed.
...