网站地图    收藏   

主页 > 后端 > ucenter >

uc_client与ucenter通信原理 - UCenter

来源:自学PHP网    时间:2014-12-02 12:01 作者: 阅读:

[导读] 以用户登录为例介绍,其它注销,改密码,消息,头像,好友均类同 1 从用户xxx在某一应用程序的login php,输入用户名,密码讲起。先用uc_user_lo...

uc_client与ucenter通信原理

以用户登录为例介绍,其它注销,改密码,消息,头像,好友均类同.

1.从用户xxx在某一应用程序的login.php,输入用户名,密码讲起。

先用uc_user_login函数到uc server验证此用户和密码,如正确,则写入session,写入cookies,并更新应用程序会员表中的登录ip,登录时间。用户感觉不到这个过程。

2.然后通过uc_user_synlogin通知uc server 用户xxx登录成功,这个过程可能使用ajax,用户感觉不到通知过程。

3.uc server收到这个消息后,马上命令手下,把xxx登录的消息,像令牌环一样,发给所有愿意接收(后台中那个是否开启同步登录)这个消息的其它应用程序。其实就是带参数访问一下各应用程序的uc.php,用户感觉不到这个过程。

4.各应用程序靠api下的uc.php来接收uc server发来的消息,并对uc server言听计从,让干什么就干什么。现在,收到让xxx用户在你的程序中登录的命令,马上执行。

并写本应用程序的session,并且使用p3p, 写入相同域或不同域的cookies.   用户感觉不到这个过程。

5.最后所有和uc整合的程序,xxx均登录成功。用户从www.test.com/bbs登录后, 跳到www.test.com/news同样显示登录。因为bbs 和news系统在后台均已登录。

6.应用程序与uc server的会话结束。

得益于uc设计的精巧过程,整个过程,用户完全感觉不到ucenter的存在.这是整合程序历史上的创新。

以下为其中的一个例子:

Supesite的uc_client和ucenter登录通信过程

1、登录入口Index.php?action-login

  1. //系统频道 
  2. if($_SGET['action'] != 'index') {      
  3. if(emptyempty($channels['menus'][$_SGET['action']]['upnameid']) && $channels['menus'][$_SGET['action']]['upnameid'] != 'news') { 
  4. $scriptfile = S_ROOT.'./'.$_SGET['action'].'.php'
  5. else { 
  6. $scriptfile = S_ROOT.'./news.php'
  7.      //echo   $scriptfile; 
  8. if(file_exists($scriptfile)) { 
  9. include_once($scriptfile); 
  10. exit(); 

登录控制器:Login.php

登录视图:Site_login.html.php

提交登录action:batch.login.php?action=login

2、登录处理地址batch.login.php?action=login

include_once(S_ROOT.'./uc_client/client.php');

登录操作及其中涉及到的一些函数:

$password = $_POST['password'];

$username = $_POST['username'];

去ucenter进行远程登录验证

$ucresult = uc_user_login($username, $password, $loginfield == 'uid');

如果登录成功,则查本地用户信息,如果有更新本地信息,如果没有插入新的用户数据保持与ucenter进行同步,然后同步其他子系统登录信息:

  1. $msg = $lang['login_succeed'].uc_user_synlogin($members['uid']); 
  2.  
  3. function uc_user_synlogin($uid) { 
  4. $uid = intval($uid); 
  5. $return = uc_api_post('user''synlogin'array('uid'=>$uid)); 
  6. return $return
  7.  
  8.  
  9. function uc_api_post($module$action$arg = array()) { 
  10. $s = $sep = ''
  11. foreach($arg as $k => $v) { 
  12. $k = urlencode($k); 
  13. if(is_array($v)) { 
  14. $s2 = $sep2 = ''
  15. foreach($v as $k2 => $v2) { 
  16. $k2 = urlencode($k2); 
  17. $s2 .= "$sep2{$k}[$k2]=".urlencode(uc_stripslashes($v2)); 
  18. $sep2 = '&'
  19. $s .= $sep.$s2
  20. else { 
  21. $s .= "$sep$k=".urlencode(uc_stripslashes($v)); 
  22. $sep = '&'
  23. $postdata = uc_api_requestdata($module$action$s); 
  24.  
  25. return uc_fopen2(UC_API.'/index.php', 500000, $postdata'', TRUE, UC_IP, 20); 
  26.  
  27.  
  28. function uc_api_requestdata($module$action$arg=''$extra='') { 
  29. $input = uc_api_input($arg); 
  30. $post = "m=$module&a=$action&inajax=2&release=".UC_CLIENT_RELEASE."&input=$input&appid=".UC_APPID.$extra
  31. return $post
  32.  
  33. function uc_api_url($module$action$arg=''$extra='') { 
  34. $url = UC_API.'/index.php?'.uc_api_requestdata($module$action$arg$extra); 
  35. return $url
  36.  
  37. function uc_api_input($data) { 
  38. $s = urlencode(uc_authcode($data.'&agent='.md5($_SERVER['HTTP_USER_AGENT'])."&time=".time(), 'ENCODE', UC_KEY)); 
  39. return $s
  40.  
  41.  
  42. function uc_fopen2($url$limit = 0, $post = ''$cookie = ''$bysocket = FALSE, $ip = ''$timeout = 15, $block = TRUE) { 
  43. $__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1; 
  44. if($__times__ > 2) { 
  45. return ''
  46. $url .= (strpos($url'?') === FALSE ? '?' : '&')."__times__=$__times__"
  47. return uc_fopen($url$limit$post$cookie$bysocket$ip$timeout$block); 
  48.  
  49. function uc_fopen($url$limit = 0, $post = ''$cookie = ''$bysocket = FALSE, $ip = ''$timeout = 15, $block = TRUE) { 
  50. $return = ''
  51. $matches = parse_url($url); 
  52. !isset($matches['host']) && $matches['host'] = ''
  53. !isset($matches['path']) && $matches['path'] = ''
  54. !isset($matches['query']) && $matches['query'] = ''
  55. !isset($matches['port']) && $matches['port'] = ''
  56. $host = $matches['host']; 
  57. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/'
  58. $port = !emptyempty($matches['port']) ? $matches['port'] : 80; 
  59. if($post) { 
  60. $out = "POST $path HTTP/1.0\r\n"
  61. $out .= "Accept: **\r\n"
  62. //$out .= "Referer: $boardurl\r\n"; 
  63. $out .= "Accept-Language: zh-cn\r\n"
  64. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"
  65. $out .= "Host: $host\r\n"
  66. $out .= "Connection: Close\r\n"
  67. $out .= "Cookie: $cookie\r\n\r\n"
  68. $fp = @fsockopen(($ip ? $ip : $host), $port$errno$errstr$timeout); 
  69. if(!$fp) { 
  70. return '';//note $errstr : $errno \r\n 
  71. else { 
  72. stream_set_blocking($fp$block); 
  73. stream_set_timeout($fp$timeout); 
  74. @fwrite($fp$out); 
  75. $status = stream_get_meta_data($fp); 
  76. if(!$status['timed_out']) { 
  77. while (!feof($fp)) { 
  78. if(($header = @fgets($fp)) && ($header == "\r\n" ||   $header == "\n")) { 
  79. break
  80.  
  81. $stop = false; 
  82. while(!feof($fp) && !$stop) { 
  83. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); 
  84. $return .= $data
  85. if($limit) { 
  86. $limit -= strlen($data); 
  87. $stop = $limit <= 0; 
  88. @fclose($fp); 
  89. return $return

远程同步登录子系统操作之后:

  1. //显示信息 
  2. function showmessage($message$url_forward=''$second=3, $vars=array()) { 
  3. global $_SGLOBAL$_SCONFIG$_SC$channels
  4.  
  5. if(emptyempty($_SGLOBAL['inajax']) && $url_forward && emptyempty($second)) { 
  6. //直接301跳转 
  7. obclean(); 
  8. header("HTTP/1.1 301 Moved Permanently"); 
  9. header("Location: $url_forward"); 
  10. else { 
  11. if(!defined('IN_SUPESITE_ADMINCP')) { 
  12. $tpl_file = 'showmessage'
  13. $fullpath = 0; 
  14. include_once(S_ROOT.'./language/message.lang.php'); 
  15. if(!emptyempty($mlang[$message])) $message = $mlang[$message]; 
  16. else { 
  17. $tpl_file = 'admin/tpl/showmessage.htm'
  18. $fullpath = 1; 
  19. include_once(S_ROOT.'./language/admincp_message.lang.php'); 
  20. if(!emptyempty($amlang[$message])) $message = $amlang[$message]; 
  21.  
  22. if(isset($_SGLOBAL['mlang'][$message])) $message = $_SGLOBAL['mlang'][$message]; 
  23. foreach ($vars as $key => $val) { 
  24. $message = str_replace('{'.$key.'}'$val$message); 
  25. //显示 
  26. obclean(); 
  27. if(!emptyempty($url_forward)) { 
  28. $second = $second * 1000; 
  29. $message .= "<script>setTimeout(\"window.location.href ='$url_forward';\", $second);</script><ajaxok>"
  30.  
  31. include template($tpl_file$fullpath); 
  32. ob_out(); 
  33. exit(); 

Supesite中的Common.php部分解读:

1、define('S_ROOT', dirname(__FILE__).DIRECTORY_SEPARATOR);

dirname(__FILE__)

S_ROOT=E:\mydoc\supesite

2、error_reporting指令确定PHP错误报告敏感度的级别,一共有十三个预定的错误级别,每一个都唯一对应于应用程序或服务器功能。

D_BUG?error_reporting(7):error_reporting(E_ERROR);

1 E_ERROR 

2 E_WARNING 

4 E_PARSE 

8 E_NOTICE 

16 E_CORE_ERROR 

32 E_CORE_WARNING

3、$_SGLOBAL = $_SBLOCK   = $_SCONFIG = $_SHTML = $_DCACHE = $_SGET = array();

4、//基本文件

  1. if(!@include_once(S_ROOT.'./config.php')) { 
  2. header("Location: install/index.php");//安装 
  3. exit(); 
  4. include_once(S_ROOT.'./function/common.func.php'); 
  5. @include_once(S_ROOT.'./data/system/config.cache.php'); 

5、PHP extract() 函数从数组中把变量导入到当前的符号表中。

$_SCONFIG = array_merge($_SSCONFIG, $_SC);//合并配置

extract($_SC);

6、函数:get_magic_quotes_gpc()

取得 PHP 环境变量 magic_quotes_gpc 的值。

语法: long get_magic_quotes_gpc(void);

返回值: 长整数

函数种类: PHP 系统功能

本函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动转为含有反斜线的溢出字符。

7、过滤’单引号

  1. function saddslashes($string) { 
  2. if(is_array($string)) { 
  3. foreach($string as $key => $val) { 
  4. $string[$key] = saddslashes($val); 
  5. else { 
  6. $string = addslashes($string); 
  7. return $string

addslashes()函数的作用是:使用反斜线引用字符串。

8、strlen()函数的作用:取字符串的长度

9、

  1. foreach($_COOKIE as $key => $val) { 
  2. if(substr($key, 0, $prelength) == $_SC['cookiepre']) { 
  3. $_SCOOKIE[(substr($key$prelength))] = emptyempty($magic_quote) ? saddslashes($val) : $val

10、getenv

取得系统的环境变量

语法: string getenv(string varname);

11、php strcasecmp()函数

strcasecmp()函数的作用是:对两个字符串进行比较。

12、preg_match

13、ob_start 打开缓冲区

14、preg_replace执行正则表达式的搜索和替换

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论