forked from BIN-YEE/bae_tuchuang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.inc.php
119 lines (94 loc) · 3.23 KB
/
common.inc.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('asia/chongqing');
require_once 'conf.inc.php';
/*从环境变量里取出数据库连接需要的参数*/
$host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');
$port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');
$user = getenv('HTTP_BAE_ENV_AK');
$pwd = getenv('HTTP_BAE_ENV_SK');
$mysql = @new mysqli($host, $user, $pwd, DBNAME, $port);
if($mysql->connect_errno) {
die("Connect Server Failed: " . $mysql->connect_error);
}
$mysql->query("SET NAMES 'utf8'");
$mysql->query("SET CHARACTER_SET_CLIENT=utf8");
$mysql->query("SET CHARACTER_SET_RESULTS=utf8");
$CurrUser =array();
if(isset($_COOKIE['auth']))
$CurrUser = (array)json_decode(base64_decode(urldecode($_COOKIE['auth'])));
function shutdown(){
global $link;
if(is_resource($link))
$link->close();
}
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_CLIENTIP"])){
$realip = $_SERVER["HTTP_CLIENTIP"];
} else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$realip = explode(',',$_SERVER["HTTP_X_FORWARDED_FOR"]);
$realip = $realip[0];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
$realip = explode(',',$realip);
$realip = $realip[0];
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
function genRandomString($len=10)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars); // 将数组打乱
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
function counter_incr($countname){
require_once ( "BaeCounter.class.php" ) ;
$cr = new BaeCounter();
if(!$cr->isExist($countname))
$cr->register($countname);
$cr->increase($countname,1);
}
function counter_decr($countname){
require_once ( "BaeCounter.class.php" ) ;
$cr = new BaeCounter();
if(!$cr->isExist($countname))
return;
$cr->get($countname) > 0 && $cr->decrease($countname);
}
function counter_get($countname){
require_once ( "BaeCounter.class.php" ) ;
$cr = new BaeCounter();
return $cr->isExist($countname) ? $cr->get($countname) : 0;
}
function setupSize( $fileSize ) {
$size = sprintf ( " %u " , $fileSize );
$sizename = array ( " Bytes " , " KB " , " MB " , " GB " , " TB " , " PB " , " EB " , " ZB " , " YB " );
return round ( $size / pow ( 1024 , ( $i = floor ( log ( $size , 1024 )))) , 3 ) . $sizename [ $i ];
}
register_shutdown_function('shutdown');
?>