This way I link to my files in html and php.
Because by rewriting the url, the styles change and the face of the site becomes ugly.
But this method is not very good because it does not work on some sites.
Sample of my code:
<?php $domain = example.com ; $host = $_SERVER['HTTP-HOST'];?>
<link rel="stylesheet" href="<?php echo "http://$host/$domain" ?>style.css" >
May be come must be like this?
<?php
$domain = 'example.com';
$host = $_SERVER['HTTP_HOST'];
if ($host === 'localhost') {
$host .= "/$domain";
}
?>
<link rel="stylesheet" href="<?php echo "http://$host/" ?>style.css" >
UPD: Added dynamin host naming for localhost host.
Related
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!
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}'>";
?>
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';*/
}
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.
I Use this part of code to print canonical tag:
<?php
if(!empty($_SERVER["REQUEST_URI"])){
$url = strtok($_SERVER["REQUEST_URI"],'?');
?>
<link rel="canonical" href="https://mywebsite.com<?=urldecode($url);?>" />
<?php
} else {
?>
<link rel="canonical" href="https://mywebsite.com" />
<?php
}
?>
But it not going to ready else condition if current URL is https://mywebsite.com. I found out why, because var_dump($_SERVER["REQUEST_URI"]); return string(1) "/". Why when I open this URL REQUEST_URI return / while there is no slash at tail?
Anyway, beside of this question, how can I solve my code to working?
In a HTTP call $_SERVER['REQUEST_URI'] can never be empty unless it is unset($_SERVER['REQUEST_URI']) or $_SERVER['REQUEST_URI] = '' by the script itself.
For a HTTP call to http://mywebsite.com, it will hold / as its value. That's what you are getting. / denotes root of the website, so when HTTP request is made to the root of the website, / is sent as path and will always be present, whether you add it or not into the URL.
I think that clarifies why you are not hitting else condition of your code ever.
Coming to your problem, if you want to append the path to the url without any query parameters, the better approach for this should be:
<link rel="canonical" href="https://mywebsite.com<?php echo parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); ?>">
This replaces all of your code. You may want to take a look at parse_url function of PHP.
If you don't want the trailing slash intentionally, you can make use of rtrim.
<link rel="canonical" href="https://mywebsite.com<?php echo rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); ?>">
In case, you are passing any valid URL to parse_url and don't want trailing slash only for root, then:
<?php
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if($path == '/' || !$path) {
$path = '';
} ?>
<link rel="canonical" href="https://mywebsite.com<?php echo $path; ?>">
I am very new to php. Want to load css file of my plugin if the css file doesnot exist in the theme/my_plugin folder (i copy pasted the file).
to include if the file exists in the theme/my_plugin folder I am using #get_headers[0], what could be the alternative to it, which is simpler and easier.
I'm including it like this:
<link rel="stylesheet" href="<?php if( #get_headers( MY_THEME_PLUGIN_CSS . 'style-archive.css')[0] == 'HTTP/1.1 404 Not Found') {
echo set_url_scheme( MY_THEME_PLUGIN_CSS . 'style-archive.css') . MVERSION;
} else {
echo set_url_scheme( MY_PLUGIN_CSS . 'style-brochures-archive.css') . EPL_BR_CSS_VERSION;
} ?>" type="text/css" media="all" />
where MY_THEME_PLUGIN_CSS = my_theme/my_plugin/css/
and MY_PLUGIN_CSS = my_plugin/css
Please let me know how can I minimise the code and yet keep it simple
Building my first PHP site and need some help.
Trying to make it so that the domain name will be used for a directory.
Since I know this is a very broad question, the following will be the example of what I have and what I need.
$domain = $_SERVER['HTTP_HOST'];
if ($domain == 'website.com' || $domain == 'www.website.com') {
$domain = 'Website';
include ('Website/variables.php') ; }
else {
$domain = 'Other';
include ('Other/variables.php') ;
}
Now, what I need it to do is be able to place the $domain inside of an
link href="css/custom.css" rel="stylesheet"
So what it should end up doing is actually using the $domain named folder like so
link href="$domain/css/custom.css" rel="stylesheet"
No matter what I try to do, I just can not get my site to read into the directory of the domain that it is in which is stopping my CSS from working.
Any help would be appreciated.
Try
echo "<link href=' " . $domain . "/css/custom.css' rel='stylesheet'>";
Also you can do as..
<link href="<?php echo $domain; ?>/css/custom.css" rel="stylesheet">
Same thing only....
If I am using this:
$subDir = '/myfolder site on wamp/';
$includePath = $_SERVER['DOCUMENT_ROOT'] . $subDir;
Then how do I link my styles to that root directory?
I've been trying this : <link rel="stylesheet" href="'.$includePath.'/css/main.css">
But it is not working.
Try to use relative path like, if you have following directory structure-
myfolder
|-->css
|-->main.css
|-->index.php
The relative path should be like -
<link rel="stylesheet" href="./css/main.css">
But if you want to have an absolute path than try to check with following code that what value is suitable for your web application-
<?php print_r($_SERVER); ?>
Or create a method which returns the absolute path like-
<?php
function getAddress() {
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
$fileptrn = '/'.basename(__FILE__).'/';
return $protocol.'://'.$_SERVER['HTTP_HOST'].preg_replace($fileptrn,"",$_SERVER['REQUEST_URI']);
}
?>
Then call it like-
<link rel="stylesheet" href="<?php echo getAddress().'css/main.css'; ?>">