简体   繁体   中英

Redirect if connection has timed out

in my test homepage i am connecting to server and taking some data, but if somehow server cannot respond i want to redirect user to some other page, so i have a question how can i do that?

$url = 'server address';
$contents = file_get_contents($url);
//if connection times out make redirect 

as far as i understand i can make redirection like this:

header("Location: http://www.yourwebsite.com/page");
exit();

but how to make redirect only if connection times out?

This will test if the file was loaded. If it was not loaded it will return boolean false, but can also return a non-boolean value which evaluates to false. But you should take care when using an if statement to check this. More info here.

http://php.net/function.file-get-contents

if(!$contents){
  header("location:http://www.yourwebsite.com/page");
  exit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM