Table of Contents
PHP+RSA生成密钥位数为1024位步骤详解" >PHP+RSA生成密钥位数为1024位步骤详解
PHP开发api接口安全验证步骤详解" >PHP开发api接口安全验证步骤详解
PHP进制相互转换计算实现步骤" >PHP进制相互转换计算实现步骤
Home Backend Development PHP Tutorial Detailed explanation of steps to implement single sign-on with PHP

Detailed explanation of steps to implement single sign-on with PHP

May 18, 2018 am 09:59 AM
php step Detailed explanation

这次给大家带来PHP实现单点登录步骤详解,PHP实现单点登录的注意事项有哪些,下面就是实战案例,一起来看一下。

1.准备两个虚拟域名

127.0.0.1  www.openpoor.com
127.0.0.1  www.myspace.com

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

index.PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

session_start();

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8"/>

<title>sync login</title>

</head>

<body>

<?php if(empty($_SESSION[&#39;username&#39;])):?>

hello,游客;请先<a href="login.php" rel="external nofollow" >登录</a><a href="http://www.myspace.com/index.php" rel="external nofollow" rel="external nofollow" >进入空间</a>

<?php else: ?>

hello,<?php echo $_SESSION[&#39;username&#39;]; ?>;<a href="http://www.myspace.com/index.php" rel="external nofollow" rel="external nofollow" >进入空间</a>

<?php endif; ?>

 <a href="http://www.openpoor.com/index.php" rel="external nofollow" >home</a>

</body>

</html>

Copy after login

login.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?php

session_start();

if(!empty($_POST[&#39;username&#39;])){

 require &#39;../Des.php&#39;;

 $_SESSION[&#39;username&#39;] = $_POST[&#39;username&#39;];

 $redirect = &#39;http://www.openpoor.com/index.php&#39;;

 header(&#39;Location:http://www.openpoor.com/sync.php?redirect=&#39;.urlencode($redirect).&#39;&code=&#39;.Des::encrypt($_POST[&#39;username&#39;],&#39;openpoor&#39;));exit;

}

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8"/>

<title>sync login</title>

</head>

<body>

<form action="" method="post">

 <input type="text" name="username" placeholder="用户名"/>

 <input type="text" name="password" placeholder="密码"/>

 <input type="submit" value="登录"/>

</form>

</body>

</html>

Copy after login

sync.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<?php

$redirect = empty($_GET[&#39;redirect&#39;]) ? &#39;www.openpoor.com&#39; : $_GET[&#39;redirect&#39;];

if(empty($_GET[&#39;code&#39;])){

 header(&#39;Loaction:http://&#39;.urldecode($redirect));

 exit;

}

$apps = array(

 &#39;www.myspace.com/slogin.php&#39;

);

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8"/>

<?php foreach($apps as $v): ?>

<script type="text/javascript" src="http://<?php echo $v.&#39;?code=&#39;.$_GET[&#39;code&#39;] ?>"></script>

<?php endforeach; ?>

<title>passport</title>

</head>

<body>

<script type="text/javascript">

window.onload=function(){

 location.replace('<?php echo $redirect; ?>');

}

</script>

</body>

</html>

Copy after login

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

slogin文件 完成session的设置

1

2

3

4

5

6

7

8

9

10

11

12

<?php

session_start();

header(&#39;Content-Type:text/javascript; charset=utf-8&#39;);

if(!empty($_GET[&#39;code&#39;])){

 require &#39;../Des.php&#39;;

 $username = Des::decrypt($_GET[&#39;code&#39;],&#39;openpoor&#39;);

 if(!empty($username)){

  header(&#39;P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"&#39;);

  $_SESSION[&#39;username&#39;] = $username;

 }

}

?>

Copy after login

index.php

1

2

3

4

5

6

7

8

9

<?php

session_start();

if(!empty($_SESSION[&#39;username&#39;]))

{

  echo "欢迎来到".$_SESSION[&#39;username&#39;]."的空间";

}else{

  echo "请先登录";

}

?>

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<?php

/**

 *@see Yii CSecurityManager;

 */

class Des{

 public static function encrypt($data,$key){

   $module=mcrypt_module_open(&#39;des&#39;,&#39;&#39;, MCRYPT_MODE_CBC,&#39;&#39;);

   $key=substr(md5($key),0,mcrypt_enc_get_key_size($module));

   srand();

   $iv=mcrypt_create_iv(mcrypt_enc_get_iv_size($module), MCRYPT_RAND);

   mcrypt_generic_init($module,$key,$iv);

   $encrypted=$iv.mcrypt_generic($module,$data);

   mcrypt_generic_deinit($module);

   mcrypt_module_close($module);

   return md5($data).&#39;_&#39;.base64_encode($encrypted);

 }

 public static function decrypt($data,$key){

   $_data = explode(&#39;_&#39;,$data,2);

   if(count($_data)<2){

  return false;

   }

   $data = base64_decode($_data[1]);

   $module=mcrypt_module_open(&#39;des&#39;,&#39;&#39;, MCRYPT_MODE_CBC,&#39;&#39;);

   $key=substr(md5($key),0,mcrypt_enc_get_key_size($module));

   $ivSize=mcrypt_enc_get_iv_size($module);

   $iv=substr($data,0,$ivSize);

   mcrypt_generic_init($module,$key,$iv);

   $decrypted=mdecrypt_generic($module,substr($data,$ivSize,strlen($data)));

   mcrypt_generic_deinit($module);

   mcrypt_module_close($module);

   $decrypted = rtrim($decrypted,"\0");

   if($_data[0]!=md5($decrypted)){

  return false;

   }

   return $decrypted;

 }

}

?>

Copy after login

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

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

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

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

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

PHP+RSA生成密钥位数为1024位步骤详解

PHP开发api接口安全验证步骤详解

PHP进制相互转换计算实现步骤

The above is the detailed content of Detailed explanation of steps to implement single sign-on with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1280
29
C# Tutorial
1257
24
The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Using Laravel: Streamlining Web Development with PHP Using Laravel: Streamlining Web Development with PHP Apr 19, 2025 am 12:18 AM

Laravel optimizes the web development process including: 1. Use the routing system to manage the URL structure; 2. Use the Blade template engine to simplify view development; 3. Handle time-consuming tasks through queues; 4. Use EloquentORM to simplify database operations; 5. Follow best practices to improve code quality and maintainability.

PHP and IIS: Making Them Work Together PHP and IIS: Making Them Work Together Apr 21, 2025 am 12:06 AM

Configuring and running PHP on IIS requires the following steps: 1) Download and install PHP, 2) Configuring IIS and adding FastCGI module, 3) Create and set up an application pool, 4) Create a website and bind to an application pool. Through these steps, you can easily deploy PHP applications on your Windows server and improve application stability and efficiency by configuring scaling and optimizing performance.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

See all articles