Home  >  Article  >  Backend Development  >  PHP gets the code reference of the current URL and page content

PHP gets the code reference of the current URL and page content

WBOY
WBOYOriginal
2016-07-25 08:57:29942browse
  1. /**

  2. * Get current URL
  3. * eidt by bbs.it-home.org
  4. */
  5. function get_url() {
  6. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  7. $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
  8. $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
  9. $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
  10. return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
  11. }

  12. //输出当前网址

  13. $url = get_url();
  14. echo $url;
  15. ?>

复制代码

2,获取页面内容的代码

  1. /**
  2. * Get page content
  3. * edit by bbs.it-home.org
  4. */
  5. function get_contents($url){
  6. if(function_exists('file_get_contents')){
  7. $file_contents = file_get_contents($url);
  8. }else{
  9. $ch = curl_init();
  10. $timeout = 5;
  11. curl_setopt ($ch, CURLOPT_URL, $url);
  12. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  13. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  14. $file_contents = curl_exec($ch);
  15. curl_close($ch);
  16. }

  17. //输出内容

  18. return $file_contents;
  19. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn