Redirect in PHP without use of header method

家住魔仙堡 提交于 2020-01-10 01:03:28

问题


This contact.php form was working to handle a submit and then redirect to a new page and then all of a sudden just stopped working. I have tried adding error handling and also moving header to the top in front of all other, but neither works. The form is still submitting the data as expected, it's just the redirect that doesn't work. Any ideas would be appreciated.

<?php 
include 'config.php';
$post = (!empty($_POST)) ? true : false;
if($post)
{
    $email = trim($_POST['email']);
    $subject = "Downloaded Course Units";
    $error = '';
    if(!$error)
    {
        $mail = mail(WEBMASTER_EMAIL, $subject, $message, 
        "From: ".$email."\r\n"
        ."Reply-To: ".$email."\r\n"
        ."X-Mailer: PHP/" . phpversion());
        if($mail)
        {
            echo 'OK';
            header('location: http://www.google.com.au/');
            exit();
        }
    }
}
?>

回答1:


Use javascript.

Instead of

header('location: http://www.google.com.au/');

Use,

?>
<script type="text/javascript">
window.location.href = 'http://www.google.com.au/';
</script>
<?php

It will redirect even if something is output on your browser.

But, one precaution is to be made: Javascript redirection will redirect your page even if there is something printed on the page.

Make sure that it does not skip any logic written in PHP.




回答2:


Replace the header('location: http://www.google.com.au/'); line with the below code to redirect in php without using header function.

$URL="http://yourwebsite.com/";
echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';

If you're wondering that why I have used both Meta tag and JavaScript to Redirect, then the answer is very simple.

If JavaScript is Disabled in the Browser, then meta tag will redirect the page.




回答3:


header not working after include, echo. try again without include, echo. OR instead of function header use

echo '<meta http-equiv="refresh" content="0; URL=http://www.google.com.au/">';



回答4:


I solved this with:

function GoToNow ($url){
    echo '<script language="javascript">window.location.href ="'.$url.'"</script>';
}

Use : GoToNow ('http://example.com/url-&error=value');




回答5:


If you want to redirect to another page after html code then use location.href javascript method.

Refer to this sample code:

<html>
    <head>
        <title> Using the href property of the Location object</title>
    </head>
    <body>
        <script language="JavaScript">
        <!--
            function show(){
                 document.location.href ="http://www.java2s.com";
            }
        -->
        </script>
        <form name="form1">
            <br>
            <input type="button" name="sethref" value="Set href" onClick='show()'>
            <br>
        </form>
    </body>
    </html>


来源:https://stackoverflow.com/questions/27123470/redirect-in-php-without-use-of-header-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!