How to access Telegram web app parameters from URL fragment in PHP

I’m developing a Telegram bot using a web app and facing challenges with getting data from the URL. The issue arises from the fact that the relevant information appears after the hash (#) in the URL, making it inaccessible via the standard $_GET array in PHP.

For context, here’s how my URL is structured:

https://mysite.com/app.php#tgWebAppData=query_id=BBFc4eW4BgAAAAFc4eW4ct8_Gi&user=%7B%22id%22%3A755232383%2C%22first_name%22%3A%22John%22%2C%22last_name%22%3A%22Doe%22%2C%22username%22%3A%22john_doe%22%2C%22language_code%22%3A%22en%22%2C%22is_premium%22%3Afalse%2C%22allows_write_to_pm%22%3Atrue%7D&auth_date=1704988707&hash=8c1357ce9f4e8825168cggfgc7cee9bef3a06827f47743e463de79f636949621

When I examine the content of the $_SERVER variable, I don’t find this information listed. Is there a method to retrieve this data with PHP? Should I consider altering my .htaccess file, or is it necessary to utilize JavaScript to first extract the fragment information?

URL fragments never reach PHP - they’re client-side only. I ran into this same issue building Telegram mini apps. You’ll need to grab the fragment with JavaScript using window.location.hash, then send it to your PHP backend with a POST or fetch call. Capture the fragment when the page loads, parse out the parameters, and immediately fire them off to a PHP endpoint that validates the Telegram data. Check the hash parameter against your bot token to prevent data tampering. Once validated, store it in session variables to avoid repeated JavaScript calls.

totally! you’re right, the stuff after # isn’t sent to PHP directly. you’ll need to use JS to get it using window.location.hash, then send it to your server with AJAX or a form. that’s the way to go for Telegram web apps!