php - 是否可以使用 PHP 进行重定向?

标签 php redirect

我正在尝试将 die() 上的用户重定向到其他位置。例如,如果用户点击取消,他将被重定向到 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;
}
?>

最佳答案

你可以做这样的事情

header("Location: http://example.com/tryagain.php");
die();

关于php - 是否可以使用 PHP 进行重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51846659/

相关文章:

javascript - 在 Moodle 插件中使用外部 JavaScript 库的函数

.htaccess - 从 https 页面重定向到 http

spring - Vue.JS 和 Spring Boot - 在 404 上重定向到主页

http - Google 搜索显示不同的网站网址

JSF 过滤器在初始重定向后不重定向

php - 停止动态下拉列表中的重复行

php - Curl 替代 Cron Job PHP

javascript - 子菜单隐藏在谷歌地图后面

redirect - 配置/etc/nginx/nginx.conf重定向到单独的主机

php - 根据条件(desc 或 asc)在 MySQL 查询后添加逗号