Jump to content

Recommended Posts

Hi, please please help with this PHP code. Essentially, this PHP page will be stored on Server1. It will function as a redirect to another server. ServerA is the Primary, while ServerB is the secondary. If the Primary fails, the PHP page should redirect the access to ServerB url.

 

I tried this and the redirection works only for the Primary. I also tried several attempts to put an invalid host for the Primary, but it seems like the ELSE condition does not work. Please help me how to fix this. Thank you very much.

 

 

<?php

$Pri = "http://www.yahoo.com";

$Sec = "http://www.google.com";

 

//check for port number, default is 80

$link = $_GET['link'].":";

$s_link = str_replace("::", ":", $link);

list($addr,$port)= explode (':',"$s_link");

if (empty($port)){

$port = 80;

}

//Test the server connection

$churl = @fsockopen(server($addr), $port, $errno, $errstr, 20);

if (!$churl){

//echo $errstr;

header("Location: $Pri");

}

else {

header("Location: $Sec");

}

function server($addr){

if(strstr($addr,"/")){$addr = substr($addr, 0, strpos($addr, "/"));}

return $addr;

}

?>

Link to comment
https://forums.phpfreaks.com/topic/184342-php-redirection/
Share on other sites

PHP has to know about the function before it can call it.

 

Ummm no it doesn't. You can define a function anywhere on the page and PHP can call that function just fine anywhere on the page. Functions are put into memory before the script is ran.

Link to comment
https://forums.phpfreaks.com/topic/184342-php-redirection/#findComment-973552
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.