当前位置:首页 > PHP教程 > php应用 > 列表

PHP简单实现单点登录功能示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-05 16:26:44 浏览: 评论:0 

这篇文章主要介绍了PHP简单实现单点登录功能,结合实例形式分析了php基于session控制实现单点登录的相关操作技巧,需要的朋友可以参考下。

本文实例讲述了PHP简单实现单点登录功能,分享给大家供大家参考,具体如下:

1.准备两个虚拟域名

127.0.0.1  www.openpoor.com

127.0.0.1  www.myspace.com

2.在openpoor的根目录下创建以下文件

index.PHP

  1. <?php 
  2. session_start(); 
  3. ?> 
  4. <!DOCTYPE html> 
  5. <html> 
  6. <head> 
  7. <meta charset="UTF-8"/> 
  8. <title>sync login</title> 
  9. </head> 
  10. <body> 
  11. <?php if(emptyempty($_SESSION['username'])):?> 
  12. hello,游客;请先<a href="login.php" rel="external nofollow" >登录</a><a href="http://www.myspace.com/index.php" rel="external nofollow" rel="external nofollow" >进入空间</a> 
  13. <?php else: ?> 
  14. hello,<?php echo $_SESSION['username']; ?>;<a href="http://www.myspace.com/index.php" rel="external nofollow" rel="external nofollow" >进入空间</a> 
  15. <?php endif; ?> 
  16.  <a href="http://www.openpoor.com/index.php" rel="external nofollow" >home</a> 
  17. </body> 
  18. </html> 

login.php

  1. <?php 
  2. session_start(); 
  3. if(!emptyempty($_POST['username'])){ 
  4.  require '../Des.php'
  5.  $_SESSION['username'] = $_POST['username']; 
  6.  $redirect = 'http://www.openpoor.com/index.php'
  7.  header('Location:http://www.openpoor.com/sync.php?redirect='.urlencode($redirect).'&code='.Des::encrypt($_POST['username'],'openpoor'));exit
  8. ?> 
  9. <!DOCTYPE html> 
  10. <html> 
  11. <head> 
  12. <meta charset="UTF-8"/> 
  13. <title>sync login</title> 
  14. </head> 
  15. <body> 
  16. <form action="" method="post"
  17.  <input type="text" name="username" placeholder="用户名"/> 
  18.  <input type="text" name="password" placeholder="密码"/> 
  19.  <input type="submit" value="登录"/> 
  20. </form> 
  21. </body> 
  22. </html> 

sync.php

  1. <?php 
  2. $redirect = emptyempty($_GET['redirect']) ? 'www.openpoor.com' : $_GET['redirect']; 
  3. if(emptyempty($_GET['code'])){ 
  4.  header('Loaction:http://'.urldecode($redirect)); 
  5.  exit
  6. $apps = array
  7.  'www.myspace.com/slogin.php' 
  8. ); 
  9. ?> 
  10. <!DOCTYPE html> 
  11. <html> 
  12. <head> 
  13. <meta charset="UTF-8"/> 
  14. <?php foreach($apps as $v): ?> 
  15. <script type="text/javascript" src="http://<?php echo $v.'?code='.$_GET['code'] ?>"></script> 
  16. <?php endforeach; ?> 
  17. <title>passport</title> 
  18. </head> 
  19. <body> 
  20. <script type="text/javascript"
  21. window.onload=function(){ 
  22.  location.replace('<?php echo $redirect; ?>'); 
  23. </script> 
  24. </body> 
  25. </html> 

3.在myspace的根目录下创建如下文件

slogin文件 完成session的设置

  1. <?php 
  2. session_start(); 
  3. header('Content-Type:text/javascript; charset=utf-8'); 
  4. if(!emptyempty($_GET['code'])){ 
  5.  require '../Des.php'
  6.  $username = Des::decrypt($_GET['code'],'openpoor'); 
  7.  if(!emptyempty($username)){ 
  8.   header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); 
  9.   $_SESSION['username'] = $username
  10.  } 
  11. ?> 

index.php

  1. <?php 
  2. session_start(); 
  3. if(!emptyempty($_SESSION['username'])) 
  4.   echo "欢迎来到".$_SESSION['username']."的空间"
  5. }else
  6.   echo "请先登录"
  7. ?> 

4.Des.php的文件内容如下

  1. <?php 
  2. /** 
  3.  *@see Yii CSecurityManager; 
  4.  */ 
  5. class Des{ 
  6.  public static function encrypt($data,$key){ 
  7.    $module=mcrypt_module_open('des','', MCRYPT_MODE_CBC,''); 
  8.    $key=substr(md5($key),0,mcrypt_enc_get_key_size($module)); 
  9.    srand(); 
  10.    $iv=mcrypt_create_iv(mcrypt_enc_get_iv_size($module), MCRYPT_RAND); 
  11.    mcrypt_generic_init($module,$key,$iv); 
  12.    $encrypted=$iv.mcrypt_generic($module,$data); 
  13.    mcrypt_generic_deinit($module); 
  14.    mcrypt_module_close($module); 
  15.    return md5($data).'_'.base64_encode($encrypted); 
  16.  } 
  17.  public static function decrypt($data,$key){ 
  18.    $_data = explode('_',$data,2); 
  19.    if(count($_data)<2){ 
  20.   return false; 
  21.    } 
  22.    $data = base64_decode($_data[1]); 
  23.    $module=mcrypt_module_open('des','', MCRYPT_MODE_CBC,''); 
  24.    $key=substr(md5($key),0,mcrypt_enc_get_key_size($module)); 
  25.    $ivSize=mcrypt_enc_get_iv_size($module); 
  26.    $iv=substr($data,0,$ivSize); 
  27.    mcrypt_generic_init($module,$key,$iv); 
  28.    $decrypted=mdecrypt_generic($module,substr($data,$ivSize,strlen($data))); 
  29.    mcrypt_generic_deinit($module); 
  30.    mcrypt_module_close($module); 
  31.    $decrypted = rtrim($decrypted,"\0"); 
  32.    if($_data[0]!=md5($decrypted)){ 
  33.   return false; 
  34.    } 
  35.    return $decrypted
  36.  } 
  37. ?> 

当在openpoor登录后将session信息传到其他域名下的文件下进行处理,以script标签包含的形式进行运行。

Tags: PHP单点登录

分享到: