简体   繁体   中英

Include file and if doesn't load redirect to another page

I know may be is silly question and I know how to redirect to another page with meta/js but is it possible to include file like this

include 'file.php';

and if after X seconds file.php doesn't load to redirect visitor to another page?

Well before loading/including file.php, you can check if file does exists or not. Check this link file_exists . In case if it does not exists then redirect to your desired page. Check this link header

As per your requirement below:

Is it possible to put here some timer or something to not redirect right away?

Only thing I came to understand that you want to show something user that page is not being loaded and its about to redirect to another page. Even if you use sleep it will just delay the process and will not show anything to user and after that delay it will redirect right away.

In case if you want to show user some text or notification you can do it like this.

<?php
if(!file_exists($file)){
    include('Notification_File.php');
}else{
    include($file);
    // include code regarding to this page if $file is existed
}
?>

And in your Notification_File.php file you can do something like this.

<div>
    Unable to load regarding file.
</div>

<script>
    setTimeout(redirectPage, 5000);
    function redirectPage() {
        window.location = "http://www.yoururl.com";
    }
</script>

You could look at the return value of the include

if ((include 'file.php') == false) {
    header('Location: ' . $url);
}

You can do this

if(!file_exists($file)){
    // redirect here
}else{
    include($file);
}
$filename = '/path/to/file.php';

if (file_exists($filename)) {
    include $filename;
} else {
    header( "refresh:5;url=your_url.php" );
}

or you can use javascript for redirection

setTimeout(function () {
   window.location.href = 'http://your_site.com/your_url.php';
},5000);

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