1.项目A:php
a.首页登陆页面index.php(是你当前项目首页)html
<?php header('Content-Type:text/html; charset=utf-8'); $sso_address = 'http://www.sso.com/login.php'; //你SSO所在的域名,不是当前项目地址 $callback_address = 'http://'.$_SERVER['HTTP_HOST'] .str_replace('index.php','',$_SERVER['SCRIPT_NAME']) .'callback.php'; //callback地址用于回调设置cookie if(isset($_COOKIE['sign'])){ exit("欢迎您{$_COOKIE['sign']} <a href='{$sso_address}?logout=1'>退出</a>"); }else{ echo '您还未登陆 <a href="'.$sso_address.'?callback='.$callback_address.'">点此登陆</a>'; } ?>
b.当前项目的中间件用来赋值cookie和清空cookie的文件callback.php服务器
<?php header('Content-Type:text/html; charset=utf-8'); if(empty($_GET)){ exit('您还未登陆'); }else{ foreach($_GET as $key=>$val){ setcookie($key,$val,0,''); } header("location:index.php"); }
2.SSO服务器:cookie
<?php header('Content-Type:text/html; charset=utf-8'); if(isset($_GET['logout'])){ setcookie('sign','',-300); $callback = $_COOKIE['callback']; unset($_GET['logout']); header("location:$callback.?sign"); //注意替换成你项目的域名 } if(isset($_POST['username']) && isset($_POST['password'])){ setcookie('sign',$_POST['username'],0,''); setcookie('callback',$_POST['callback'],0,''); header("location:".$_POST['callback']."?sign={$_POST['username']}&callback={$_POST['callback]}"); } if(empty($_COOKIE['sign'])){ ?> <form method="post"> <p>用户名:<input type="text" name="username" /></p> <p>密 码:<input type="password" name="password" /></p> <input type="hidden" name="callback" value="<?php echo $_GET['callback']; ?>" /> <input type="submit" value="登陆" /> </form> <?php }else{ $query = http_build_query($_COOKIE); echo "系统检测到您已登陆 {$_COOKIE['sign']} <a href='?logout'>退出</a>"; }