PHP单点登录SSO简单示例Demo
我本地建了三个测试站点,分别为:
- www.testa.com
- www.testb.com
- www.sso.com
其中,站点sso是用于集中验证的站点。
访问站点testa index.php
<?php
// testa: index.php
$url = urlencode("http://www.testa.com/");
if ($_GET['action'] == 'logout') {
setcookie("islogged");
}
if (isset($_GET['token']) && !empty($_GET['token'])) {
$token = $_GET['token'];
$res = file_get_contents("http://www.sso.com/verify.php?token={$token}");
if ($res == 'yes') {
echo "verify success";
setcookie('islogged', true);
} else {
echo "verify fail";
}
header("Location: index.php");
exit;
}
if ($_COOKIE['islogged'] == true) {
echo "a success";
} else {
echo "fail";
header("Location: http://www.sso.com/index.php?redirect={$url}");
}
程序判断用户是否登录站点testa,如果没有,则跳转到站点sso进行验证。