douliaotong4944 2011-09-05 14:48
浏览 21

如何在PHP中获取当前包含脚本的URL?

I have a file called index.php which includes a class from another file (class/event.class.php) with the following code:

 <?php
      require_once('class/event.class.php');
      $evt = new Event;
      $evt->doSomething();
 ?>

The file event.class.php should be able to read the URL used in the request (not $_SERVER["SCRIPT_FILENAME") to the file.

ie: http://localhost:8888/class/event.class.php

How do I do that?

  • 写回答

3条回答 默认 最新

  • douguangxiang0363 2011-09-05 14:54
    关注

    Using the $_SERVER array, example:

    $protocol = 'http';
    $port = '';
    
    if ( isset( $_SERVER['HTTPS'] ) && strcasecmp( $_SERVER['HTTPS'], 'on' ) == 0 ) {
        $protocol = 'https';
    }
    if ( isset( $_SERVER['SERVER_PORT'] ) ) {
        $port = ':' . $_SERVER['SERVER_PORT'];
    }
    
    // Don't display the standard ports :80 for http and :443 for https
    if ( ( $protocol == 'http' && $port == ':80' ) || ( $protocol == 'https' && $port == ':443' ) ) {
        $port = '';
    }
    
    $host_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . $port;
    $base_url = rtrim( str_replace( basename( $_SERVER['SCRIPT_NAME'] ), '', $_SERVER['SCRIPT_NAME'] ), '/' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭