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>"; }