以用戶登錄為例介紹,其它注銷,改密碼,消息,頭像,好友均類同.
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言聽計從,讓干什么就干什么?,F在,收到讓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
- //系統頻道
- if($_SGET['action'] != 'index') {
- if(emptyempty($channels['menus'][$_SGET['action']]['upnameid']) && $channels['menus'][$_SGET['action']]['upnameid'] != 'news') {
- $scriptfile = S_ROOT.'./'.$_SGET['action'].'.php';
- } else {
- $scriptfile = S_ROOT.'./news.php';
- }
- //echo $scriptfile;
- if(file_exists($scriptfile)) {
- include_once($scriptfile);
- 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進行同步,然后同步其他子系統登錄信息:
- $msg = $lang['login_succeed'].uc_user_synlogin($members['uid']);
- function uc_user_synlogin($uid) {
- $uid = intval($uid);
- $return = uc_api_post('user', 'synlogin', array('uid'=>$uid));
- return $return;
- }
- function uc_api_post($module, $action, $arg = array()) {
- $s = $sep = '';
- foreach($arg as $k => $v) {
- $k = urlencode($k);
- if(is_array($v)) {
- $s2 = $sep2 = '';
- foreach($v as $k2 => $v2) {
- $k2 = urlencode($k2);
- $s2 .= "$sep2{$k}[$k2]=".urlencode(uc_stripslashes($v2));
- $sep2 = '&';
- }
- $s .= $sep.$s2;
- } else {
- $s .= "$sep$k=".urlencode(uc_stripslashes($v));
- }
- $sep = '&';
- }
- $postdata = uc_api_requestdata($module, $action, $s);
- return uc_fopen2(UC_API.'/index.php', 500000, $postdata, '', TRUE, UC_IP, 20);
- }
- function uc_api_requestdata($module, $action, $arg='', $extra='') {
- $input = uc_api_input($arg);
- $post = "m=$module&a=$action&inajax=2&release=".UC_CLIENT_RELEASE."&input=$input&appid=".UC_APPID.$extra;
- return $post;
- }
- function uc_api_url($module, $action, $arg='', $extra='') {
- $url = UC_API.'/index.php?'.uc_api_requestdata($module, $action, $arg, $extra);
- return $url;
- }
- function uc_api_input($data) {
- $s = urlencode(uc_authcode($data.'&agent='.md5($_SERVER['HTTP_USER_AGENT'])."&time=".time(), 'ENCODE', UC_KEY));
- return $s;
- }
- function uc_fopen2($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
- $__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1;
- if($__times__ > 2) {
- return '';
- }
- $url .= (strpos($url, '?') === FALSE ? '?' : '&')."__times__=$__times__";
- return uc_fopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block);
- }
- function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
- $return = '';
- $matches = parse_url($url);
- !isset($matches['host']) && $matches['host'] = '';
- !isset($matches['path']) && $matches['path'] = '';
- !isset($matches['query']) && $matches['query'] = '';
- !isset($matches['port']) && $matches['port'] = '';
- $host = $matches['host'];
- $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
- $port = !emptyempty($matches['port']) ? $matches['port'] : 80;
- if($post) {
- $out = "POST $path HTTP/1.0/r/n";
- $out .= "Accept: **/r/n";
- //$out .= "Referer: $boardurl/r/n";
- $out .= "Accept-Language: zh-cn/r/n";
- $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]/r/n";
- $out .= "Host: $host/r/n";
- $out .= "Connection: Close/r/n";
- $out .= "Cookie: $cookie/r/n/r/n";
- }
- $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
- if(!$fp) {
- return '';//note $errstr : $errno /r/n
- } else {
- stream_set_blocking($fp, $block);
- stream_set_timeout($fp, $timeout);
- @fwrite($fp, $out);
- $status = stream_get_meta_data($fp);
- if(!$status['timed_out']) {
- while (!feof($fp)) {
- if(($header = @fgets($fp)) && ($header == "/r/n" || $header == "/n")) {
- break;
- }
- }
- $stop = false;
- while(!feof($fp) && !$stop) {
- $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
- $return .= $data;
- if($limit) {
- $limit -= strlen($data);
- $stop = $limit <= 0;
- }
- }
- }
- @fclose($fp);
- return $return;
- }
- }
遠程同步登錄子系統操作之后:
- //顯示信息
- function showmessage($message, $url_forward='', $second=3, $vars=array()) {
- global $_SGLOBAL, $_SCONFIG, $_SC, $channels;
- if(emptyempty($_SGLOBAL['inajax']) && $url_forward && emptyempty($second)) {
- //直接301跳轉
- obclean();
- header("HTTP/1.1 301 Moved Permanently");
- header("Location: $url_forward");
- } else {
- if(!defined('IN_SUPESITE_ADMINCP')) {
- $tpl_file = 'showmessage';
- $fullpath = 0;
- include_once(S_ROOT.'./language/message.lang.php');
- if(!emptyempty($mlang[$message])) $message = $mlang[$message];
- } else {
- $tpl_file = 'admin/tpl/showmessage.htm';
- $fullpath = 1;
- include_once(S_ROOT.'./language/admincp_message.lang.php');
- if(!emptyempty($amlang[$message])) $message = $amlang[$message];
- }
- if(isset($_SGLOBAL['mlang'][$message])) $message = $_SGLOBAL['mlang'][$message];
- foreach ($vars as $key => $val) {
- $message = str_replace('{'.$key.'}', $val, $message);
- }
- //顯示
- obclean();
- if(!emptyempty($url_forward)) {
- $second = $second * 1000;
- $message .= "<script>setTimeout(/"window.location.href ='$url_forward';/", $second);</script><ajaxok>";
- }
- include template($tpl_file, $fullpath);
- ob_out();
- }
- 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、//基本文件
- if(!@include_once(S_ROOT.'./config.php')) {
- header("Location: install/index.php");//安裝
- exit();
- }
- include_once(S_ROOT.'./function/common.func.php');
- @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、過濾’單引號
- function saddslashes($string) {
- if(is_array($string)) {
- foreach($string as $key => $val) {
- $string[$key] = saddslashes($val);
- }
- } else {
- $string = addslashes($string);
- }
- return $string;
- }
addslashes()函數的作用是:使用反斜線引用字符串。
8、strlen()函數的作用:取字符串的長度
9、
- foreach($_COOKIE as $key => $val) {
- if(substr($key, 0, $prelength) == $_SC['cookiepre']) {
- $_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執行正則表達式的搜索和替換
新聞熱點
疑難解答