0

I want to redirect a page to another server using php. But sometimes the server goes down for maintenance as It's still in BETA.

So I would like to use a php script which can first run a curl function or code to receive a particular value from the server to ensure that the server is online. then redirect the page.

like

curl http://11.21.21.231/curl_connector_script.php

then recive the value as a string

if $string=1 

run the php redirect code

else

showing in my page that server is offline

Sorry but I am very very new to php.

Can anyone tell me how can I do this in a proper php code?

Yan Khan
  • 49
  • 1
  • 9
  • possible duplicate of [How can one check to see if a remote file exists using PHP?](http://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php) – andy Nov 01 '14 at 19:21

2 Answers2

0

This should go on the very top of whatever file you have

$resp = file_get_contents('http://11.21.21.231/curl_connector_script.php');
if($resp == 0) { // Not online
    header('location: offline.php');
}

This works by sending a simple get request to the page that checks the status of your website, if the response is zero, meaning not online, then he's redirected to offline.php using headers

Ali
  • 3,479
  • 4
  • 16
  • 31
  • and what code I should include the curl_connector_script.php ? – Yan Khan Nov 01 '14 at 19:25
  • It depends on how your servers gets into maintenance mode. – Ali Nov 01 '14 at 19:28
  • I just need to check if the file is available or not.. if file is available then I will run the redirect code and if not then I will not redirect the page to that server but I will show in my page that the server is offline.. – Yan Khan Nov 01 '14 at 19:30
0

if itsnt ssl, you can use $status = file_get_contents('http://server.com/curl_connector_script.php');

This script http://server.com/curl_connector_script.php can returning some flag "1"

Maxim Dvorkin
  • 91
  • 1
  • 5
  • sorry but I am a nob to php could you please explain the code further as it works on php? and what code I should include in the curl_connector_script.php ? should I just put a value there like 1? or something??? – Yan Khan Nov 01 '14 at 19:28
  • curl_connector_script.php code: if this script will return some value, when server ok – Maxim Dvorkin Nov 01 '14 at 19:32