首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在未通过身份验证的情况下将用户转发回去

问如何在未通过身份验证的情况下将用户转发回去
EN

Stack Overflow用户
提问于 2019-06-14 00:02:18
回答 1查看 43关注 0票数 1

我需要在几个页面上设置身份验证,并在未授权的情况下转发用户回来。目前,它会显示包含错误的目标页面。

代码语言:javascript
复制
Unauthorized
This server could not verify that you are authorized to access the 
document requested. Either you supplied the wrong credentials (e.g., 
bad password), or your browser doesn't understand how to supply the 
credentials required.

我对基本身份验证和apache conf一无所知。我有谷歌深度,没有找到任何解决方案,请指教。

谢谢

代码语言:javascript
复制
SetEnvIf Request_URI ^/en auth=1

AuthName "Please login to access english part"
AuthType Basic
AuthUserFile "/path/to/my/.htpasswd"

# first, allow everybody
Order Allow,Deny
Satisfy any
Allow from all
Require valid-user
# then, deny only if required
Deny from env=auth
EN

回答 1

Stack Overflow用户

发布于 2019-06-15 03:36:28

这实际上是HTTP基本身份验证的一个限制。

但是,您可以自定义401响应(我假设这就是您所看到的,而不是403)。但是,您可以直接从自定义401重定向,这将导致客户端收到3xx响应,而不是401,这对用户来说不是那么详细和令人困惑。或者,你展示一条“友好的”消息,并提供一个链接,指向它们的来源地。

额外的问题是知道将用户返回到哪个页面。除非您将此信息存储在会话中,否则您将需要检查Referer HTTP请求报头,该报头可能根本没有设置。

例如..。在.htaccess文件的顶部,为401响应定义您的自定义错误文档:

代码语言:javascript
复制
ErrorDocument 401 /errordocs/e401.php

在/errordocs/e401.php中,您将拥有类似以下内容:

代码语言:javascript
复制
<?php
/**
 * 401 Unauthorized - Error Document
 */

// Get the HTTP Referer (if set at all)
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;

// Immediately redirect back to the referring page if known
// But the client then sees a 3xx response, without any error, which could be confusing for users
if (!empty($referer)) {
    header('Location: '.$referer,true,302);
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>401 Unauthorized</title>
</head>
<body>
<h1>401 Unauthorized</h1>

<p>Sorry, you do not have permission to view that resource.</p>

<p>
<?php if (empty($referer)): ?>
    <a href="/">Go back to the home page</a>
<?php else: ?>
    <a href="<?=$referer?>">Go back to <?=$referer?></a>
<?php endif; ?>
</p>

</body>
</html>

要“自动化”这一过程,您也许可以在这么多秒之后实现元刷新(或JS“重定向”)返回到引用页面(如果设置)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56584483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档