-
-
Save SoeAung95/10311da6f589778177739b35c127471c to your computer and use it in GitHub Desktop.
Gist_Clone_Telegram_Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script async | |
src="https://telegram.org/js/telegram-widget.js?22" | |
data-telegram-login="MagicSszBot" | |
data-size="large" | |
data-radius="10" | |
data-auth-url="https://yourdomain.com/auth/telegram" | |
data-request-access="write"> | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('BOT_USERNAME', 'XXXXXXXXXX'); // place username of your bot here | |
function getTelegramUserData() { | |
if (isset($_COOKIE['tg_user'])) { | |
$auth_data_json = urldecode($_COOKIE['tg_user']); | |
$auth_data = json_decode($auth_data_json, true); | |
return $auth_data; | |
} | |
return false; | |
} | |
if ($_GET['logout']) { | |
setcookie('tg_user', ''); | |
header('Location: login_example.php'); | |
} | |
$tg_user = getTelegramUserData(); | |
if ($tg_user !== false) { | |
$first_name = htmlspecialchars($tg_user['first_name']); | |
$last_name = htmlspecialchars($tg_user['last_name']); | |
if (isset($tg_user['username'])) { | |
$username = htmlspecialchars($tg_user['username']); | |
$html = "<h1>Hello, <a href=\"https://t.me/{$username}\">{$first_name} {$last_name}</a>!</h1>"; | |
} else { | |
$html = "<h1>Hello, {$first_name} {$last_name}!</h1>"; | |
} | |
if (isset($tg_user['photo_url'])) { | |
$photo_url = htmlspecialchars($tg_user['photo_url']); | |
$html .= "<img src=\"{$photo_url}\">"; | |
} | |
$html .= "<p><a href=\"?logout=1\">Log out</a></p>"; | |
} else { | |
$bot_username = BOT_USERNAME; | |
$html = <<<HTML | |
<h1>Hello, anonymous!</h1> | |
<script async src="https://telegram.org/js/telegram-widget.js?2" data-telegram-login="{$bot_username}" data-size="large" data-auth-url="check_authorization.php"></script> | |
HTML; | |
} | |
echo <<<HTML | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Login Widget Example</title> | |
</head> | |
<body><center>{$html}</center></body> | |
</html> | |
HTML; | |
?> |
Author
SoeAung95
commented
Dec 29, 2024
$value) {
$data_check_arr[] = "$key=$value";
}
sort($data_check_arr);
$data_check_string = implode("\n", $data_check_arr);
// Generate hash for verification
$hash = hash_hmac("sha256", $data_check_string, $secret);
if (strcmp($hash, $check_hash) !== 0) {
die("Data is NOT from Telegram!");
}
// Output user data
$user_id = $_GET["id"];
$username = $_GET["username"];
$first_name = $_GET["first_name"];
$last_name = $_GET["last_name"];
echo "Welcome, $first_name $last_name ( @$username, ID: $user_id )";
?>
• Example: https://example.com/auth/telegram.
$value) { $data_check_arr[] = "$key=$value"; } sort($data_check_arr); $data_check_string = implode("\n", $data_check_arr); // Generate hash for verification $hash = hash_hmac("sha256", $data_check_string, $secret); if (strcmp($hash, $check_hash) !== 0) { die("Data is NOT from Telegram!"); } // Output user data $user_id = $_GET["id"]; $username = $_GET["username"]; $first_name = $_GET["first_name"]; $last_name = $_GET["last_name"]; echo "Welcome, $first_name $last_name ( @$username, ID: $user_id )"; ?>
$ curl -OL https://github.com/psf/requests/tarball/main
optionally, zipball is also available (for Windows users).
*** Topic #70027
*** Body #152826
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment