简体   繁体   中英

linking css files in php

I am new to php and i am working on a project i am facing some problems with linking css and js files. Now when the project is executed,First index.php loads (located in root directory) which includes home.php and everything in it is executed correctly. But as soon as you click on 'about' link on the 'home page' ,the page is redirected to about.php( which is located inside Presentation/about.php )here the css and js linking fails.......
On execution of index.php it takes css link successfully (the link is as follows)
<link href="css/bootstrap-theme.min.css" rel="stylesheet">
but later when the page is redirected to about.php it expects the link to be <link href="../css/bootstrap-theme.min.css" rel="stylesheet">
and i am using a common file for these links, i kept all links in header.php(located in Presentation/Template/header.php)
I have also defined all paths in config.php(located in inc/config.php)
i have defined the path in config.php as follows

// SITE_ROOT contains the full path to the RichTongue folder
define('SITE_ROOT', dirname(dirname(__FILE__)));

// Application directories
define('PRESENTATION_DIR', SITE_ROOT . '/Presentation/');
define('TEMPLATE_DIR', PRESENTATION_DIR.'Template/');
define('BUSINESS_DIR', SITE_ROOT . '/Business/');
define('CSS_DIR', SITE_ROOT. '/css/');
define('FONT_DIR', SITE_ROOT . '/fonts/');
define('IMAGE', SITE_ROOT . '/images/');
define('JS', SITE_ROOT . '/js/');


so i tried linking css in the following way
<link href="<?php echo CSS_DIR ?>bootstrap.min.css" rel="stylesheet" >
now the above code gives me an error in chrome which says
Not allowed to load local resource: file:///C:/xampp/htdocs/RichTongue/css/bootstrap.min.css home.php:13


now the path in the error above(file:///C:/xampp/htdocs/RichTongue/css/bootstrap.min.css) is correct but it doesnt load the file and gives the error (error specified above)
Q)What should i do? what is the correct way of linking css and js files in php? need help

Link your css and other ressources with an absolute path seems to be the easy solution for you.

Absolute path is from the root dir of your website : C:/xampp/htdocs/RichTongue

instead of file:///C:/xampp/htdocs/RichTongue/css/bootstrap.min.css you can use /css/bootstrap.min.css

so you have to modify

define('CSS_DIR', SITE_ROOT. '/css/');

to

define('CSS_DIR', '/css/');

same for other ressources (js, font, etc.)

Exemple result :

<link href="/css/bootstrap-theme.min.css" rel="stylesheet">

Say if you have a folder called css you can use real path like this:

define('CSS_DIR', realpath(dirname(__FILE__).'/css'));

Then you would use that constant path like this:

echo CSS_DIR."/style.css"; 

set URl path for SITE_ROOT

if ur project path is C:/xampp/htdocs/RichTongue/css/bootstrap.min.css

so change with below line

define('SITE_ROOT', 'http://localhost/RichTongue');

Try to use it like that way:

 $SITE_FOLDER_NAME = "demo/"; define('SITEURL', 'http://'.$_SERVER['SERVER_NAME'].'/'.$SITE_FOLDER_NAME); define('SITEURL_CSS', SITEURL.'css/');

./assets/CSS/filename.css 为我完成了这项工作。

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