php - 通过 CURL 登录 htpasswd 并将 session 保留在浏览器中

标签 php .htaccess curl session-cookies .htpasswd

我有几个文件夹(每个客户一个),我在其中保存了只有他自己才能访问的图像。

这些文件夹通过 htpasswd 进行保护,我尝试在用户正常登录后通过 cUrl 登录,并保持此 session 处于事件状态,并允许用户仅导航和访问他的文件夹图像。

在我的身份验证 Controller 中,我正在这样做:

$username = 'username';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'routeToOneFileInHisFolder');
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);    
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

我还尝试为 COOKIEJAR 和 COOKIEFILE 设置curl_setopt,但没有不同的结果。我不知道我是否采用了错误的方法或者是否缺少一些代码。

最佳答案

您可以使用$_SESSION,即...

//put this in the top of your php
session_start();

//then store the user and password in the session...
$_SESSION['username'] = 'username';
$_SESSION['password'] = 'password';

// replace in your code anywhere the $username and $password variables 
// with the new $_SESSION['username'] and $_SESSION['password']

关于php - 通过 CURL 登录 htpasswd 并将 session 保留在浏览器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48562669/

相关文章:

.htaccess - 配置文件系统的重写规则

json - 获取PayPal沙箱环境下的用户信息

curl - Rustup 说 libcurl 中不支持或禁用协议(protocol) "https"

php - 从 WooCommerce 中的 SKU 获取产品的 Url

php - Assetic(与 Symfony 2)在运行 'assetic:dump' 时更改图像文件的名称

PHP sleep 强制慢查询

php - Foreach 仅返回数组的第一个值

regex - .htaccess 重写问题

php - 当项目位于子目录中时,.htaccess 文件无法正确重写 URL

curl - 无法使用 curl 检索网址