Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用文件获取内容时的会话身份验证_Php_File Get Contents - Fatal编程技术网

在PHP中使用文件获取内容时的会话身份验证

在PHP中使用文件获取内容时的会话身份验证,php,file-get-contents,Php,File Get Contents,我的项目中的下载受PHP下载脚本和会话身份验证的保护 在生成TCPDF时,我使用文件获取内容和下面的脚本获取图像并生成pdf stream\u context\u create发送头PHPSESSID,但仍然没有身份验证 pdfexport.php: $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: de\r\n" .

我的项目中的下载受PHP下载脚本和会话身份验证的保护

在生成TCPDF时,我使用文件获取内容和下面的脚本获取图像并生成pdf

stream\u context\u create发送头PHPSESSID,但仍然没有身份验证

pdfexport.php:

    $opts = array( 'http'=>array( 'method'=>"GET",
                  'header'=>"Accept-language: de\r\n" .
                  "Cookie: ".session_name()."=".session_id()."\r\n" ) );
    $context = stream_context_create($opts);
    session_write_close();  

foreach($data['we_files'] as $we_file){ 

    $getimage1 = file_get_contents( URLROOT . "/file.php?path=" .$we_file->image, false, $context);
    $image1_name = tempnam("/tmp", $we_file->image);
    file_put_contents($image1_name, $getimage1);
    $image1_image = new Imagick($image1_name);
    $image1_image->setImageCompression(imagick::COMPRESSION_JPEG);
    $image1_image->setImageCompressionQuality(100);
    $image1_image->thumbnailImage(500, 0);
    $image1 = '@'.base64_encode($image1_image);

echo $image1;
} 
file.php

$path = $_GET["path"];
$search = 'uploads' ;
$pathnew = str_replace($search, '', $path) ;

header('X-Accel-Redirect: /uploads/' . $pathnew);
header('Content-Type:');
Imagick错误:

Fatal error: Uncaught ImagickException: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509
调试:

Warning: file_get_contents(https://domain.de/file.php?path=uploads/481/8979fc24e116c4577a44424a8814c79b0d5c73d9-19-03-2019-08-28-11-SA-150.jpg): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in /var/www/clients/...

// DIE(print_r($opts));
Array
(
    [http] => Array
        (
            [method] => GET
            [header] => Accept-language: de
            Cookie: PHPSESSID=5krl856uibhugaf6p6n6hluufq

        )

)
1

//DIE(print_r($_COOKIE));
    Array(
     [PHPSESSID] => 5krl856uibhugaf6p6n6hluufq
    )
    1

您实际上是在试图欺骗用户会话:当您实际上是(潜在恶意的)第三方时,执行一个假装您是用户的操作。如果会话设置安全,则无法正常工作

您应该做的是在代码中验证用户的访问权限,并通过文件系统读取图像


另一种方法是创建一个更复杂的系统,在该系统中,服务通过后端进行身份验证,并传入这样的信息:“此用户已授权我为他们执行此操作”

您是否尝试通过网络访问本地文件?为什么不使用文件系统呢?如果您真的需要通过http服务器,发布生成401错误“我的项目中的下载受保护”的代码,这到底适用于什么?您试图在此处阅读的图像是否被视为“下载”,因此它也受此机制的保护?你不只是通过文件系统读取图像内容,为什么首先要通过HTTP进行“往返”呢?我尝试从同一个Web服务器访问文件。身份验证来自file.php->X-Accel-Redirect和nginx-config:location/uploads{root/var/www/…/web/public;internal;}上传文件夹位于public dir中,文件应防止公共访问,并且还具有基于角色会话的访问。这是正确的。也许会话管理也是解释为什么脚本在过去的旧代码中工作的原因。。。所以我将在这里使用文件系统。保护脚本仍然适用于实际用户活动。非常感谢。