PHP SSOÏe½a
*SSOÓÐÈýÖÖģʽ:¢Ù¿ç×ÓÓoµ¥µaµÇ½¢ÚÍeÈ«¿çµ¥µaÓoµÇ½¢ÛվȺ¹²ÏiÉiÝÈÏÖ¤**
µÚÒ»ÖÖģʽºÜ¼oµ¥,Ö»ÐeÒª½«CookieµÄÓoÉeÖóɶa¸oÓ¦ÓõĸuÓo¼´¿É
µÚ¶þÖֽʽ,Ò²ºÜ¼oµ¥,¾ÍÊǽ«ËuÒÔÓ¦ÓõÄÈÏÖ¤µØÖ¸u»»³Éͬһ¸oÈÏÖ¤µØÖ,ÿ´Î²e¿´ÊÇñÔÚÈÏÖ¤ÖÐÐĵǽ,Èç¹uµÇ½ÁË,¸øµ÷ÓÃÓ¦ÓâÅÒ»¸o¼ÓÃÜÁiÅƼ´¿É
µÚÈýÖÖ¿çÓo,¾ÍÊÇÀ´»ØÌøתÀ´»ØÑeÖ¤tokenÂÔÓÐÂe*³
ÅaÖÃĿ¼½a¹¹
ÔÚ*þÎñÆ÷¸uĿ¼ÏÂ,н¨Èý¸oÏiĿĿ¼:
|¨C/ÍøÕ¾¸uĿ¼/
|¨C|¨C/oa/
|¨C|¨C/bbs/
|¨C|¨C/blog/
ÔÚ¸uĿ¼ÏÂн¨functions.PHP½Å±¾Îļþ,¾ßÌaÄÚÈÝÈçÏÂ:
<?php
/**
* »ñÈ¡µÇ½token
* @param string $url »ñÈ¡tokenµÄµØÖ*
* 2017-01-03T13:08:43+0800
*/
function getToken($url)
{
$bool = isLogin();
if ($bool) {
// Èç¹uµÇ½ÁËÌøתµ½±¾Õ¾Ê×Ò³
header('location: index.php');
exit();
}
// *ñÔoûÓеǽ,È¥ÁiÒ»¸oÕ¾µa¿´ÊÇ*ñµÇ½
header('location: '.$url);
}
// УÑeÁiÅÆÊÇ*ñÕýÈ*
function yzToken($domain)
{
$url = isset($_GET['url']) ? $_GET['url'] : '';
$username = isset($_GET['username']) ? $_GET['username'] : '';
$token = isset($_GET['token']) ? $_GET['token'] : '';
if (!empty($username) && !empty($token)) {
$salt = 'taoip';
$_token = md5($salt.$username);
// УÑeµÚÈý*½Õ¾µa¹ýÀ´Ê±µÄtokenÊÇ*ñÕýÈ*
if ($_token == $token) {
// ÉeÖÃÌøת¹ýÀ´µÄÍøÕ¾µÄCookie
setCook($username, $_token, $domain);
header('location: index.php');
}
}
}
// ÉeÖÃcookie
function setCook($username, $_password, $domain)
{
// УÑe³É¹¦,¿ªÊ¼µÇ½
setcookie('username', $username, time()+3600, '/', $domain);
setcookie('token', $_password, time()+3600, '/', $domain);
header('location: index.php');
}
// ÅжÏÊÇ*ñµÇ½
function isLogin()
{
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
$salt = 'taoip';
$_token = md5($salt.$username);
if ($token == $_token) {
return true;
} else {
return false;
}
}
?>
ÔÚoaÏiĿĿ¼ÏÂ,н¨index.phpºÍlogin.phpÁ½¸o½Å±¾Îļþ
±a¼index.phpÎļþ
<?php
// OAÕ¾µa
// (1)¿ªÆoSession»a»°
session_name('taoip');
session_start();
// (2)»ñÈ¡Óû§ÃuºÍtoken½øÐÐУÑe
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
$salt = 'taoip';
$_token = md5($salt.$username);
if ($token != $_token) {
header('location: login.php');
exit();
}
echo "»¶Ó{$username}Óû§,*ÃÎÊOAÕ¾µa";
?>
±a¼login.phpÎļþ
<?php
// OAÕ¾µaµÇ½ϵͳ
require '../functions.php';
// (2)ÑeÖ¤
yzToken('taoip.cn');
// (1)ÅжÏÊÇ*ñµÇ½,µÇ½ÔoÌøתÊ×Ò³,δµÇ¼ÔoÈ¥ÆaËuÕ¾µa»ñÈ¡token
$url = isset($_GET['url']) ? $_GET['url'] : '';
if (empty($url)) {
getToken('http://dengpeng.cc/login.php?url=http://oa.taoip.cn/login.php');
}
// (1)ÅжÏÓû§ÊÇ*ñµÇ½
$bool = isLogin();
$url = isset($_GET['url']) ? $_GET['url'] : '';
if ($bool) {
if (empty($url)) {
header('location: index.php');
} else {
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
$lurl = $url.'?username='.$username.'&token;='.$token;
header('location: '.$lurl);
}
}
if (!empty($_POST)) {
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
// ´Ó¿aÖвeѯÓû§ÃÜÂe
@$link = mysql_connect('localhost', 'root', '');
mysql_query('use sso', $link);
mysql_query('set names utf8', $link);
$sql = "select * from users where username = '".$username."'";
$user = mysql_fetch_assoc(mysql_query($sql, $link));
// УÑe
$salt = 'taoip';
$_password = md5($salt.$username);
// var_dump($user['password'] == $_password);
// print_r($user);exit();
if ($user['password'] == $_password) {
// УÑe³É¹¦,¿ªÊ¼µÇ½
setcookie('username', $username, time()+3600, '/', 'taoip.cn');
setcookie('token', $_password, time()+3600, '/', 'taoip.cn');
// Èç¹uURLûÓÐÖµÖض¨Ïoµ½Ê×Ò³,*ñÔoÖض¨Ïoµ½URLÒ³Ãae
if (empty($url)) {
header('location: index.php');
} else {
header('location: '.$lurl);
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Sublime Text 3114">
<meta name="author" content="3@dengpeng.cc">
<meta name="keywords" content="">
<meta name="description" content="">
<title>OAÕ¾µaµÇ½ϵͳ</title>
</head>
<body>
<div class="container">
<h2>oa.taoip.cnÕ¾µaµÇ½ϵͳ</h2>
<form action="" method="post">
<label for="">Óû§Ãu</label>
<input type="text" name="username">
<br>
<label for="">ÃÜÂe</label>
<input type="text" name="password">
<hr>
<button type="submit">Ìa½»</button>
</form>
</div>
</body>
</html>
ÔÚbbsÏiĿĿ¼ÏÂ,н¨index.phpºÍlogin.phpÁ½¸o½Å±¾Îļþ
±a¼index.phpÎļþ
<?php
/**
* @author DengPeng <3@dengpeng.cc>
* @since 2017/01/03
* @copyright copyright (c) 2017 zixue.it GPL
* @license http://www.zixue.it/
*/
// BBSÕ¾µa
// (1)¿ªÆoSession»a»°
session_name('taoip');
session_start();
// (2)»ñÈ¡Óû§ÃuºÍtoken½øÐÐУÑe
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
$salt = 'taoip';
$_token = md5($salt.$username);
if ($token != $_token) {
header('location: login.php');
exit();
}
echo "»¶Ó{$username}Óû§,*ÃÎÊBBSÕ¾µa";
?>
±a¼login.phpÎļþ
<?php
/**
* @author DengPeng <3@dengpeng.cc>
* @since 2017/01/03
* @copyright copyright (c) 2017 zixue.it GPL
* @license http://www.zixue.it/
*/
// BBSÕ¾µaµÇ½ϵͳ
require '../functions.php';
// (2)ÑeÖ¤
yzToken('taoip.cn');
// (1)ÅжÏÊÇ*ñµÇ½,µÇ½ÔoÌøתÊ×Ò³,δµÇ¼ÔoÈ¥ÆaËuÕ¾µa»ñÈ¡token
$url = isset($_GET['url']) ? $_GET['url'] : '';
if (empty($url)) {
getToken('http://dengpeng.cc/login.php?url=http://bbs.taoip.cn/login.php');
}
// (1)ÅжÏÓû§ÊÇ*ñµÇ½
$bool = isLogin();
$url = isset($_GET['url']) ? $_GET['url'] : '';
if ($bool) {
if (empty($url)) {
header('location: index.php');
} else {
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
$lurl = $url.'?username='.$username.'&token;='.$token;
header('location: '.$lurl);
}
}
if (!empty($_POST)) {
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
// ´Ó¿aÖвeѯÓû§ÃÜÂe
@$link = mysql_connect('localhost', 'root', '');
mysql_query('use sso', $link);
mysql_query('set names utf8', $link);
$sql = "select * from users where username = '".$username."'";
$user = mysql_fetch_assoc(mysql_query($sql, $link));
// УÑe
$salt = 'taoip';
$_password = md5($salt.$username);
// var_dump($user['password'] == $_password);
// print_r($user);exit();
if ($user['password'] == $_password) {
// УÑe³É¹¦,¿ªÊ¼µÇ½
setcookie('username', $username, time()+3600, '/', 'taoip.cn');
setcookie('token', $_password, time()+3600, '/', 'taoip.cn');
// Èç¹uURLûÓÐÖµÖض¨Ïoµ½Ê×Ò³,*ñÔoÖض¨Ïoµ½URLÒ³Ãae
if (empty($url)) {
header('location: index.php');
} else {
header('location: '.$lurl);
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Sublime Text 3114">
<meta name="author" content="3@dengpeng.cc">
<meta name="keywords" content="">
<meta name="description" content="">
<title>BBSÕ¾µaµÇ½ϵͳ</title>
</head>
<body>
<div class="container">
<h2>bbs.taoip.cnÕ¾µaµÇ½ϵͳ</h2>
<form action="" method="post">
<label for="">Óû§Ãu</label>
<input type="text" name="username">
<br>
<label for="">ÃÜÂe</label>
<input type="text" name="password">
<hr>
<button type="submit">Ìa½»</button>
</form>
</div>
</body>
</html>
ÔÚblogÏiĿĿ¼ÏÂ,н¨index.phpºÍlogin.phpÁ½¸o½Å±¾Îļþ
±a¼index.phpÎļþ
<?php
/**
* @author DengPeng <3@dengpeng.cc>
* @since 2017/01/03
* @copyright copyright (c) 2017 zixue.it GPL
* @license http://www.zixue.it/
*/
// blogÕ¾µa
// (1)¿ªÆoSession»a»°
session_name('taoip');
session_start();
// (2)»ñÈ¡Óû§ÃuºÍtoken½øÐÐУÑe
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
$salt = 'taoip';
$_token = md5($salt.$username);
if ($token != $_token) {
header('location: login.php');
exit();
}
echo "»¶Ó{$username}Óû§,*ÃÎÊblogÕ¾µa";
?>
<?php
/**
* @author DengPeng <3@dengpeng.cc>
* @since 2017/01/03
* @copyright copyright (c) 2017 zixue.it GPL
* @license http://www.zixue.it/
*/
// blogÕ¾µa
// (1)¿ªÆoSession»a»°
session_name('taoip');
session_start();
// (2)»ñÈ¡Óû§ÃuºÍtoken½øÐÐУÑe
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
$salt = 'taoip';
$_token = md5($salt.$username);
if ($token != $_token) {
header('location: login.php');
exit();
}
echo "»¶Ó{$username}Óû§,*ÃÎÊblogÕ¾µa";
?>
±a¼login.phpÎļþ
<?php
/**
* @author DengPeng <3@dengpeng.cc>
* @since 2017/01/03
* @copyright copyright (c) 2017 zixue.it GPL
* @license http://www.zixue.it/
*/
// blogÕ¾µaµÇ½ϵͳ
require '../functions.php';
// (2)ÑeÖ¤
yzToken('dengpeng.cc');
// (1)ÅжÏÊÇ*ñµÇ½,µÇ½ÔoÌøתÊ×Ò³,δµÇ¼ÔoÈ¥ÆaËuÕ¾µa»ñÈ¡token
$url = isset($_GET['url']) ? $_GET['url'] : '';
if (empty($url)) {
getToken('http://oa.taoip.cn/login.php?url=http://dengpeng.cc/login.php');
}
// (1)ÅжÏÓû§ÊÇ*ñµÇ½
$bool = isLogin();
$url = isset($_GET['url']) ? $_GET['url'] : '';
if ($bool) {
if (empty($url)) {
header('location: index.php');
} else {
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
$lurl = $url.'?username='.$username.'&token;='.$token;
header('location: '.$lurl);
}
}
// (3)ÅжÏÓû§ÊÇ*ñÌa½»Êý¾Ý
if (!empty($_POST)) {
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
// ´Ó¿aÖвeѯÓû§ÃÜÂe
@$link = mysql_connect('localhost', 'root', '');
mysql_query('use sso', $link);
mysql_query('set names utf8', $link);
$sql = "select * from users where username = '".$username."'";
$user = mysql_fetch_assoc(mysql_query($sql, $link));
// УÑe
$salt = 'taoip';
$_password = md5($salt.$username);
// var_dump($user['password'] == $_password);
// print_r($user);exit();
if ($user['password'] == $_password) {
setCook($username, $_password, 'dengpeng.cc');
if (empty($url)) {
header('location: index.php');
} else {
header('location: '.$lurl);
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Sublime Text 3114">
<meta name="author" content="3@dengpeng.cc">
<meta name="keywords" content="">
<meta name="description" content="">
<title>blogÕ¾µaµÇ½ϵͳ</title>
</head>
<body>
<div class="container">
<h2>dengpeng.ccÕ¾µaµÇ½ϵͳ</h2>
<form action="" method="post">
<label for="">Óû§Ãu</label>
<input type="text" name="username">
<br>
<label for="">ÃÜÂe</label>
<input type="text" name="password">
<hr>
<button type="submit">Ìa½»</button>
</form>
</div>
</body>
</html>
ÅaÖñ¾µØÐeÄaÖ÷»u
¾ßÌaÅaÖò½Öe,ÎÒÏe´o¼ÒÓ¦¸Ã¶¼»aÁË,²»ÐeÒªÎÒһһ׸Êo.ÄaÖ»ÐeÒª°´ÕÕÎÒ¸øµÄ²ÎÕÕ,ÅaÖúͲ»Í¬ÓoÃu¶ÔӦĿ¼µÄÓ³Éa¼´¿É.
ÓoÃu /ÏiĿĿ¼/
oa.taoip.cn /oa/
bbs.taoip.cn /bbs/
dengpeng.cc /blog/
¹§Ï²Äu,ÒѾÍe³ÉÁËÒ»¸o¼oµ¥µÄSSOϵͳ
ÅaÖÃÍe³Éºo,¼ÇµÃÖØÆoWebþÎñÆ÷.È»ºoÄaÖ»ÐeÒªÃÎÊÕaÈý¸o²»Í¬µÄÕ¾µa,¼´¿ÉʵÏÖÒ»¸oÕ¾µaµÇ½,ÆaËuÕ¾µa²»ÔÙ*¢Ë͵ǽÇeÇo.
¸ÐлÔĶÁ£¬Ï£ÍuÄÜ°iÖuµ½´o¼Ò£¬Ð»Ð»´o¼Ò¶Ô±¾Õ¾µÄÖ§³Ö£¡