为 Steam OpenID 添加 cookie
Add cookies for Steam OpenID
我正在使用 this GitHub 存储库以允许用户通过 Steam 登录。
这个 repo 使用只持续一段时间的会话,或者直到浏览器关闭,所以我需要使用 cookie 来让用户长时间登录。
经过一些研究,从 cookie 文档和询问的问题来看,用户拥有用户名和密码,并且没有使用第三方登录。
我是第一次使用session和cookies,还请多多包涵。
登录代码如下所示:
<?php
ob_start();
session_start();
function logoutbutton() {
echo "<form action='' method='get'><button name='logout' type='submit'>Logout</button></form>";
}
function loginbutton($buttonstyle = "square") {
$button['rectangle'] = "01";
$button['square'] = "02";
$button = "<a href='?login'><img src='https://steamcommunity-a.akamaihd.net/public/images/signinthroughsteam/sits_".$button[$buttonstyle].".png'></a>";
echo $button;
}
if (isset($_GET['login'])){
require 'openid.php';
try {
require 'SteamConfig.php';
$openid = new LightOpenID($steamauth['domainname']);
if(!$openid->mode) {
$openid->identity = 'https://steamcommunity.com/openid';
header('Location: ' . $openid->authUrl());
} elseif ($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
if($openid->validate()) {
$id = $openid->identity;
$ptn = "/^https?:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
$_SESSION['steamid'] = $matches[1];
if (!headers_sent()) {
header('Location: '.$steamauth['loginpage']);
exit;
} else {
?>
<script type="text/javascript">
window.location.href="<?=$steamauth['loginpage']?>";
</script>
<noscript>
<meta http-equiv="refresh" content="0;url=<?=$steamauth['loginpage']?>" />
</noscript>
<?php
exit;
}
} else {
echo "User is not logged in.\n";
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
}
if (isset($_GET['logout'])){
require 'SteamConfig.php';
session_unset();
session_destroy();
header('Location: '.$steamauth['logoutpage']);
exit;
}
if (isset($_GET['update']) || !empty($_SESSION['steam_uptodate']) && $_SESSION['steam_uptodate']+(24*60*60) < time()){
unset($_SESSION['steam_uptodate']);
require 'userInfo.php';
header('Location: '.$_SERVER['PHP_SELF']);
exit;
}
?>
我尝试将 $_SESSION['steamid'] = $matches[1];
替换为 setcookie("CookieTest", $matches[1], time()+3600);
并将任何 $_SESSION
替换为 $_COOKIE
,当我尝试登录时,cookie 设置在浏览器,但它没有让我登录
我的问题是,如何使用此 Steam 身份验证实施 cookie,以便让用户保持登录状态以备将来访问?
要构建真正彻底的“记住我”功能,您通常需要一个数据库来存储生成的用户令牌,其中包含到期日期和您可能决定需要的任何其他条件。然后令牌将存储在 setcookie()
.
的 cookie 中
但最简单的方法是将普通 PHP 会话 cookie 的生命周期延长到超过其默认值 0
的时间段(直到浏览器关闭)。您可以使用 session_set_cookie_params()
执行此操作,选择较长的生命周期。
// Session cookie lasts 2 days
session_set_cookie_params(172800);
当使用会话 cookie 或其他 cookie 来识别用户时,始终使用 HTTPS 并设置 cookie 的 secure
参数
很重要
// Specify path, domain, and secure
session_set_cookie_params(172800, '/', '.example.com', true);
您可以采取的另一个针对会话固定的小缓解措施是通过简单地调用 session_regenerate_id()
在每个页面加载时生成一个新的会话 ID。如果会话 ID 恰好被拦截,如果真实用户仍然处于活动状态,它将不会长期有效。
我正在使用 this GitHub 存储库以允许用户通过 Steam 登录。
这个 repo 使用只持续一段时间的会话,或者直到浏览器关闭,所以我需要使用 cookie 来让用户长时间登录。
经过一些研究,从 cookie 文档和询问的问题来看,用户拥有用户名和密码,并且没有使用第三方登录。
我是第一次使用session和cookies,还请多多包涵。
登录代码如下所示:
<?php
ob_start();
session_start();
function logoutbutton() {
echo "<form action='' method='get'><button name='logout' type='submit'>Logout</button></form>";
}
function loginbutton($buttonstyle = "square") {
$button['rectangle'] = "01";
$button['square'] = "02";
$button = "<a href='?login'><img src='https://steamcommunity-a.akamaihd.net/public/images/signinthroughsteam/sits_".$button[$buttonstyle].".png'></a>";
echo $button;
}
if (isset($_GET['login'])){
require 'openid.php';
try {
require 'SteamConfig.php';
$openid = new LightOpenID($steamauth['domainname']);
if(!$openid->mode) {
$openid->identity = 'https://steamcommunity.com/openid';
header('Location: ' . $openid->authUrl());
} elseif ($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
if($openid->validate()) {
$id = $openid->identity;
$ptn = "/^https?:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
$_SESSION['steamid'] = $matches[1];
if (!headers_sent()) {
header('Location: '.$steamauth['loginpage']);
exit;
} else {
?>
<script type="text/javascript">
window.location.href="<?=$steamauth['loginpage']?>";
</script>
<noscript>
<meta http-equiv="refresh" content="0;url=<?=$steamauth['loginpage']?>" />
</noscript>
<?php
exit;
}
} else {
echo "User is not logged in.\n";
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
}
if (isset($_GET['logout'])){
require 'SteamConfig.php';
session_unset();
session_destroy();
header('Location: '.$steamauth['logoutpage']);
exit;
}
if (isset($_GET['update']) || !empty($_SESSION['steam_uptodate']) && $_SESSION['steam_uptodate']+(24*60*60) < time()){
unset($_SESSION['steam_uptodate']);
require 'userInfo.php';
header('Location: '.$_SERVER['PHP_SELF']);
exit;
}
?>
我尝试将 $_SESSION['steamid'] = $matches[1];
替换为 setcookie("CookieTest", $matches[1], time()+3600);
并将任何 $_SESSION
替换为 $_COOKIE
,当我尝试登录时,cookie 设置在浏览器,但它没有让我登录
我的问题是,如何使用此 Steam 身份验证实施 cookie,以便让用户保持登录状态以备将来访问?
要构建真正彻底的“记住我”功能,您通常需要一个数据库来存储生成的用户令牌,其中包含到期日期和您可能决定需要的任何其他条件。然后令牌将存储在 setcookie()
.
但最简单的方法是将普通 PHP 会话 cookie 的生命周期延长到超过其默认值 0
的时间段(直到浏览器关闭)。您可以使用 session_set_cookie_params()
执行此操作,选择较长的生命周期。
// Session cookie lasts 2 days
session_set_cookie_params(172800);
当使用会话 cookie 或其他 cookie 来识别用户时,始终使用 HTTPS 并设置 cookie 的 secure
参数
// Specify path, domain, and secure
session_set_cookie_params(172800, '/', '.example.com', true);
您可以采取的另一个针对会话固定的小缓解措施是通过简单地调用 session_regenerate_id()
在每个页面加载时生成一个新的会话 ID。如果会话 ID 恰好被拦截,如果真实用户仍然处于活动状态,它将不会长期有效。