简体   繁体   中英

header.php not displaying properly from root folder homepage

I have the index.php in my public_ root destination which has the header taken using php include from the folder include/header.php. But when the index page loads the menu and images within the header.php doesnt load because its directory is from include folder.

Ex in header.php the menu will be in directory

        <link rel="stylesheet" href="../scripts/menu.css" type="text/css" media="all" />

but the index.php directory to menu stylesheet would be scripts/menu.css. So i guess the menu styles are not loading.

Is there a way to get the index page right without having to have header in the same folder as index.php or is it because i am testing this on windows using xampp?

Let me clear this one up for you.

When you write HTML and CSS from PHP like you are doing here, the HTML and CSS don't know where the PHP is being run from or anything about the PHP. This is because PHP is interpreted and writes out the page into HTML which is then processed in your browser (ie the PHP never reaches your browser, all it sees is HTML).

This means that you must add the correct paths to the CSS files relative to your web server root not the PHP script location (in your case that is probably "/scripts/menu.css" for the example you've provided - assuming that the scripts is in the root web folder).

So for example, on your website / means the root of the web server (ie mywebsite.com/) and anything after that is it's path relative to the root of the web server.

By using /mycssfolder/mycssfile.css like I suggested, you can ensure that the location is correct regardless of the page which the PHP script is called on.


Hope this helps.

Ben

<link rel="stylesheet" href="/scripts/menu.css" type="text/css" media="all" />

You might try simple removing the .., like shown above. Remember, any external source file that you include becomes part of the destination. Thus, think of the location of your CSS file relative to index.php.

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