PHP inside an <link href="css/custom.css" rel="stylesheet"> - php

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....

Related

How do I get my site domain and hosting using php?

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.

alternative to #get_header[0] to check if a remote file exists

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

Calling 2 different stylesheets wordpress (some logic within)

Hi guys I have some logic here that does not seem to be working for me. Currently I am trying to use two different stylesheets in wordpress. Here is the logic:
<?php
$url = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if ($url=="theurl.com")
{
?><link rel="stylesheet" type="text/css" href="stylesheetforthispage.css" /><?;
}
else
{
?><link rel="stylesheet" type="text/css" href="stylesheetforotherpages.css" /><?;
}
?>
My problem is that the URL that I am trying to use the other stylesheet for is the homepage, and everytime I try this logic is does not seem to work? All help would be appreciated, thanks in advance!
<?php
if ($_SERVER["REQUEST_URI"] == "/")
echo '<link rel="stylesheet" href="/css/homepage.css" type="text/css"/>';
else
echo '<link rel="stylesheet" href="/css/other-pages.css" type="text/css"/>';
?>

How to link my styles to through a one root path created on php?

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'; ?>">

How to swap stylesheets with a link

I am developing a site using PHP, XHTML strict, and jQuery that will be flexible to support both mobile and desktop devices, using the Responsive Web Design approach and serving different stylesheets using the min-device-width property.
<link rel="stylesheet" href="css/960.css" type="text/css" media="screen and (min-device-width: 480px)" />
But the site will need to have a link to toggle the "desktop" and "moblie" view. I have a basic idea of how this could be done using jQuery, but would prefer a solution in php for devices that don't support JavaScript.
PHP isn't too hard to implement this with. If you were to use a link, like so...
Desktop view
Then in your page script...
//test for stylesheet parameter
if(isset($_GET['view']))
{
//save and set it in the session
$stylesheet = $_GET['view'];
$_SESSION['stylesheet'] = $stylesheet;
}
elseif(isset($_SESSION['stylesheet']))
{
//parameter not sent, so get it from the session
$stylesheet = $_SESSION['stylesheet'];
}
Later, you select your stylesheet code based on this $stylesheet variable.
if($stylesheet == 'desktop')
{
echo '<link rel="stylesheet" href="/css/desktop.css" type="text/css" />';
}
elseif($stylesheet == 'mobile')
{
echo '<link rel="stylesheet" href="/css/mobile.css" type="text/css" />';
}
else
{
echo'<link rel="stylesheet" href="css/960.css" type="text/css" media="screen and (min-device-width: 480px)" />';
}
You could use a SESSION variable for that. The following example assumes there are two stylesheets mobile.css and desktop.css
Testing the argument, and setting up the session.
<?php
session_start();
if(isset($_GET['style']))//Tests the argument
{
if($_GET['style']=="desktop")//This to prevent passing wrong arguments
{
$_SESSION['style']=$_GET['style'];
}
if($_GET['style']=="mobile")//This to prevent passing wrong arguments
{
$_SESSION['style']=$_GET['style'];
}
}
?>
<head>
blah
<?php
if(isset($_SESSION['style'])
{
echo '<link rel="stylesheet" href="'.$_SESSION['style'].'.css" type="text/css" media="all" />';
}
?>
</head>
Your link to switch from/to desktop CSS.
Desktop version
Mobile version
Well, why not just include the appropriate stylesheet depending on a query parameter, or session variable or UA etc etc
//Code that figures out what type is
$css = '';
switch ($type) {
case 'mobile':
$css = '<link rel="stylesheet" href="css/960.css" type="text/css" />';
break;
case 'touch':
$css = '<link rel="stylesheet" href="css/960-mobile.css" type="text/css" />';
break;
case 'desktop':
default:
$css = '<link rel="stylesheet" href="css/960-full.css" type="text/css" />';
break;
}
In your view
echo $css;
NOTE This is only an example. Ideally you shouldn't be constructing your whole link html in PHP, but not knowing any of your implementation details, it will suffice. Best way to do it would be to name your CSS files in a standard manner with only a -type at the end to differentiate where each file is used and just pass the $type variable to the view.
Is this what you're looking for?

Categories