table('user')->where(['account'=>$account, 'password'=>md5($password)])->find();
if(empty($user)){
$_SESSION['error'] = '账号或密码有误';
require 'sso.html';
die;
}
unset($_SESSION['error']);
$login_a_url = "http://a.com/index.php?action=login&ticket=".$user['ticket'];
$login_b_url = "http://b.com/index.php?action=login&ticket=".$user['ticket'];
$res1 = file_get_content($login_a_url );
$res2 = file_get_content($login_b_url );
if($res1 == $res2 == 'success'){
header("location:".$server."?action=login&ticket=".$user['ticket']);
}else{
$_SESSION['error'] = '登录失败';
require 'sso.html';
}
}else{
$server = isset($_GET['server'])?trim($_GET['server']):die('来源不明');
require 'sso.html';
}
}
/**
* 退出登录
*/
public function logout()
{
$url1 = 'http://a.com/index.php?action=logout&server='.$_GET['server'];
header('Location:'.$url1);
}
/**
* 验证ticket有效性
*/
public function verify()
{
$ticket = trim($_GET['ticket']);
//验证ticket有效性
if($ticket){
$db = Db::getInstance();
$user = $db->table('user')->where(['ticket'=>$ticket])->find();
if($user){
echo 'success';
}else{
echo "fail";
}
die;
}
echo 'fail';
die;
}
/**
* 通过ticket获取用户信息
*/
public function user()
{
$ticket = trim($_GET['ticket']);
$db = Db::getInstance();
$user = $db->table('user')->where(['ticket'=>$ticket])->find();
echo json_encode($user);
}
}
$action = isset($_GET['action'])?trim($_GET['action']):'login';
(new Passport())->$action();
html表单
单点登录
_jump($msg,$url);
}
}else{
$msg = "您还未登录";
$url = "http://passport.com/index.php?action=login&server=http://a.com/index.php";
$this->_jump($msg,$url);
}
}
/**
* 若用户未登陆,则跳转到单点登陆
*/
public function index()
{
$ticket = @$_GET['ticket'];
if($ticket && !isset($_SESSION['user'])){
$verify_url = 'http://passport.com/index.php?action=verify&ticket='.$ticket;
if(file_get_contents($verify_url)=='success') {
// 获取用户信息
$get_user_info_url = 'http://passport.com/index.php?action=user&ticket=' . $ticket;
$user = file_get_contents($get_user_info_url);
$_SESSION['user'] = json_decode($user, true);
}else{
$msg = "您还未登录";
$url = "http://passport.com/index.php?action=login&server=http://a.com/index.php";
$this->_jump($msg,$url);
}
}
if($_SESSION['user']) {
$ticket = $_SESSION['user']['ticket'];
echo "";
echo "A已登陆成功退出
";
echo "跳转到B";
}else{
$msg = "您还未登录";
$url = "http://passport.com/index.php?action=login&server=http://a.com/index.php";
$this->_jump($msg,$url);
}
}
public function logout()
{
session_destroy();
$server = $_GET['server'];
$url1 = 'http://b.com/index.php?action=logout&server='.$server;
header('Location:'.$url1);
}
/**
* 跳转方法
* @param $msg
* @param $url
*/
private function _jump($msg, $url)
{
ob_clean();
echo "{$msg}3秒后跳转。";
echo "";
die;
}
}
$action = isset($_GET['action'])?trim($_GET['action']):'index';
(new A())->$action();
_jump($msg,$url);
}
}else{
$msg = "您还未登录";
$url = "http://passport.com/index.php?action=login&server=http://b.com/index.php";
$this->_jump($msg,$url);
}
}
/**
* 若用户未登陆,则跳转到单点登陆
*/
public function index()
{
$ticket = @$_GET['ticket'];
if($ticket && !isset($_SESSION['user'])){
$verify_url = 'http://passport.com/index.php?action=verify&ticket=' . $ticket;
if(file_get_contents($verify_url)=='success') {
// 获取用户信息
$get_user_info_url = 'http://passport.com/index.php?action=user&ticket=' . $ticket;
$user = file_get_contents($get_user_info_url);
$_SESSION['user'] = json_decode($user, true);
}else{
$msg = "您还未登录";
$url = "http://passport.com/index.php?action=login&server=http://b.com/index.php";
$this->_jump($msg,$url);
}
}
if($_SESSION['user']) {
$ticket = $_SESSION['user']['ticket'];
echo "";
echo "B已登陆成功退出
";
echo "跳转到A";
}else{
$msg = "您还未登录";
$url = "http://passport.com/index.php?action=login&server=http://b.com/index.php";
$this->_jump($msg,$url);
}
}
public function logout()
{
session_destroy();
$server = $_GET['server'];
$url2 = 'http://passport.com/index.php?action=login&server='.$server;
header('Location:'.$url2);
}
/**
* 跳转方法
* @param $msg
* @param $url
*/
private function _jump($msg, $url)
{
ob_clean();
echo "{$msg}3秒后跳转。";
echo "";
die;
}
}
$action = isset($_GET['action'])?trim($_GET['action']):'index';
(new B())->$action();