PHP 实现单点登录

1.准备两个虚拟域名

127.0.0.1  www.openpoor.com

127.0.0.1  www.myspace.com

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

index.PHP

[php] view plain copy

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


login.php

[php] view plain copy

  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

[php] view plain copy

  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的设置

 [php] view plain copy

  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的文件内容如下

[php] view plain copy

  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,"");         
  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都是未登录状态

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

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

原文地址:https://www.cnblogs.com/zhangtianle/p/7375932.html