SauloA Posted February 18, 2010 Share Posted February 18, 2010 I have a header, like so: header.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Website Page Title</title> <link href="master.css" rel="stylesheet" type="text/css" /> </head> <body> a footer like so: footer.php </body> </html> and several pages use the header and footer like so: index.php, page1.php, and page2.php <?php include("header.php"); ?> <p>Page Content</p> <?php include("footer.php"); ?> The only problem is that I can't change the page <title> for each page. I tried using this code to change the page <title>: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <?php $title; switch($_SERVER['PHP_SELF']) { case 'index.php': $title = 'Welcome to the Home Page'; break; case 'page1.php': $title = 'This is the Page One'; break; case 'page2.php': $title = 'This is the Second Page'; break; } echo '<title>'.$title.'</title>'; ?> <link href="master.css" rel="stylesheet" type="text/css" /> </head> <body> I get no errors but the title always comes out blank. Is there a better solution? or is this the code I should be using? Quote Link to comment https://forums.phpfreaks.com/topic/192475-how-do-i-change-the-page-dynamically-with-php/ Share on other sites More sharing options...
sader Posted February 18, 2010 Share Posted February 18, 2010 it's because $_SERVER['PHP_SELF'] is not what u expect for. Echo $_SERVER['PHP_SELF'] and u will see that it looks smthg like "/localhost/folder/index.php" Quote Link to comment https://forums.phpfreaks.com/topic/192475-how-do-i-change-the-page-dynamically-with-php/#findComment-1014165 Share on other sites More sharing options...
teamatomic Posted February 18, 2010 Share Posted February 18, 2010 You are returning more than the filename, to get just the filename you must breal it apart. The best way when the return may be ant number of folders deep is to use php's basename() function. $path=$_SERVER['PHP_SELF']; $page=basename($path); switch("$page") HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192475-how-do-i-change-the-page-dynamically-with-php/#findComment-1014166 Share on other sites More sharing options...
khr2003 Posted February 18, 2010 Share Posted February 18, 2010 one way is to include it into a function like this: function header($title) { echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>.'$title'.</title> <link href="master.css" rel="stylesheet" type="text/css" /> </head> <body>'; } then you can use the function anywhere you like: header('This is page One'); header('This is page Two'); header('This is page Three'); and so on Quote Link to comment https://forums.phpfreaks.com/topic/192475-how-do-i-change-the-page-dynamically-with-php/#findComment-1014167 Share on other sites More sharing options...
SauloA Posted February 18, 2010 Author Share Posted February 18, 2010 I ended up using the code from teamatomic. I received no errors and the code works fine: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <?php $title; $path=$_SERVER['PHP_SELF']; $page=basename($path); switch("$page") { case 'index.php': $title = 'Welcome to the Home Page'; break; case 'page1.php': $title = 'This is the Page One'; break; case 'page2.php': $title = 'This is the Second Page'; break; } echo '<title>'.$title.'</title>'; ?> <link href="master.css" rel="stylesheet" type="text/css" /> </head> <body> <link href="master.css" rel="stylesheet" type="text/css" /> </head> <body> Thanks everybody. This is officially solved. Quote Link to comment https://forums.phpfreaks.com/topic/192475-how-do-i-change-the-page-dynamically-with-php/#findComment-1014179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.