Implementing failover with a PHP file + Cloudflare!

Due to unforeseen circumstances, the server experienced a failure.

The timing is unpredictable, as I may be sleeping or online and able to address it promptly.

So I created this simple tool, provided that the domain is hosted on Cloudflare.

File: Click here to download

Specific tutorial:#

  1. Log in to Cloudflare first, click on a domain. Find the Zone ID and copy it to the PHP file.

    //Zone ID
    $id = '';

  2. Below the Zone ID, there is a Get your API token option. Click on it.

Click on Create Token, then use the Edit Zone DNS template.

Select your domain in the Zone Resources row, and you can add the client IP address filter to the whitelist.

After creating it, copy it to the PHP file.

//API token created, write after Bearer
$Authorization = 'Authorization: Bearer ';

ps. There should be a space between Bearer and your token.

  1. After adding the two pieces of information, go to line 25.

    //Get the list of DNS record IDs, changing it to true will display the JSON format of the DNS record list when accessing the PHP URL.
    $get_dnsid = false;

Change false to true, and then upload it to the server and access it.

Some JSON will appear, select the ID of the record you currently need to perform failover on.

Fill in:

//DNS record ID to be modified, $get_dnsid = true to get the record list, set dnsid and then set $get_dnsid to false
$dnsid = '';

Then change $get_dnsid = true; to $get_dnsid = false;!

  1. Modify other parameters

    //IP to ping

Change it to the address you want to ping.

$dnstype = 'A';
$dnsname = '';
//Modified IP
$dnscontent = '';
(bool)$dnsproxied = true;
$dnsttl = 3600;

The above parameters are for modifying the IP of the record after a failure occurs, with the default being an A record.

The second one is the content of the record, the third is the IP of the backup server, the fourth is set to false to disable Cloudflare's CDN function.

By default, it is enabled. The fifth one is the TTL time, with the default being one hour. To set it to automatic, you can change the value to 1.

  1. Ping address port

    if (pingDomain($pingip, 80) === -1) {

This is from line 43, where the default port is 80, but you can change it to another port.

After setting up everything above, just set the monitoring access URL.

Friendly reminder, the way this file pings is different from how you ping in your computer's terminal.

This method is similar to port scanning, where setting the port to 80 or 443 can monitor the web.

Setting the port to 3306 can monitor MySQL, but the port must be open for the IP where the PHP file is deployed.

For example, if you allow port 7777 in your control panel, but there is no process occupying that port, it will also be considered a failure.

Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.