如何在PHP中重定向/发送? [重复]

问题描述:

This question already has an answer here:

I am working on login functionality.

So what I want to do is, on form submission from login.php if details are correct it should go to home.php else it should go back to login.php.

My login processing is on process.php. But I don't know how to redirect/dispatch to appropriate page from process.php.

</div>

此问题已经存在 这里有一个答案: p>

  • 如何在PHP中进行重定向? 31 answers span> li> ul> div>

    我正在处理登录功能。 p>

    所以我想做的是,在 login.php code>的表单提交中,如果细节正确则应该转到 home.php code >否则它应该返回 login.php code>。 p>

    我的登录处理在 process.php code>上。 但我不知道如何从 process.php code> 重定向/调度 code>到适当的页面。 p> div>

Use header function like this:

header('Location: login.php');
exit;

But don't print any html output before calling header function else it will result in an error.

header('Location: http://someweb.com'); will redirect the user.

Try something like this :

<?php
session_start();
//do some login processing

if(login === true){
    exit(header('Location: home.php'));
}else{
    //Extra marks set a reason why failed
    $_SESSION['error'] = 'Some error about why it failed';
    exit(header('Location: login.php'));
}
?>

exit(header('Location: *')); is what your after.