---
title: "UCenter原理"
author: "popwang"
date: "2011-06-15T16:35:41Z"
tags: []
categories: ["编程语言"]
slug: "ucenter"
draft: false
lastmod: "2011-06-15T17:47:07Z"
id: 1090612
url: "https://popwang.iteye.com/blog/1090612"
---

1、用户登录discuz，通过logging.php文件中的函数uc\_user\_login对post过来的数据进行验证，也就是对username和password进行验证。

2、如果验证成功，将调用位于uc\_client下client.php文件中的函数uc\_user\_synlogin，在这个函数中调用 uc\_api\_post('user', 'synlogin', array('uid'=&gt;$uid))。

3、然后这个函数后向Ucenter的index.php传递数据,index.php接受传递的数据，获得model为user，action为synlogin的值。

4、然后Ucenter的index.php调用control目录下的user.php类中的onsynlogin方法，通过foreach循环，以javascript的方式通知uc应用列表中开启同步登陆的应用进行同步登录；即通过get方式传递给各个应用目录中api下的uc.php一些数据。

5、uc.php接收通知并处理get过来的数据，并在函数synlogin（位于uc.php中）通过函数\_authcode加密数据（默认以UC\_KEY作为密钥），用函数\_setcookie设置cookie。

6、各个应用用对应的密钥解码上面设置的cookie，得到用户id等数据；通过这个值来判断用户是否经过其它应用登录过，从而让用户可以自动登陆。

应用程序的logging.php ------&gt;uc\_client中的client.php------&gt;Ucenter------&gt;应用程序中api/uc.php

      其实Ucenter实现同步登陆的原理就是cookie，一个应用登陆成功之后，向Ucenter传递数据，让Ucenter通知其他的应用也设置cookie，这样用户在访问其他应用的时候通过已经设置好的cookie实现自动登陆。了解了Ucenter的同步原理，再遇到无法同步登陆，或者开发一些与UCenter接口的时候就会容易很多。

 

login:

user(ajax or jquery)-&gt;login-&gt;(fun判断)-&gt;Ucenter\_Func-&gt;return to user

.continue  run ajax or jquery  -&gt;&gt;send list of all URL(is made up with site  which is store in the UCenter and en encoded String(action and name)) to each site

then each code which worked on different sites works ,likes java , UC,it is a servlet, it decode the url and run the code , for example ,it returns cookies

 

logout:

user(ajax or jquery)-&gt;logout-&gt;(fun判断)-&gt;Ucenter\_Func-&gt;return to user

.continue  run ajax or jquery  -&gt;&gt;send list of all (is made up with site  which is store in the UCenter and en encoded String(action and name)) each site

then each code which worked on different sites works ,likes java , UC,it is a servlet, it decode the url and run the code , for example ,it change every cookie to maxExpire=0

 

其余都相似。