[PHP] Server Check and Failover Code


 
Thread Tools Search this Thread
Top Forums Web Development [PHP] Server Check and Failover Code
# 1  
Old 12-06-2009
[PHP] Server Check and Failover Code

Here is some sample PHP code you can run if you have a PHP web application that uses code or images from an ad server, image server, or content deliver network, and you want to check if it is working and if not, failover to another one:

PHP Code:
<?php

$current_server 
"server.domain.com";

// set frequency based on your server traffic
$freq 100;

if(
rand(1,$freq) == 1){

        
// create curl resource
        
$ch curl_init();

        
// set url
        
curl_setopt($chCURLOPT_URL"server.domain.com/heartbeat.html");

        
// don't get header
        
curl_setopt($chCURLOPT_HEADER0);

        
// 2 second connection timeout
        
curl_setopt($chCURLOPT_CONNECTTIMEOUT2);

        
// store results as the return of curl_exec
        
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);

        
// 2 second curl timeout
        
curl_setopt($chCURLOPT_TIMEOUT2);

        
// if HTML error 400 over over, fail
        
curl_setopt($chCURLOPT_FAILONERRORTRUE);

        
$subject curl_exec($ch);
        
$pattern '/YOUR_PATTERN_HERE/';
        
$errno preg_match($pattern$subject$matchesPREG_OFFSET_CAPTURE);
        if(
$errno == 0$current_server  "backup.domain.com";
        
curl_close($ch);
}
?>
I chose a random number to sample the server but you could change the code and put a timer or counter if you like that better..
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test Server - Forum Code Changes for PHP 5.3.10 to PHP 7

Here is some docs of my ongoing work to port this forum PHP code which is running on 5.3.10, to PHP 7. Motivation: Unfortunately, every thing that has a beginning must have an end. PHP 5.6 active support ended January 19, 2017. It will receive security support until December 31, 2018. #1 ... (7 Replies)
Discussion started by: Neo
7 Replies

2. Red Hat

DNS server loadbalancing and failover

Hi Friends, I am setting up my new public DNS server using bind 9 on RHEL5.5 64bit. I also want to have an another DNS failover server on one of my another datacenter. My question is, if i installed an new slave DNS server on the second datacenter and configure the zone transfer, do it will be... (2 Replies)
Discussion started by: arumon
2 Replies

3. HP-UX

HP-UX: LVM migration from legacy to agile addressing onto a failover server

Hi there! I'm having problems migrating and vgimport onto a different server. Original server uses the legacy DSF naming (ctd naming like /dev/dsk/c0t1d0) and the new server where i'm migrating those luns to is using the Agile addressing (i.e persistent DSF i.e. /dev/disk/diskxxx) Importing a... (1 Reply)
Discussion started by: ilan
1 Replies

4. High Performance Computing

Veritas Cluster Server Management Console IP Failover

I have just completed a first RTFM of "Veritas Cluster Server Management Console Implementation Guide" 5.1, with a view to assessing it to possibly make our working lives easier. Unfortunately, at my organisation, getting a test installation would be worse than pulling teeth, so I can't just go... (2 Replies)
Discussion started by: Beast Of Bodmin
2 Replies

5. UNIX for Advanced & Expert Users

LAMP Server Failover

How does everyone else handle this? My Setup: Server A: CentOS 5.x 10.0.0.1 Apache MySQL Master Server B: CentOS 5.x 10.0.0.2 Apache MySQL Slave My Domains: dom1.com A record: 10.0.0.1 A Record: 10.0.0.2 dom2.com A record: 10.0.0.1 A Record: 10.0.0.2 (3 Replies)
Discussion started by: Ikon
3 Replies
Login or Register to Ask a Question