Home:ALL Converter>include header php inside every file

include header php inside every file

Ask Time:2012-08-13T22:11:05         Author:Pacuraru Daniel

Json Formatter

i am trying to include a php header inside each of the pages i make. Now as far as i know, it can be done by using <?php include('header.php'); ?>inside each file.

Now my problem is that i have 5 pages, and in the header php i have a nav which links to each of this pages. When i load the header i would like to change the link of the file which the header is loaded on.

Ex: if i open the Home page, i want the header to have the link inside the header nav bolded. That would probably require me to send a variable to the header file when i include it or smt? if so, than how to do that ? :D

Thank you in advance, Daniel!

Author:Pacuraru Daniel,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11936064/include-header-php-inside-every-file
sachleen :

You can either do this by setting a variable before including the header or by looking at the URL.\n\nA simplified example:\n\n$page = \"home\";\ninclude \"header.php\"\n\n\nIn header.php\n\nif ($page == \"home\") { ... }\n\n\nThe following code will give you the file name from the URL\n\n$basename = basename($_SERVER['REQUEST_URI']);\n\n\nSo if you have http://mysite.com/index.php it'll give you index.php\n\nYou can use this in your header.php similarly to how I described above to decide which link to bold.",
2012-08-13T14:15:14
Kuba Wyrostek :

You can always use $_SERVER['SCRIPT_NAME'] to obtain the name of the most outer php file that was requested. Than you can change style of link in navigation accordingly, ie:\n\n <a href=\"some.php\" style=\"font-weight: <?= $_SERVER[\"SCRIPT_NAME\"] == '/some.php' ? 'bold' : 'normal' ?>\">Link to Some</a>\n <a href=\"other.php\" style=\"font-weight: <?= $_SERVER[\"SCRIPT_NAME\"] == '/other.php' ? 'bold' : 'normal' ?>\">Link to Other</a>\n",
2012-08-13T14:15:09
Jubin Thomas :

In below code, you can call this class to other pages and pass the page name as parameters and can call a jQuery function with that very easily\n\n<?php\nclass header {\nfunction __construct($title = \"Home\", $page = 'home') {\n ?>\n <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n <html xmlns=\"http://www.w3.org/1999/xhtml\">\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <title><?php echo $title; ?></title>\n <script type=\"text/javascript\" src=\"js/jquery.js\"></script>\n <script type=\"text/javascript\" src=\"js/script.js\"></script>\n <script>\n $(document).ready(function(){\n $('.<?php echo $page ?>').addClass('active');\n }); \n </script> \n </head>\n<body>\n <ul class=\"nav\">\n <li class=\"home\"><a href=\"index.php\"><br />HOME<br /></a></li>\n <li class=\"about\"><br />ABOUT US<br /></li>\n </ul>\n<?php }} ?>\n",
2012-08-13T14:21:21
yy