查找当前url并将其分成几个部分,并检查url中是否存在index.php


finding current url and splitting them into parts and checking if there is index.php or not in the url

我遇到了一个问题,而我采取页面的当前url并将它们分成部分,然后检查index.php短语。到目前为止,我已经这样做了:

  <?php
      $domain=$_SERVER['REQUEST_URI'];
      $values = parse_url($domains);
  $path = explode('/',$values['path']);
  if(array_search($path[2], "index.php"))
        {
          echo "hello";
        }
  ?>

但它不起作用,所以大家帮帮我,提前感谢你们,因为我知道我会对你们的答案感到满意。

试试这个:

$pathToFile = $_SERVER['PHP_SELF'];
$currentFilename = substr($pathToFile, strrpos($pathToFile, '/') + 1);
if($currentFilename == 'index.php')
{
    echo 'This file is index.php!';
}
  1. $_SERVER['PHP_SELF']是本地系统上当前文件的路径。因为你不关心域名或查询字符串,这是更容易的。
  2. strrpos($pathToFile, '/')得到/$pathToFile中最后一次出现的索引。
  3. substr($pathToFile, strrpos($pathToFile, '/') + 1)获得$pathToFile的部分,从strrpos()在步骤2中找到的索引后面的字符开始。
  4. 您应该只留下$currentFilename中的文件名,您可以与您选择的任何内容进行比较。

注意,这将匹配任何 index.php文件,而不仅仅是域根目录下的那个。例如,如果您的站点位于http://example.com,那么http://example.com/subdir/index.php对于$currentFilename == 'index.php'也将为真。如果这不是你想要的,你可以做一些不同的。

使用

$domain=$_SERVER['REQUEST_URI'];
$path = explode('/',$domain);
if(array_search($path[2], "index.php"))
    {
      echo "hello";
    }

我不确定parse_url()是什么,但它似乎没有在你的代码做任何事情。