当前位置:Gxlcms > PHP教程 > PHP编程SSO详细介绍及简单实例

PHP编程SSO详细介绍及简单实例

时间:2021-07-01 10:21:17 帮助过:3人阅读

PHP SSO详解

SSO有三种模式:①跨子域单点登陆②完全跨单点域登陆③站群共享身份认证

第一种模式很简单,只需要将Cookie的域设置成多个应用的根域即可

第二种方式,也很简单,就是将所以应用的认证地址更换成同一个认证地址,每次查看是否在认证中心登陆,如果登陆了,给调用应用发放一个加密令牌即可

第三种跨域,就是来回跳转来回验证token略有麻烦

配置目录结构

在服务器根目录下,新建三个项目目录:

|–/网站根目录/
|–|–/oa/
|–|–/bbs/
|–|–/blog/

在根目录下新建functions.PHP脚本文件,具体内容如下:

  1. <?php
  2. /**
  3. * 获取登陆token
  4. * @param string $url 获取token的地址
  5. * 2017-01-03T13:08:43+0800
  6. */
  7. function getToken($url)
  8. {
  9. $bool = isLogin();
  10. if ($bool) {
  11. // 如果登陆了跳转到本站首页
  12. header('location: index.php');
  13. exit();
  14. }
  15. // 否则没有登陆,去另一个站点看是否登陆
  16. header('location: '.$url);
  17. }
  18. // 校验令牌是否正确
  19. function yzToken($domain)
  20. {
  21. $url = isset($_GET['url']) ? $_GET['url'] : '';
  22. $username = isset($_GET['username']) ? $_GET['username'] : '';
  23. $token = isset($_GET['token']) ? $_GET['token'] : '';
  24. if (!empty($username) && !empty($token)) {
  25. $salt = 'taoip';
  26. $_token = md5($salt.$username);
  27. // 校验第三方站点过来时的token是否正确
  28. if ($_token == $token) {
  29. // 设置跳转过来的网站的Cookie
  30. setCook($username, $_token, $domain);
  31. header('location: index.php');
  32. }
  33. }
  34. }
  35. // 设置cookie
  36. function setCook($username, $_password, $domain)
  37. {
  38. // 校验成功,开始登陆
  39. setcookie('username', $username, time()+3600, '/', $domain);
  40. setcookie('token', $_password, time()+3600, '/', $domain);
  41. header('location: index.php');
  42. }
  43. // 判断是否登陆
  44. function isLogin()
  45. {
  46. $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
  47. $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
  48. $salt = 'taoip';
  49. $_token = md5($salt.$username);
  50. if ($token == $_token) {
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. ?>

在oa项目目录下,新建index.php和login.php两个脚本文件

编辑index.php文件

  1. <?php
  2. // OA站点
  3. // (1)开启Session会话
  4. session_name('taoip');
  5. session_start();
  6. // (2)获取用户名和token进行校验
  7. $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
  8. $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
  9. $salt = 'taoip';
  10. $_token = md5($salt.$username);
  11. if ($token != $_token) {
  12. header('location: login.php');
  13. exit();
  14. }
  15. echo "欢迎{$username}用户,访问OA站点";
  16. ?>

编辑login.php文件

  1. <?php
  2. // OA站点登陆系统
  3. require '../functions.php';
  4. // (2)验证
  5. yzToken('taoip.cn');
  6. // (1)判断是否登陆,登陆则跳转首页,未登录则去其他站点获取token
  7. $url = isset($_GET['url']) ? $_GET['url'] : '';
  8. if (empty($url)) {
  9. getToken('http://dengpeng.cc/login.php?url=http://oa.taoip.cn/login.php');
  10. }
  11. // (1)判断用户是否登陆
  12. $bool = isLogin();
  13. $url = isset($_GET['url']) ? $_GET['url'] : '';
  14. if ($bool) {
  15. if (empty($url)) {
  16. header('location: index.php');
  17. } else {
  18. $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
  19. $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
  20. $lurl = $url.'?username='.$username.'&token='.$token;
  21. header('location: '.$lurl);
  22. }
  23. }
  24. if (!empty($_POST)) {
  25. $username = isset($_POST['username']) ? $_POST['username'] : '';
  26. $password = isset($_POST['password']) ? $_POST['password'] : '';
  27. // 从库中查询用户密码
  28. @$link = mysql_connect('localhost', 'root', '');
  29. mysql_query('use sso', $link);
  30. mysql_query('set names utf8', $link);
  31. $sql = "select * from users where username = '".$username."'";
  32. $user = mysql_fetch_assoc(mysql_query($sql, $link));
  33. // 校验
  34. $salt = 'taoip';
  35. $_password = md5($salt.$username);
  36. // var_dump($user['password'] == $_password);
  37. // print_r($user);exit();
  38. if ($user['password'] == $_password) {
  39. // 校验成功,开始登陆
  40. setcookie('username', $username, time()+3600, '/', 'taoip.cn');
  41. setcookie('token', $_password, time()+3600, '/', 'taoip.cn');
  42. // 如果URL没有值重定向到首页,否则重定向到URL页面
  43. if (empty($url)) {
  44. header('location: index.php');
  45. } else {
  46. header('location: '.$lurl);
  47. }
  48. }
  49. }
  50. ?>
  51. <!DOCTYPE html>
  52. <html>
  53. <head>
  54. <meta charset="UTF-8">
  55. <meta name="generator" content="Sublime Text 3114">
  56. <meta name="author" content="3@dengpeng.cc">
  57. <meta name="keywords" content="">
  58. <meta name="description" content="">
  59. <title>OA站点登陆系统</title>
  60. </head>
  61. <body>
  62. <p>
  63. <h2>oa.taoip.cn站点登陆系统</h2>
  64. <form action="" method="post">
  65. <label for="">用户名</label>
  66. <input type="text" name="username">
  67. <br>
  68. <label for="">密码</label>
  69. <input type="text" name="password">
  70. <hr>
  71. <button type="submit">提交</button>
  72. </form>
  73. </p>
  74. </body>
  75. </html>

在bbs项目目录下,新建index.php和login.php两个脚本文件

编辑index.php文件

  1. <?php
  2. /**
  3. * @author DengPeng <3@dengpeng.cc>
  4. * @since 2017/01/03
  5. * @copyright copyright (c) 2017 zixue.it GPL
  6. * @license http://www.zixue.it/
  7. */
  8. // BBS站点
  9. // (1)开启Session会话
  10. session_name('taoip');
  11. session_start();
  12. // (2)获取用户名和token进行校验
  13. $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
  14. $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
  15. $salt = 'taoip';
  16. $_token = md5($salt.$username);
  17. if ($token != $_token) {
  18. header('location: login.php');
  19. exit();
  20. }
  21. echo "欢迎{$username}用户,访问BBS站点";
  22. ?>

编辑login.php文件

  1. <?php
  2. /**
  3. * @author DengPeng <3@dengpeng.cc>
  4. * @since 2017/01/03
  5. * @copyright copyright (c) 2017 zixue.it GPL
  6. * @license http://www.zixue.it/
  7. */
  8. // BBS站点登陆系统
  9. require '../functions.php';
  10. // (2)验证
  11. yzToken('taoip.cn');
  12. // (1)判断是否登陆,登陆则跳转首页,未登录则去其他站点获取token
  13. $url = isset($_GET['url']) ? $_GET['url'] : '';
  14. if (empty($url)) {
  15. getToken('http://dengpeng.cc/login.php?url=http://bbs.taoip.cn/login.php');
  16. }
  17. // (1)判断用户是否登陆
  18. $bool = isLogin();
  19. $url = isset($_GET['url']) ? $_GET['url'] : '';
  20. if ($bool) {
  21. if (empty($url)) {
  22. header('location: index.php');
  23. } else {
  24. $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
  25. $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
  26. $lurl = $url.'?username='.$username.'&token='.$token;
  27. header('location: '.$lurl);
  28. }
  29. }
  30. if (!empty($_POST)) {
  31. $username = isset($_POST['username']) ? $_POST['username'] : '';
  32. $password = isset($_POST['password']) ? $_POST['password'] : '';
  33. // 从库中查询用户密码
  34. @$link = mysql_connect('localhost', 'root', '');
  35. mysql_query('use sso', $link);
  36. mysql_query('set names utf8', $link);
  37. $sql = "select * from users where username = '".$username."'";
  38. $user = mysql_fetch_assoc(mysql_query($sql, $link));
  39. // 校验
  40. $salt = 'taoip';
  41. $_password = md5($salt.$username);
  42. // var_dump($user['password'] == $_password);
  43. // print_r($user);exit();
  44. if ($user['password'] == $_password) {
  45. // 校验成功,开始登陆
  46. setcookie('username', $username, time()+3600, '/', 'taoip.cn');
  47. setcookie('token', $_password, time()+3600, '/', 'taoip.cn');
  48. // 如果URL没有值重定向到首页,否则重定向到URL页面
  49. if (empty($url)) {
  50. header('location: index.php');
  51. } else {
  52. header('location: '.$lurl);
  53. }
  54. }
  55. }
  56. ?>
  57. <!DOCTYPE html>
  58. <html>
  59. <head>
  60. <meta charset="UTF-8">
  61. <meta name="generator" content="Sublime Text 3114">
  62. <meta name="author" content="3@dengpeng.cc">
  63. <meta name="keywords" content="">
  64. <meta name="description" content="">
  65. <title>BBS站点登陆系统</title>
  66. </head>
  67. <body>
  68. <p>
  69. <h2>bbs.taoip.cn站点登陆系统</h2>
  70. <form action="" method="post">
  71. <label for="">用户名</label>
  72. <input type="text" name="username">
  73. <br>
  74. <label for="">密码</label>
  75. <input type="text" name="password">
  76. <hr>
  77. <button type="submit">提交</button>
  78. </form>
  79. </p>
  80. </body>
  81. </html>

在blog项目目录下,新建index.php和login.php两个脚本文件

编辑index.php文件

  1. <?php
  2. /**
  3. * @author DengPeng <3@dengpeng.cc>
  4. * @since 2017/01/03
  5. * @copyright copyright (c) 2017 zixue.it GPL
  6. * @license http://www.zixue.it/
  7. */
  8. // blog站点
  9. // (1)开启Session会话
  10. session_name('taoip');
  11. session_start();
  12. // (2)获取用户名和token进行校验
  13. $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
  14. $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
  15. $salt = 'taoip';
  16. $_token = md5($salt.$username);
  17. if ($token != $_token) {
  18. header('location: login.php');
  19. exit();
  20. }
  21. echo "欢迎{$username}用户,访问blog站点";
  22. ?>
  23. <?php
  24. /**
  25. * @author DengPeng <3@dengpeng.cc>
  26. * @since 2017/01/03
  27. * @copyright copyright (c) 2017 zixue.it GPL
  28. * @license http://www.zixue.it/
  29. */
  30. // blog站点
  31. // (1)开启Session会话
  32. session_name('taoip');
  33. session_start();
  34. // (2)获取用户名和token进行校验
  35. $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
  36. $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
  37. $salt = 'taoip';
  38. $_token = md5($salt.$username);
  39. if ($token != $_token) {
  40. header('location: login.php');
  41. exit();
  42. }
  43. echo "欢迎{$username}用户,访问blog站点";
  44. ?>

编辑login.php文件

  1. <?php
  2. /**
  3. * @author DengPeng <3@dengpeng.cc>
  4. * @since 2017/01/03
  5. * @copyright copyright (c) 2017 zixue.it GPL
  6. * @license http://www.zixue.it/
  7. */
  8. // blog站点登陆系统
  9. require '../functions.php';
  10. // (2)验证
  11. yzToken('dengpeng.cc');
  12. // (1)判断是否登陆,登陆则跳转首页,未登录则去其他站点获取token
  13. $url = isset($_GET['url']) ? $_GET['url'] : '';
  14. if (empty($url)) {
  15. getToken('http://oa.taoip.cn/login.php?url=http://dengpeng.cc/login.php');
  16. }
  17. // (1)判断用户是否登陆
  18. $bool = isLogin();
  19. $url = isset($_GET['url']) ? $_GET['url'] : '';
  20. if ($bool) {
  21. if (empty($url)) {
  22. header('location: index.php');
  23. } else {
  24. $username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
  25. $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';
  26. $lurl = $url.'?username='.$username.'&token='.$token;
  27. header('location: '.$lurl);
  28. }
  29. }
  30. // (3)判断用户是否提交数据
  31. if (!empty($_POST)) {
  32. $username = isset($_POST['username']) ? $_POST['username'] : '';
  33. $password = isset($_POST['password']) ? $_POST['password'] : '';
  34. // 从库中查询用户密码
  35. @$link = mysql_connect('localhost', 'root', '');
  36. mysql_query('use sso', $link);
  37. mysql_query('set names utf8', $link);
  38. $sql = "select * from users where username = '".$username."'";
  39. $user = mysql_fetch_assoc(mysql_query($sql, $link));
  40. // 校验
  41. $salt = 'taoip';
  42. $_password = md5($salt.$username);
  43. // var_dump($user['password'] == $_password);
  44. // print_r($user);exit();
  45. if ($user['password'] == $_password) {
  46. setCook($username, $_password, 'dengpeng.cc');
  47. if (empty($url)) {
  48. header('location: index.php');
  49. } else {
  50. header('location: '.$lurl);
  51. }
  52. }
  53. }
  54. ?>
  55. <!DOCTYPE html>
  56. <html>
  57. <head>
  58. <meta charset="UTF-8">
  59. <meta name="generator" content="Sublime Text 3114">
  60. <meta name="author" content="3@dengpeng.cc">
  61. <meta name="keywords" content="">
  62. <meta name="description" content="">
  63. <title>blog站点登陆系统</title>
  64. </head>
  65. <body>
  66. <p>
  67. <h2>dengpeng.cc站点登陆系统</h2>
  68. <form action="" method="post">
  69. <label for="">用户名</label>
  70. <input type="text" name="username">
  71. <br>
  72. <label for="">密码</label>
  73. <input type="text" name="password">
  74. <hr>
  75. <button type="submit">提交</button>
  76. </form>
  77. </p>
  78. </body>
  79. </html>

配置本地虚拟主机

具体配置步骤,我想大家应该都会了,不需要我一一赘述.你只需要按照我给的参照,配置和不同域名对应目录的映射即可.

域名 /项目目录/
oa.taoip.cn /oa/
bbs.taoip.cn /bbs/
dengpeng.cc /blog/

恭喜您,已经完成了一个简单的SSO系统

配置完成后,记得重启Web服务器.然后你只需要访问这三个不同的站点,即可实现一个站点登陆,其他站点不再发送登陆请求.

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多PHP编程 SSO详细介绍及简单实例相关文章请关注PHP中文网!

人气教程排行