0

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

Pushkarraj Pujari
  • 666
  • 11
  • 25

5 Answers5

0

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">
Apolo
  • 3,844
  • 1
  • 21
  • 51
0

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"; 
meda
  • 45,103
  • 14
  • 92
  • 122
0

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');
Wiram Rathod
  • 1,895
  • 1
  • 19
  • 41
  • but this may cause a problem when hosting the site on a web server ? – Pushkarraj Pujari Dec 30 '14 at 13:21
  • this should work as long as the website is in the folder "RichTongue" on the webserver (in fact you can route, but keep it simple) – Apolo Dec 30 '14 at 13:23
  • yes its quick fixing bro, yo need to change while you host your site with domain URI – Wiram Rathod Dec 30 '14 at 13:25
  • yes i tried it but i get an error which says': require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in ' – Pushkarraj Pujari Dec 30 '14 at 13:31
  • some php file not include correctly refer this link might be useful to you http://stackoverflow.com/questions/23285503/warning-require-once-http-wrapper-is-disabled-in-the-server-configuration – Wiram Rathod Dec 30 '14 at 13:36
0

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/');
Lakhan
  • 12,328
  • 3
  • 19
  • 28
-2

./assets/CSS/filename.css Did the job for me.

Pope Francis
  • 557
  • 8
  • 9