使用php实现单点登录

1.准备两个虚拟域名

127.0.0.1  www.openpoor.com

127.0.0.1  www.myspace.com

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

index.PHP

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

  1. <?php    
  2. session_start();    
  3. if(!empty($_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. ?>    
  10. <!DOCTYPE html>    
  11. <html>    
  12. <head>    
  13. <meta charset="UTF-8"/>    
  14. <title>sync login</title>    
  15. </head>    
  16. <body>    
  17. <form action="" method="post">    
  18.   <input type="text" name="username" placeholder="用户名"/>    
  19.   <input type="text" name="password" placeholder="密码"/>    
  20.   <input type="submit" value="登录"/>    
  21. </form>    
  22. </body>    
  23. </html>    
sync.php

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

slogin文件 完成session的设置

  1. <?php  
  2. session_start();  
  3. header('Content-Type:text/javascript; charset=utf-8');  
  4. if(!empty($_GET['code'])){  
  5.   require '../Des.php';  
  6.   $username = Des::decrypt($_GET['code'],'openpoor');  
  7.   if(!empty($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. }  
  12. ?>  
index.php

[php] view plain copy
  1. <?php  
  2. session_start();  
  3. if(!empty($_SESSION['username']))  
  4. {  
  5.     echo "欢迎来到".$_SESSION['username']."的空间";  
  6. }else{  
  7.     echo "请先登录";  
  8. }  
  9. ?>  
4.Des.php的文件内容如下

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

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


5.此时访问www.openpoor.com和www.myspace.com都是未登录状态

登录后两个域名下都是登录状态

到此我们实现了一个简单的单点登录。

posted on 2016-12-01 13:27  如果蜗牛有爱情  阅读(221)  评论(0编辑  收藏  举报

导航