开发者

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

I am suffering from a problem while i take the current url of the page and spliting them into parts and then checking for the index.php phrase.So far i have done this:

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

but its not working so 开发者_StackOverflow社区guys help me out and thank you in advance coz i know i will be satisfied by your answers.


Try this:

$pathToFile = $_SERVER['PHP_SELF'];

$currentFilename = substr($pathToFile, strrpos($pathToFile, '/') + 1);

if($currentFilename == 'index.php')
{
    echo 'This file is index.php!';
}
  1. $_SERVER['PHP_SELF'] is the path to the current file on the local system. Since you don't care about the domain name or the query string, this is easier.
  2. strrpos($pathToFile, '/') gets the index of the last occurrence of / in $pathToFile.
  3. substr($pathToFile, strrpos($pathToFile, '/') + 1) get the portion of $pathToFile starting with the character after the index found by strrpos() in step 2.
  4. You should be left with only the filename in $currentFilename, which you can compare with whatever you choose.

Note that this will match any index.php file, not just the one at your domain root. For example, if your site is located at http://example.com, http://example.com/subdir/index.php would also be true for $currentFilename == 'index.php'. If that's not what you want, you'd do it a little differently.


Use this:

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

I'm not sure what parse_url() is, but it didn't seem to do anything in your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜