我需要爲我正在執行的腳本動態創建標題,標題應取決於當前正在使用的文件。不同的標題取決於什麼file.php當前正在使用
我的結構腳本是:
@require_once"bd.php";
@require_once"Funciones/functions.php";
include head.php;
include body.php;
include footer.php;
我的標題功能的代碼是從head.php
稱這是我的功能,但不工作總是返回空白的結果:■
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;
}
我呼叫功能:
<title><? echo get_title(); ?></title>
什麼是$ url實際返回?你有沒有測試過它返回的php文件名? –
是的$ url正確返回:index.php,post.php,search.php和register.php取決於正在使用的文件。 –
你應該做'strpos($ url,$ indexurl)!== FALSE',而不是假設它返回一個整數。因爲如果它的第一個出現strpos將返回0,那麼你的if語句轉換爲false。 – vinsanity38