Faheem Anis
Faheem Anis

Reputation: 314

Can't generate CSS link with php

I am using PHP to generate layout for some pages, the trouble is, it just don't seem to be able to find my custom CSS file. Here is the code that I am using:

<link rel="stylesheet" href="' . $_SERVER['DOCUMENT_ROOT'] . '/CSS/jokesStyle.css">

Note: it is a php string in which I am using it. The CSS file is located in htdocs/CSS. Here is the path generated in browser:

C:/xampp/htdocs/CSS/jokesStyle.css

no CSS rules from this file applies to the generated page. Thanks in advance!

Upvotes: 0

Views: 70

Answers (3)

Paurush Sinha
Paurush Sinha

Reputation: 165

Use the following code

<?php  
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'){  
         $url = "https://";   
    }else{  
         $url = "http://";
    }
    $url.= $_SERVER['HTTP_HOST']; 
    $url .= "/CSS/jokesStyle.css";
    echo "<link rel='stylesheet' href='{$url}'>";
?>

Upvotes: 1

lll
lll

Reputation: 476

First of all check that do you use echo command correctly.

Your code can be like that

echo '<link rel="stylesheet" href="' . $_SERVER['DOCUMENT_ROOT'] . '/CSS/jokesStyle.css">';

or like that

<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/CSS/jokesStyle.css">

When you open developer tools and see your css file is not found with error code 404 this means you are having some issue with path.

So check var_dump($_SERVER['DOCUMENT_ROOT']); path is that giving you what you want.

If C:/xampp/htdocs/CSS/jokesStyle.css this path is working so you need to be sure $_SERVER['DOCUMENT_ROOT'] must give you that C:/xampp/htdocs.

Upvotes: 0

Hedayat H
Hedayat H

Reputation: 382

You can define like

define('ROOTINDEX','https://localhost/Your site.com');

define('ROOTCSS','http://localhost/assets/css');

and where you want

<?php echo ROOTINDEX ?>/url
<?php echo ROOTCSS ?>/file.css

And For live and local you can define like this

if($_SERVER['HTTP_HOST']=="localhost")
{
define('ROOTINDEX','http://localhost/your site.com');
define('ROOTBIMG','http://localhost/your site.com/upload/banner-images');
define('ROOTLOGO','http://localhost/yoursite/upload/logo-images');  
define('ROOT','http://localhost/yoursite.com/admin');
///define('HOST', $_SERVER['SERVER_NAME']);
$server = 'localhost';
$user = 'root';
$password = ''; 
$database = 'localdb name';
$port = '3308';
}else{
define('ROOTINDEX','http://yoursite.com');
define('ROOTBIMG','http://yoursite.com/upload/banner-images');
define('ROOTIMG','http://yoursite.com/upload/post-images');
define('ROOTPROIMG','http://Your site.com/upload/product');
define('ROOT','http://Your site.com/admin');
//define('HOST', $_SERVER['SERVER_NAME']);
$server = 'localhost';
$user = 'username';
$password = 'Password';
$database = 'dbname';
/*$port = '3308';*/
}

Upvotes: 0

Related Questions