dongxuan1660 2015-09-07 17:33 采纳率: 100%
浏览 29
已采纳

使用php重定向到相同的路径减去文件名

So I want to redirect a page in a directory, for example auth/login.php, to another page, at auth/. The page is actually called auth/index.php but I don't want this to show up to the client. How can I do this using this PHP code:

header("Location: /auth/");

However, I want the filename to be relative, e.g. ../ instead of hard-coded as auth. Is this possible, and how?

  • 写回答

1条回答 默认 最新

  • douxin1956 2015-09-07 18:20
    关注

    To direct the users to your current directory, you should extract the script's directory from $_SERVER['SCRIPT_NAME'], and then compare it to the actual HTTP request ($_SERVER['REQUEST_URI']). If they're not the same - redirect to the current directory. If they're the same - you should start your actual script:

    <?php
    
    $currentScript = $_SERVER['SCRIPT_NAME'];
    $pathInfo = pathinfo($currentScript);
    $currentDir = $pathInfo['dirname'].'/';
    
    if ($currentDir != $_SERVER['REQUEST_URI'])
    {
      header('Location: '.$currentDir);
      return ;
    }
    
    // Here be real site data
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算