Skip to content

Instantly share code, notes, and snippets.

@Polda18
Created June 11, 2026 16:40
Show Gist options
  • Select an option

  • Save Polda18/94f5bddfc2d1bf568821699530dcc704 to your computer and use it in GitHub Desktop.

Select an option

Save Polda18/94f5bddfc2d1bf568821699530dcc704 to your computer and use it in GitHub Desktop.
PHP code for detecting a correct Discord server from a custom invite link, with a fallback to a default invite link
<?php
/**************************************************************
* PHP Script for Discord server checking
* Author: Marek Poláček
* License: MIT License
* File: discord.php
* Description: Redirects to the Discord server invite link.
**************************************************************
* How to use:
* 1. Get your server IDs and place them inside the
* $discordServer variable
* 2. Get your custom invite codes (or prepare some beforehand)
* and place them in the $customInviteCode variable
* 3. Get your fallback invite codes (your default ones)
* and place them in the $fallbackInviteCode variable
* 4. Use the comments as a guide to how to set things up.
* 5. Upload the file to your PHP server, optionally use
* .htaccess for a nice looking URL rewrite for each
* Discord server you own.
**************************************************************/
// Check if the 'server' query parameter is set to determine which Discord server to redirect to
$devMode = isset($_GET['server']) && $_GET['server'] === 'dev'; // You can remove that or customize and/or add more checks
// for your other Discord servers. Anything else will just
// lead to your main Discord server.
// Set the Discord server ID based on the server type
// Customize that according to your checks
// To copy the server ID, you need to enable Development Mode in your Discord account.
// Then right click the server you want to copy the ID of, and select Copy Server ID (at the bottom of the menu).
if ($devMode) {
$discordServer = 'DEV-DISCORD-ID'; // Development server ID => Replace with your actual server ID
} else {
$discordServer = 'MAIN-DISCORD-ID'; // Main server ID => Replace with your actual server ID
}
// Custom invite code for the Discord server (optional)
$customInviteCode = ($devMode) ? 'devserver' : 'mainserver'; // Replace with your custom invite codes if you have them set up,
// otherwise these will be ignored and the fallback invite codes
// will be used instead
// Fallback to the default invite link if the custom invite code is not set
$fallbackInviteCode = ($devMode) ? 'devinvite' : 'maininvite'; // Use previously generated codes as fallback => Replace with actual
// fallback invite codes (will look something like this: "aXTd45p")
// DO NOT DELETE YOUR FALLBACK INVITE CODES!!! If you do delete them,
// you'll be left with a disfunctional invite link from your website.
// It's best practice to leave your default invite codes existing
// To make sure you have a safe fallback in case your custom invite
// code stops working for the reason your server dips below Level 3
// Nitro boosts, in which case your server invite will cease to exist
// and is vulnerable for custom invite code sniping (could be used to
// guide your users to a malicious Discord server impersonating you).
// Construct the invite URL using the custom invite code and perform a quick check to ensure it's valid and points to the correct server
$inviteUrl = "https://discord.gg/{$customInviteCode}";
$headers = @get_headers($inviteUrl);
if ($headers && strpos($headers[0], '200') !== false) {
// If the custom invite code is valid, check if it points to the correct server by fetching the invite details
$inviteDetailsUrl = "https://discord.com/api/v10/invites/{$customInviteCode}?with_counts=true";
$inviteDetails = @file_get_contents($inviteDetailsUrl);
if ($inviteDetails) {
$inviteData = json_decode($inviteDetails, true);
if (isset($inviteData['guild']) && $inviteData['guild']['id'] === $discordServer) {
// If the invite points to the correct server, use it
header("Location: {$inviteUrl}");
exit;
}
}
}
// If the custom invite code is not valid or does not point to the correct server, redirect to the fallback invite link
// MAKE SURE THESE INVITE CODES ARE EXISTING AND POINT TO YOUR SERVER, YOU DON'T WANT TO EXPOSE YOUR USERS TO A MALICIOUS DISCORD SERVER
// OR PROVIDE AN INVALID INVITE LINK
$fallbackInviteUrl = "https://discord.gg/{$fallbackInviteCode}";
header("Location: {$fallbackInviteUrl}");
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment