全局变量
$starttime
$time
$conf
$ip
$longip
$ajax
$method
$db
$cache
$errno
$errstr
$starttime
脚本执行的开始时间,单位为毫秒,格式:1448335223.1877 。
一般用来计算程序执行时间。
【定义】 文件:xiunophp/xiunophp.php 大约 40 行:
$starttime = microtime(1);
【注意】
XiunoPHP 在被 include 后,会初始化十几个常用的全局变量,请注意命名不要冲突。
$time
脚本执行的开始时间,单位为秒,格式:1448336550 。
通用的 UNIX 时间戳。
【定义】 文件:xiunophp/xiunophp.php 大约 41 行:
$time = time();
$conf
应用的全局配置,格式为数组,一般通过配置文件包含返回。
非强制要求,开发较大型的项目时建议设置。
【定义】 文件:xiunophp/xiunophp.php 大约 63 行:
empty($conf) AND $conf = array("db"=>array(), "cache"=>array(), "tmp_path"=>"./", "log_path"=>"./", "timezone"=>"Asia/Shanghai");
【用例】
【conf.php】
<?php
```
```
//xiunophp 依赖的配置
"db"=>array(
"type"=>"pdo_mysql",
"pdo_mysql" => array (
"master" => array (
"host" => "localhost",
"user" => "root",
"password" => "root",
"name" => "test",
"charset" => "utf8",
"tablepre"=>"bbs_",
"engine"=>"myisam", // innodb
),
"slaves" => array()
)
),
"tmp_path" => "./", // 可以配置为 linux 下的 /dev/shm ,通过内存缓存临时文件
"log_path" => "./"
);
?>
$ip
客户端的 IP 地址,格式为:202.106.0.20 。
在开启 CDN 后,它会获取 CDN 转发过来的 IP 。
【定义】 文件:xiunophp/misc.func.php:
$ip = ip();
【用例】
【ip()】
// 不安全的获取 IP 方式,在开启 CDN 的时候,如果被人猜到真实 IP,则可以伪造。
function ip() {
global $conf;
$ip = "127.0.0.1";
if(empty($conf["cdn_on"])) {
$ip = $_SERVER["REMOTE_ADDR"];
} else {
if(isset($_SERVER["HTTP_CDN_SRC_IP"])) {
$ip = $_SERVER["HTTP_CDN_SRC_IP"];
} elseif(isset($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
} elseif(isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
$arr = array_filter(explode(",", $ip));
$ip = end($arr);
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
}
return long2ip(ip2long($ip));
}
$longip
客户端的 IP 地址的 long 格式:2130706433 。
一般用 4 个字节的 unsigned int 记录到数据库。
【定义】 文件:xiunophp/xiunophp.php 大约 50 行:
$longip = ip2long($ip);
$ajax
是否为 AJAX 请求,值为 TRUE/FALSE 。
浏览器在发送 AJAX 请求的时候,会发送特定的头信息,会被当做判断依据。
【定义】 文件:xiunophp/xiunophp.php 大约 55 行:
$ajax = isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower(trim($_SERVER["HTTP_X_REQUESTED_WITH"])) == "xmlhttprequest";
$method
判断 HTTP 请求的方法 ,默认值为 GET。
【定义】 文件:xiunophp/xiunophp.php 大约 77 行:
$method = $_SERVER["REQUEST_METHOD"];
$db
默认数据库实例
如果配置文件设置了数据库相关的配置,则框架会自动实例化一个 DB 类。
一般不用直接使用这个变量,除非在升级,转换需要多个连接的时候。
一般用 db_find(), db_find_one(), db_exec(), db_count() 函数来操作数据库。
【定义】 文件:xiunophp/xiunophp.php 大约 76 行:
$db = !empty($conf["db"]) ? db_new($conf["db"]) : NULL;
【用例】
find_one("SELECT * FROM bbs_user LIMIT 1");
【conf.php】
// xiunophp 依赖的配置
"db"=>array(
"type"=>"pdo_mysql",
"pdo_mysql" => array (
"master" => array (
"host" => "localhost",
"user" => "root",
"password" => "root",
"name" => "test",
"charset" => "utf8",
"engine"=>"myisam", // innodb
),
"slaves" => array()
)
),
"tmp_path" => "./", // 可以配置为 linux 下的 /dev/shm ,通过内存缓存临时文件
"log_path" => "./"
);
$cache
默认缓存实例
如果配置文件设置了缓存相关的配置,则框架会自动实例化一个 Cache 类。
一般不用直接使用这个变量,除非在升级,转换需要多个连接的时候。
一般用 cache_set(), cache_get() 函数来操作缓存。
使用时,需要配置好 Cache 服务和 PHP 相关的 Cache 扩展,目前支持:apc|xcache|memcached|redis|mysql 。
如果为单机,建议使用 xcache,速度比较快,相关文档:http://bbs.xiuno.com/thread-8762.htm 。
【定义】 文件:xiunophp/xiunophp.php 大约 77 行:
$cache = !empty($conf["cache"]) ? cache_new($conf["cache"]) : NULL;
【用例】
set("key1", "value1");
$cache->get("key1");
【conf.php】
//xiunophp 依赖的配置
"cache"=> array(
"enable" => TRUE,
"type"=> "xcache", // apc|xcache|memcached|redis|mysql
"memcached"=> array (
"host"=>"localhost",
"port"=>"11211",
),
"redis"=> array (
"host"=>"localhost",
"port"=>"6379",
),
),
"tmp_path" => "./", // 可以配置为 linux 下的 /dev/shm ,通过内存缓存临时文件
"log_path" => "./"
);
$errno
错误号,为 0 时,表示正常,非 0 时表示错误。
在某些函数被调用后发生错误,该全局变量会被设置,有点类似于 Linux C 的 errno 。
因为 PHP 是同步的(区别于异步)并且线程安全的,所以使用这种方法来返回错误会很方便。
一般配合 errstr 一起使用,函数内通过 xn_error($errno, $errstr) 设置错误。
相比起抛出异常来,这种处理方式会轻量级很多。
【定义】 文件:xiunophp/xiunophp.php 大约 58 行:
$errno = 0;
【相关函数】
xn_error()
$errstr
错误字符串,与 $errno 配合使用。
【定义】 文件:xiunophp/xiunophp.php 大约 58 行:
$errstr = "";
【相关函数】
xn_error()