request_server.class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Provides access to the $_SERVER variable. Useful to have autocompletion working.
  4. * Access through the Request class.
  5. *
  6. * Example:
  7. *
  8. * Request :: server()-> request_uri()
  9. *
  10. *
  11. * @license see /license.txt
  12. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  13. */
  14. class RequestServer
  15. {
  16. public static function instance()
  17. {
  18. static $result = null;
  19. if (empty($result)) {
  20. $result = new self();
  21. }
  22. return $result;
  23. }
  24. function get($key, $default = null)
  25. {
  26. return isset($_SERVER[$key]) ? $_SERVER[$key] : null;
  27. }
  28. /**
  29. * The timestamp of the start of the request. Available since PHP 5.1.0.
  30. *
  31. * @return string
  32. */
  33. function request_time()
  34. {
  35. return isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : null;
  36. }
  37. /**
  38. * Contents of the Host: header from the current request, if there is one.
  39. *
  40. * @return string
  41. */
  42. function http_host()
  43. {
  44. return isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
  45. }
  46. /**
  47. * Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586). Among other things, you can use this value with get_browser() to tailor your page's output to the capabilities of the user agent.
  48. *
  49. * @return string
  50. */
  51. function http_user_agent()
  52. {
  53. return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  54. }
  55. /**
  56. * Contents of the Accept: header from the current request, if there is one.
  57. *
  58. * @return string
  59. */
  60. function http_accept()
  61. {
  62. return isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : null;
  63. }
  64. /**
  65. * Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'.
  66. *
  67. * @return string
  68. */
  69. function http_accept_language()
  70. {
  71. return isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null;
  72. }
  73. /**
  74. * Contents of the Accept-Encoding: header from the current request, if there is one. Example: 'gzip'.
  75. *
  76. * @return string
  77. */
  78. function http_accept_encoding()
  79. {
  80. return isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : null;
  81. }
  82. /**
  83. * Contents of the Connection: header from the current request, if there is one. Example: 'Keep-Alive'.
  84. *
  85. * @return string
  86. */
  87. function http_connection()
  88. {
  89. return isset($_SERVER['HTTP_CONNECTION']) ? $_SERVER['HTTP_CONNECTION'] : null;
  90. }
  91. function http_cookie()
  92. {
  93. return isset($_SERVER['HTTP_COOKIE']) ? $_SERVER['HTTP_COOKIE'] : null;
  94. }
  95. function http_cache_control()
  96. {
  97. return isset($_SERVER['HTTP_CACHE_CONTROL']) ? $_SERVER['HTTP_CACHE_CONTROL'] : null;
  98. }
  99. function path()
  100. {
  101. return isset($_SERVER['PATH']) ? $_SERVER['PATH'] : null;
  102. }
  103. function systemroot()
  104. {
  105. return isset($_SERVER['SystemRoot']) ? $_SERVER['SystemRoot'] : null;
  106. }
  107. function comspec()
  108. {
  109. return isset($_SERVER['COMSPEC']) ? $_SERVER['COMSPEC'] : null;
  110. }
  111. function pathext()
  112. {
  113. return isset($_SERVER['PATHEXT']) ? $_SERVER['PATHEXT'] : null;
  114. }
  115. function windir()
  116. {
  117. return isset($_SERVER['WINDIR']) ? $_SERVER['WINDIR'] : null;
  118. }
  119. function server_signature()
  120. {
  121. return isset($_SERVER['SERVER_SIGNATURE']) ? $_SERVER['SERVER_SIGNATURE'] : null;
  122. }
  123. /**
  124. * Server identification string, given in the headers when responding to requests.
  125. *
  126. * @return string
  127. */
  128. function server_software()
  129. {
  130. return isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : null;
  131. }
  132. /**
  133. * The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.
  134. *
  135. * @return string
  136. */
  137. function server_name()
  138. {
  139. return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null;
  140. }
  141. /**
  142. * The IP address of the server under which the current script is executing.
  143. *
  144. * @return string
  145. */
  146. function server_addr()
  147. {
  148. return isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : null;
  149. }
  150. function server_port()
  151. {
  152. return isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null;
  153. }
  154. /**
  155. * The IP address from which the user is viewing the current page.
  156. *
  157. * @return string
  158. */
  159. function remote_addr()
  160. {
  161. return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
  162. }
  163. /**
  164. * The document root directory under which the current script is executing, as defined in the server's configuration file.
  165. * @return string
  166. */
  167. function document_root()
  168. {
  169. return isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : null;
  170. }
  171. /**
  172. * The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host.
  173. *
  174. * @return string
  175. */
  176. function server_admin()
  177. {
  178. return isset($_SERVER['SERVER_ADMIN']) ? $_SERVER['SERVER_ADMIN'] : null;
  179. }
  180. /**
  181. * The absolute pathname of the currently executing script.
  182. *
  183. * Note:
  184. *
  185. * If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.
  186. *
  187. * @return string
  188. */
  189. function script_filename()
  190. {
  191. return isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : null;
  192. }
  193. /**
  194. * The port being used on the user's machine to communicate with the web server.
  195. *
  196. * @return string
  197. */
  198. function remote_port()
  199. {
  200. return isset($_SERVER['REMOTE_PORT']) ? $_SERVER['REMOTE_PORT'] : null;
  201. }
  202. /**
  203. * What revision of the CGI specification the server is using; i.e. 'CGI/1.1'.
  204. *
  205. * @return string
  206. */
  207. function gateway_interface()
  208. {
  209. return isset($_SERVER['GATEWAY_INTERFACE']) ? $_SERVER['GATEWAY_INTERFACE'] : null;
  210. }
  211. /**
  212. * Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0';
  213. *
  214. * @return string
  215. */
  216. function server_protocol()
  217. {
  218. return isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : null;
  219. }
  220. /**
  221. * Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'.
  222. *
  223. * Note:
  224. * PHP script is terminated after sending headers (it means after producing any output without output buffering) if the request method was HEAD.
  225. *
  226. * @return string
  227. */
  228. function request_method()
  229. {
  230. return isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;
  231. }
  232. /**
  233. * The query string, if any, via which the page was accessed.
  234. *
  235. * @return string
  236. */
  237. function query_string()
  238. {
  239. return isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
  240. }
  241. /**
  242. * The URI which was given in order to access this page; for instance, '/index.html'.
  243. * @return string
  244. */
  245. function request_uri()
  246. {
  247. return isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
  248. }
  249. /**
  250. * Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.
  251. *
  252. * @return string
  253. */
  254. function script_name()
  255. {
  256. return isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : null;
  257. }
  258. /**
  259. * The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.
  260. *
  261. * @return string
  262. */
  263. function php_self()
  264. {
  265. return isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : null;
  266. }
  267. }