简体   繁体   English

PHP运行脚本,然后在完成后重定向

[英]PHP run script then redirect once complete

in PHP how can I redirect my page once scrpit has run with a successful case 0 but stay on current page for anything else? 在PHP中,如何在scrpit成功运行0的情况下运行我的页面,但是在其他页面上停留在当前页面上呢?

exec( $command, $output = array(), $worked );

switch( $worked ) {

    case 0:  
        echo "Database <b>". $database. "</b> successfully exported to <b>". $filename. '</b>';
      // add redirect here
        break;

    case 1:
        echo 'There was a warning during the export of <b>'. $database .'</b> to <b>'. $filename .'</b>';
        break;

    case 2:

        echo 'There was an error during import.'
            . 'Please make sure the import file is saved in the same folder as this script and check your values:'
            . '<br/><br/><table>'
            . '<tr><td>MySQL Database Name:</td><td><b>'. $database .'</b></td></tr>'
            . '<tr><td>MySQL User Name:</td><td><b>'. $user .'</b></td></tr>'
            . '<tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr>'
            . '<tr><td>MySQL Host Name:</td><td><b>'. $host .'</b></td></tr>'
            . '<tr><td>MySQL Import Filename:</td><td><b>'. $filename .'</b></td>'
            . '</tr></table>'
        ;
        break;
}

If you don't mind removing your "echo" before the redirect you can redirect with header 如果您不介意在重定向之前删除“ echo”,则可以使用标头重定向

 header("Location: http://you-url");

If not you can use javascript 如果没有,你可以使用JavaScript

<script> window.location = "http://your-url"; </script>

And you could add something to delay the redirect or put your "successfully exported" message on an alert before the redirect, otherwise the user might not see the message if the redirect works too fast. 而且,您可以添加一些内容来延迟重定向,或者在重定向之前将“成功导出”消息放置在警报中,否则,如果重定向工作太快,用户可能看不到该消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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