简体   繁体   中英

Different titles depending what file.php is currently in use

I need to create titles dynamically for a script that I'm doing, the title should be depending on the file that is in use currently.

My structure script is:

@require_once"bd.php";
@require_once"Funciones/functions.php";
include head.php;
include body.php;
include footer.php;

My title function code is called from head.php

this is my function, but not works always returns blank result :s

function get_title(){
    $indexurl = "index.php";
    $threadurl = "post.php";
    $searchurl = "search.php";
    $registerurl = "register.php";

    $query = $_SERVER['PHP_SELF'];
    $path = pathinfo( $query );
    $url = $path['basename']; //This returns the php file that is being used

    if(strpos($url,$indexurl)) {
      $title = "My home title";
    }
    if(strpos($url,$threadurl)) {
      $title = "My post title";
    }
    if(strpos($url,$searchurl)) {
      $title = "My search title";
    }
    if(strpos($url,$registerurl)) {
      $title = "My register page title";
    }

return $title;
}

Im call the function:

<title><? echo get_title(); ?></title>

A better approach can be found here as a I said in my initial comment: https://stackoverflow.com/a/4858950/1744357

You should use the identity property with strpos and test against FALSE.

if (strpos($link, $searchterm) !== false) {
  //do stuff here
}

I found the problem:

$string = "This is a strpos() test";

if(strpos($string, "This)) {
   echo = "found!";
}else{
   echo = "not found";
}

If you try executing that, you will find that it outputs "Not found", despite "This" quite clearly being in $string. Is it another case sensitivity problem? Not quite. This time the problem lies in the fact that "This" is the first thing in $string, which means that strpos() will return 0. However, PHP considers 0 to be the same value as false, which means that our if statement cannot tell the difference between "Substring not found" and "Substring found at index 0" - quite a problem!.

So, The correct way to use strpos in my case is removing the first character from $indexurl,$threadurl,$searchurl and $registerurl

function get_title(){
    $indexurl = "ndex.php";
    $threadurl = "ost.php";
    $searchurl = "earch.php";
    $registerurl = "egister.php";

    $query = $_SERVER['PHP_SELF'];
    $path = pathinfo( $query );
    $url = $path['basename']; //This returns the php file that is being used

    if(strpos($url,$indexurl)) {
      $title = "My home title";
    }
    if(strpos($url,$threadurl)) {
      $title = "My post title";
    }
    if(strpos($url,$searchurl)) {
      $title = "My search title";
    }
    if(strpos($url,$registerurl)) {
      $title = "My register page title";
    }

return $title;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM