403错误背后的常见原因
Posted
技术标签:
【中文标题】403错误背后的常见原因【英文标题】:Common reasons behind 403 errors 【发布时间】:2013-09-09 07:01:49 【问题描述】:以下是我认为正确编写的两个函数。
问题是,有时我的 Session 没有超时,但 AJAX 请求返回 403 错误(其他一些没有任何模式的函数也会发生)。
堆栈溢出充满了寻求此问题帮助的问题,但我没有找到任何真正好的答案:
Redirection + 403 error Http 403 - Forbidden error 403 Forbidden Error getting 403 forbidden in ajax 等问题:
-
如何通过代码导致 403 错误?
如果我有多个异步 AJAX 请求同时运行,会导致 403 错误吗? (我确实会一次触发多个(最多 5 个)ajax 请求)
如果我想以 relative_path/action 而不是 relative_pat/action.php 的形式调用 AJAX 请求,是否必须在 .htaccess 中设置目录列表?
403 可能是由我的 Session 到期引起的,对吧?
AJAX:
var root = "/test_tool";
function isLoggedIn()
// return if the user is in the sign in window
if ( window.location == "http://localhost" + root +"/" )
return;
var output = "";
$.ajax (
url: root + "/users/isLoggedIn",
context: document.body,
async: true
).done( function( result )
output = result;
if ( output == "" )
alert( " You have been logged out. " );
window.location = "http://localhost" + root +"/";
);
(蛋糕)PHP:
public function isLoggedIn()
$this->autoRender = false;
return ( $this->Auth->user('username') != null );
【问题讨论】:
【参考方案1】:我知道这个问题有点老了,但我遇到了同样的问题。 在我的情况下,问题是由 session_regenerate_id 引起的,所以为了避免它,我在我的 app/Config/core.php 中使用了以下代码:
Configure::write('Session', array(
'defaults' => 'php',
'timeout' => 480, // The session will timeout after 8 hours of inactivity
'cookieTimeout' => 480, // The session cookie will live for at most 8 hours, this does not effect session timeouts
'checkAgent' => false,
'autoRegenerate' => false, // causes the session expiration time to reset on each page load, but also causes 403 errors on ajax requests, therefore disabled
));
我只是将“autoRegenerate”参数设置为 false。不幸的是,您必须避免使用其他技术进行会话固定,look here 许多其他人也报告了这个问题(只是谷歌'ajax session_regenerate_id'),但我还没有找到解决方案。
【讨论】:
我也遇到了同样的情况。我正在调用 $this->response->file($path);在循环中填充图像库。这导致了 403 错误。一旦我设置 'autoRegenerate' => false 问题就消失了。现在我需要弄清楚如何让会话仅在不活动时终止......!【参考方案2】:1.通过代码可以得到403。请查看 CakePHP 文档 (http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#handling-unauthenticated-requests):
如果验证器返回 null,AuthComponent 会将用户重定向到登录操作。如果它是一个 ajax 请求并且指定了 AuthComponent::$ajaxLogin ,则呈现该元素,否则返回 403 http 状态代码。
2. 多次 Ajax 调用不应成为 403 错误的原因。
3. 标准路由由 CakePHP 自己处理。如果你需要一些不同的路由,你应该在 routes.php 中配置它。我想说使用 .htaccess 只是为了满足极端的路由需求,应该是最后的手段。
4.是的,这可能是一个原因,因为您将不再登录,因此获得 Auth 403s(请参阅答案 #1)。
【讨论】:
以上是关于403错误背后的常见原因的主要内容,如果未能解决你的问题,请参考以下文章
Apache服务器 403 Forbidden的几种错误原因小结!
nginx “403 Forbidden” 错误的原因及解决办法
部署IIS 常见问题 HTTP 错误 403.14 - Forbidden HTTP 错误 500.19 - Internal Server Error HTTP 错误 500.21 - Intern