当前位置:Gxlcms > PHP教程 > php获取当前网址与页面内容的代码参考

php获取当前网址与页面内容的代码参考

时间:2021-07-01 10:21:17 帮助过:26人阅读

  1. /**

  2. * 得到当前网址
  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. * 得到页面内容
  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. ?>

人气教程排行