【问题标题】:Accessing PHP cookie from another php script从另一个 php 脚本访问 PHP cookie
【发布时间】:2016-04-15 11:20:58
【问题描述】:

我在从另一个 php 脚本访问 cookie 变量时遇到问题。这是代码sn-p

if (isset($remember) && $remember == 'on') {

    setcookie("username", $user, time() + (60 * 60 * 24 * 30));
    setcookie("password", $pass, time() + (60 * 60 * 24 * 30));  
}

如何从外部脚本访问 cookie 内容?谢谢

【问题讨论】:

  • 不要在 cookie 中传递密码 非常不安全。你只是在把钥匙给这个用户!
  • 查看$_COOKIE 数组。您的所有 cookie 都会传递给您的所有脚本。假设他们没有超时

标签: php cookies


【解决方案1】:

当您从外部脚本发送 HTTP 请求时,setcookie() 方法将在 HTTP 响应标头中附加一个名为 Set-Cookie 的新标头。

Set-Cookie: username=myUserName
Set-Cookie: password=myUserPass

要读取这些 cookie(实际上我们只需要从响应中解析 HTTP 标头),请使用以下内容:

file_get_contents("http://localhost:8080/test.php");

$receivedCookies = array();
$headerCount = count($http_response_header);

for($i = 0; $i < $headerCount; $i++){
    if(strpos($http_response_header[$i], "Set-Cookie") !== false){
        $receivedCookies[] = $http_response_header[$i];
    }
}

var_dump($receivedCookies);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多