我正在尝试将die()上的用户重定向到另一个地方。例如,如果用户点击cancel,他将被重定向为tryagain.php,或者如果他输入了不正确的凭据,他将被重定向到resetpassword.php
请指导我,这样我才能做到这一点。
我怎样才能做到这一点。下面是我的代码:
<?php
$realm = 'Restricted area';
//user => password
$users = array('admin' => 'mypass', 'guest' => 'guest');
if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Digest realm="'.$realm.
'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
die('Text to send if user hits Cancel button');
}
// analyze the PHP_AUTH_DIGEST variable
if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
!isset($users[$data['username']]))
die('Wrong Credentials!');
// generate the valid response
$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
if ($data['response'] != $valid_response)
die('Wrong Credentials!');
// ok, valid username & password
echo 'You are logged in as: ' . $data['username'];
// function to parse the http auth header
function http_digest_parse($txt)
{
// protect against missing data
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
$data = array();
$keys = implode('|', array_keys($needed_parts));
preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
unset($needed_parts[$m[1]]);
}
return $needed_parts ? false : $data;
}
?>发布于 2018-08-14 17:21:19
你可以做这样的事
header("Location: http://example.com/tryagain.php");
die();发布于 2018-08-14 17:20:58
只管用
header('Location: tryagain.php');
在您的if语句中,然后调用die。
来自this answer的报价
如果不在头(‘Location:http://something')后面放置die()或put (),那么脚本可能会继续导致意外行为。例如,这可能会导致内容被公开,您实际上希望通过重定向(HTTP 301)来防止。对于最终用户来说,上述内容可能无法直接显示,因为浏览器可能不会呈现它(由于301)。最后,exit()和die()函数阻止脚本继续运行。
发布于 2018-08-14 17:18:04
有一个由PHP预定义的方法,header('Location: tryagain.php');只是用上面的语句替换die语句。你不需要使用死,因为你有条件,所以你可以很容易地重定向你的用户尝试again.php,如果他插入了错误的细节,你可以在Google上做更多的研究。
https://stackoverflow.com/questions/51846659
复制相似问题