简体   繁体   English

只能由登录的Joomla用户访问的PDF文件

[英]PDFs files that can only be accessed by logged-in Joomla Users

I have been searching for hours now and it's time to ask you if there's a solution. 我现在一直在寻找几个小时,现在是时候问你是否有解决方案了。

I'm trying to make only logged-in Joomla users can see and download my PDFs in a directory. 我试图让只有登录的Joomla用户可以在目录中查看和下载我的PDF。

For this, I created a .htaccess : 为此,我创建了一个.htaccess:

# Adding PDF compatibility for the control
AddHandler php5-script .pdf
php_value auto_prepend_file "prepend.php"

# Check wether or not user is logged-in
php_value include_path "./journal/"
php_value auto_prepend_file "download.php"

I also created a file called "download.php" to check wether or not the Joomla user is loggged-in : 我还创建了一个名为“download.php”的文件来检查Joomla用户是否已登录:

<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', '../');
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );

/* Create the Application */
$app = JFactory::getApplication('site');

$user = &JFactory::getUser();
if ($user->guest) die();
else {

}
?>

And the last file : prepend.php 最后一个文件:prepend.php

<?php

header('Content-type: application/pdf');
readfile($_SERVER['SCRIPT_FILENAME']); // serve the requested file
exit(0);

?>

Problem is, when I try to access to one of the pdf, I get the plain text version of it. 问题是,当我尝试访问其中一个pdf时,我得到它的纯文本版本。 Here's the first lines... And it goes long after. 这是第一行...而且很久以后。

%PDF-1.6 %âãÏÓ 1983 0 obj <>stream hÞì[MoGý+uLÖYŸ‚Fc^†e ÇXŒ¥Ž<°4­ùß/ÉjN„8Y U ìE>oº«Y$«ºÛôà¼ôè DÁ%O2—ß*…¯d¾R}rÙWÙ¡ÏYPq ת ƒ bT#ð.d,‚€Q@JAÁ…*v+DFUg$( 3d½ZcG¢Ï^Pu‚Πѳ¿=£*6\Il ºAƱC1faÆèb z7¹˜A<@¶‘«øÌNÆ’± Y #¶Q•ÃIùØñHÊÇn$¯+±"|!º(^…ĨˆW!;‹x8 ¨ÖBåбÁ¥PÅFô.Å(6"0"±Á?SŠb#J¼Øˆl#'±ÙFAa‰l£Žcµˆ5ž–¨ñ§ dFò’@™‘ÀeH2#¡ËÍ?^~š¦Ï¡èN9PPv9ézSaD:£ºœ³Î`%ÈNTnñËl£Å‡ä¿Ì6Zürt¥Å–?.©Òâ—#_®®4ÿ¸KðrX‚VO+1*BWˆ]®×’´"x %«\Ž\Çz—mHé1bU«¸° Ç6¨ˆ§Õ;.Nñ´#nEÇ*žr¹ÊôZdTõZrœwaæsî„ÝàX‹µ2"GŽÍÊ]"{&×ø[”•Û3b¤s‰mPf.òºJÊŒKq\ñ™$På9B©òÞQÊáyŒ$«ÄIæÒ(‚‚£ÄCEFâ=±Ì)K™Ÿ‘Ä8ìT$ƒÄ’¤Rõ.Ûtƒ8I$Ù'–Q»2Øû,†%ÚX£@)xb©ƒÇ(eAIb±ƒI§1^§‘@õ„õ> %±àŠRH–ïsÃÒƒ|‘’”’—ÞÃP¬I Š5*:€ÿây².I&ÿ‘…IæH¬IáV =‚Ô.I‚¨áeñ$¹§à¶)&d­5JRtÜY%LA¬Isb(Ö¤Š5=èXº/q`(ÝBEŒ ’h!ªµ(Í:ht¸0,:9¦:' *‚ÄSLº n€,U†I¬Fo¬Z%I¬IU3k”uÿfc² ñ4€šFÐë€"PÓ-m%I/IÆ‚ö7’B QŠ˜d§X¦IèYÉÂ+·‚tãï¾Û¼|Òݽ{+¶JÜÜÀùPÀ suÙË®¥@v-b€Á‡Í›íaÚߦ©mw|ñѵ×ÓÃñåôÅÑæí|=½ÚÞ¶ÍQ½ûr;m·ûùvžßÏÞ>ßïçãö¸›÷›óéâ¸y~8î~Ý2Ð_?|ÚûOÓtƒÇÍ»íÕa{³y±»º?Lòóz¾ztÿ§ý%3O›×ò×67‘àßïücÚ^Úî¯wûéüÓV\•¯¶‡ùç›íÕlê·†ü â …Š'”N(ŸP9¡zB¤èüzw95{ï8žç‡æÌÏóáóÇyþ¼ùq¾¸¿áˆë•ß½Óp¾¾¿¹{_ÛáB¦ßH@Óp¾”ŽŸ®§›?Vÿt×—ìQKT´îghÀÊ ¢dÀ­å¼²cFcFcFcFcFcFcFcFcFcFcÆŒ9s0æ`̦$9,À˜ƒ1cŽÆ9s4f¢J`Ìј£1GcNÆœŒ9s2ædÌɘ“1'cNÆœŒ9s6ælÌÙ˜³1gcÎÆœ9s6æbÌŘ‹1ŸZN1æbÌŘ‹1c.Æ\¹³õ99n,À˜«1Wc®Æ\¹3333333333-ÌrºX@Á@4`ؤG¾¨ŒÙ4H¦A2 ’iLƒd$Ó ™É4H¦A2 ’iLƒd$Ó ™É4H¦A2 ’iLƒd$Ó ™É4H¦A2 ’iLƒd$Ó ™É4H¦A2 ’iLƒd$Ó ™É4H¦A2 ’iLƒd$Ó ™É4H¦A2 ’iš?<îÒo®ä´Vulú‹ŽíuÞ²ÿqÞ×sž:üS‡êðOþ©Ãÿ:|;bÿ½†­s°cNè˜ÿöœç×ÇoÎåAå,„o™!ý†åyî¯hŽü<¶û¸uÓ³»ãýån>‹) eî§\L‡ùÁ]^Ü–v”Í]ðöz>§ƒÒ7AR‘ç&ø(YÔ’UÖ!kA¯Ý™ï)ØÒs,,=Ç‚G(ðº–îGÓÒ÷eº8l÷ÿ¾ÛÞÜÝï¯ÔƒÔíÁÈ2jÕ–B{¸›¿MÏ~=Ìû£r l»ÿÚM{wùe¿½Ùýç~:C=Z0h˨~-BeƒÕ܃¤ŽÚ½1מ¹öl̵gc®=síÙ˜kÏÆ\{úùõ¡×“3uoÊ„CÖõœLݽâPçmÖ»ûõTõTu½y'1µ—ÂÃÏÛ;\?üÔ€¾½ÅõÝí |Ùª–÷äq<$ИRÿBzj‡tͪ]³zš€ïš]³úz×÷ÌöBÂz»pn…q•Ó7š‡ýÕyH!,ˆÊz4XBUWüè„Ðz_ÿG @¿JÀ°e×;á!¶€!®HÙº†)Û7+Œ+R¶âô"e«xä~5_N7gKK½Õ®zѧž=<€ûå›^üòmó£¿îÃð["lH ߉¡3éò5<¿$Çö Äq¦V»!3µ’ yœiù¤Þ_´¡®Ò$ãR0´âcäø \Ό㟀pùŒû¿1¬ïVBqÅÞ¼<"Ä{s{¯#ß„¾¢¤F9üyS+ÙXûsÙuºNCï Ú¿Ž‚ñÏ?˜Z õ‚´N-§V%iÅZNµQvŸ¶?|ý¿gÀ=ö¿*ôo, endstream endobj 1984 0 obj <>stream hÞŒË1 €0ЫäéoSmAÜÜ‹õ"ÅEADoov:äÿ^`!8´ä"Aœ§6hKмˆF£t‰žºŽÁèÛșӼðô…óuÞË5leïû?f똫cRÇ|k¾ì`TÔN> endstream endobj 1985 0 obj <>stream hÞŒ“KOƒ@€ç§ÌQÊ.°<’¦IcÓ“QR=øˆ´›º ²»4õ'û'ÔÙbÒ‡¹,³0û}Ìpž¤Èó”a»+G.„|ôƒÄAê‚Ã6(ÂÐFí©c±Å$˜ˆmNŠ©»ã3Z¼™Òƶ.†sï:w;—àv÷µôn[¨Ršñ˜Ò¯ª¦´xÁ™7•Æ>—MQxO”„ÙËŽÇ;xY®eéžn$¾²…<[¹1U‰Zš¼Ù\®Íâ|ë:a¸‘Ûöâù[@‹œ*cµzm¬"îd±Rå dЉ̴\ï*êPBM/od·DÐlö$a¯Ä?%1R¯e£Í ‡èu{Ž¯o˜Ã'CXÀ ”ƒDQ¯(<e IUÓ*Iè”|Ð`â^ø_ãÒ$½šèPsoðN¢5)®(ª¡ Ôº´WïÉfU£KEß×áø9‹Û¬ŽZþüqGødŸåK‰ioƒNaÒcgÄù`D„N

Any help will highly be appreciated! 任何帮助将非常感谢!

Try with this in your PHP code: 在PHP代码中尝试使用它:

<?php
header('Content-disposition: attachment; filename=filename.pdf');
header('Content-type: application/pdf');
readfile($_SERVER['SCRIPT_FILENAME']); // serve the requested file
exit(0);

You can also force it using .htaccess : 你也可以使用.htaccess强制它:

<Files *.pdf>
    Header set Content-Disposition attachment
</Files>

You need additional headers in your response - see https://stackoverflow.com/a/2882523/823549 for the full list. 您的响应中需要其他标头 - 有关完整列表,请参阅https://stackoverflow.com/a/2882523/823549

Specifically, Content-Disposition will force the save dialog - http://php.net/manual/en/function.header.php 具体来说, Content-Disposition将强制保存对话框 - http://php.net/manual/en/function.header.php

Ok so I finally got this working. 好的,所以我终于搞定了。 As Marc B also mentioned, my headers was ignored. 正如Marc B也提到的,我的标题被忽略了。 I guess the double php_value auto_prepend_file didn't work well and the first "prepend.php" was ignored. 我想双php_value auto_prepend_file不能很好地工作,第一个“prepend.php”被忽略了。 So I just copy pasted the code and kept everything in one file with the header Conten-Disposition added. 所以我只是复制粘贴代码并将所有内容保存在一个文件中,并添加了标题Conten-Disposition

Here's the working code : 这是工作代码:

.htaccess 的.htaccess

# Adding PDF compatibility to check 
AddHandler php5-script .pdf

#Path of the directory where are stored the PDFs
php_value include_path "./journal/"

#Check wether or not user is logged-in & show pdf if logged-in
php_value auto_prepend_file "prepend.php"

prepend.php prepend.php

<?php

/* Loading Joomla User core Files */
define( '_JEXEC', 1 );
define('JPATH_BASE', '../');
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );

/* Create the Application */
$app = JFactory::getApplication('site');

/* Check wether Joomla User is logged-in */
$user = &JFactory::getUser();
if ($user->guest) die();
else {

}

/* Sending back pdf */
header("Content-Disposition", "inline; filename=myfilename.myextension");
header('Content-type: application/pdf');
readfile($_SERVER['SCRIPT_FILENAME']); // serve the requested file
exit(0);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM