【问题标题】:Ajax Post Request gets 403 Forbidden ResponseAjax 发布请求获得 403 禁止响应
【发布时间】:2013-12-28 15:50:59
【问题描述】:

我正在使用 Valums 文件上传器 (https://github.com/Valums-File-Uploader/file-uploader) 将文件上传到服务器。当我尝试上传文件时,它会 100% 上传,然后我收到 403 Forbidden 响应。

当我访问处理上传的 php 脚本以及脚本在哪里 Post 请求正常加载时,我没有收到 403 Forbidden 错误消息。

这个 403 错误的原因可能是什么,它只发生在发布请求上,而不是在通常的浏览器访问中?我在两个不同的服务器设置上尝试了该脚本,它在一个上运行起来就像一个魅力,但无法让它在另一个上运行。

将文件上传器附加到 div 的脚本:

var uploader = new qq.FileUploader({
    element: document.getElementById('file-uploader'),
    action: 'fileuploader/file_upload.php<?php if ( isset( $_GET['maxwidth'] ) ) echo "?maxwidth=" . $_GET['maxwidth']; ?>',
    params: {  },
    allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
    sizeLimit: 2097152,
    uploadButtonText: '<?php echo t('Upload image'); ?>',
    cancelButtonText: '<?php echo t('Cancel'); ?>',
    dragText: '<?php echo t('Drop files here to upload'); ?>',
    failUploadText: '<?php echo t('Upload unsuccessful'); ?>',
    onComplete: function(id, fileName, responseJSON){
        $( '#cancel-upload' ).trigger( 'click' );
        $( '#refresh' ).trigger( 'click' );
        selectFile = responseJSON['file'];
    }
}); 

file_upload.php脚本是脚本自带的稍加修改的脚本:https://github.com/Valums-File-Uploader/file-uploader/blob/master/server/php.php

这是我收到的回复:

Request URL:http://***/mod/ckeditor/plugins/scriptemimage/php/fileuploader/file_upload.php?phpsessid=vej2rc55iajknkjsp4taecsbn2&maxwidth=600&qqfile=751152_13643298+(Custom).jpg
Request Method:POST
Status Code:403 Forbidden
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:hr,en-US;q=0.8,en;q=0.6
AlexaToolbar-ALX_NS_PH:AlexaToolbar/alxg-3.2
Connection:keep-alive
Content-Length:9774
Content-Type:image/jpeg
Cookie:__utma=***; __utmc=***; __utmz=***; PHPSESSID=***
Host:www.***.***
Origin:http://www.***.***
Referer:http://***/mod/ckeditor/plugins/scriptemimage/php/browser.php?langCode=hr&maxwidth=600&run=window.opener.document.getElementById('new-article-image').value=selectedImages();window.opener.updateThumb();
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
X-File-Name:751152_13643298%20(Custom).jpg
X-Mime-Type:image/jpeg
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
phpsessid:vej2rc55iajknkjsp4taecsbn2
maxwidth:600
qqfile:751152_13643298 (Custom).jpg
Response Headersview source
Connection:close
Content-Length:399
Content-Type:text/html; charset=iso-8859-1
Date:Sat, 28 Dec 2013 18:39:20 GMT
Server:Apache

【问题讨论】:

  • 你应该在这里发布你的代码。

标签: php ajax


【解决方案1】:

这可能是您的浏览器具有在服务器上对其进行身份验证的 cookie,但发布请求没有这些。检查上传网站的浏览器 cookie。

【讨论】:

  • 网站使用 PHPSESSID cookie 进行用户登录,但上传脚本不使用身份验证。
  • “用户登录”?是否有多个用户帐户,或者您是否有用户名?如果是这样,那么 post 请求肯定需要 cookie。
  • 您必须将会话 ID 传递给您的上传脚本,以便您的上传脚本可以验证自己作为上传用户的身份。在 session_start 之前使用session_id($session_id);
  • 试过了,将 session_id() 作为 get 参数发送到 file_upload.php 并运行 session_id($GET['phpsessid']) 但它不起作用。如果此上传脚本在另一个服务器配置上运行,是否可以在服务器端禁用此安全措施?
猜你喜欢
  • 2014-01-25
  • 2019-08-10
  • 1970-01-01
  • 2016-08-17
  • 2022-01-18
  • 1970-01-01
  • 2019-08-06
  • 2017-07-29
  • 2012-10-13
相关资源
最近更新 更多