27
05/2014
php学习笔记
1、$_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest' 判断请求是不是ajax请求
2、获得当前服务器的主机名:
function extract_current_hostname() { global $config; //全局配置 // Get hostname $host = (! empty ( $_SERVER ['HTTP_HOST'] )) ? $_SERVER ['HTTP_HOST'] : ((! empty ( $_SERVER ['SERVER_NAME'] )) ? $_SERVER ['SERVER_NAME'] : getenv ( 'SERVER_NAME' )); // Should be a string and lowered $host = ( string ) strtolower ( $host ); // If host is equal the cookie domain or the server name (if config is set), then we assume it is valid if ((isset ( $config ['cookie_domain'] ) && $host === $config ['cookie_domain']) || (isset ( $config ['server_name'] ) && $host === $config ['server_name'])) { return $host; } // Is the host actually a IP? If so, we use the IP... (IPv4) if (long2ip ( ip2long ( $host ) ) === $host) { return $host; } // Now return the hostname (this also removes any port definition). The http:// is prepended to construct a valid URL, hosts never have a scheme assigned $host = @parse_url ( 'http://' . $host ); $host = (! empty ( $host ['host'] )) ? $host ['host'] : ''; // Remove any portions not removed by parse_url (#) $host = str_replace ( '#', '', $host ); // If, by any means, the host is now empty, we will use a "best approach" way to guess one if (empty ( $host )) { if (! empty ( $config ['server_name'] )) { $host = $config ['server_name']; } else if (! empty ( $config ['cookie_domain'] )) { $host = (strpos ( $config ['cookie_domain'], '.' ) === 0) ? substr ( $config ['cookie_domain'], 1 ) : $config ['cookie_domain']; } else { // Set to OS hostname or localhost $host = (function_exists ( 'php_uname' )) ? php_uname ( 'n' ) : 'localhost'; } } // It may be still no valid host, but for sure only a hostname (we may further expand on the cookie domain... if set) return $host; }
3、$_SERVER ['PHP_SELF'] 获得当前脚本文件的绝对路径
4、$_SERVER ['QUERY_STRING'] 查询字符串
5、$_SERVER ['REQUEST_URI'] 获得当前脚本文件的路径及后面的查询参数组成的字符串
6、$_SERVER["HTTP_X_FORWARDED_FOR"] 获得代理服务器的服务器名以及端口,可能有很多,以“,”分割
7、$_SERVER ['REQUEST_METHOD'] 获得访问页面时的请求方法。例如:“GET”、“HEAD”、“POST”、“PUT”
0 条评论