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.
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'; ?>">
I read several similar posts but I don't see my fault.
index.php looks like:
<head>
<title>Demo Title</title>
</head>
<body>
<?php
require_once "footer.php";
?>
</body>
footer.php looks like:
<?php
/*
* _$ Rev. : 08 Sep 2010 14:52:26 $_
* footer.php
*/
$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo $url;
$file = # fopen($_SERVER[$url],"r") or die ("Can't open HTTP_REFERER.");
$text = fread($file,16384);
if (preg_match('/<title>(.*?)<\/title>/is',$text,$found)) {
$title = $found[1];
} else {
$title = " -- no title found -- ";
}
?>
A request for the URL http://127.0.0.1/test/index.php results in:
http://127.0.0.1/test/index.phpCan't open HTTP_REFERER.
or for http://127.0.0.1/test/
http://127.0.0.1/test/Can't open HTTP_REFERER.
Any hints appreciated.
$_SERVER is an array which contains a bunch of fields relating to the server config. It does not contain an element named "http://".$host.$param, so trying to open that as a filename will result in the fopen call failing, and thus going to the die() statement.
More likely what you wanted to do was just open the file called "http://".$host.$param. If that's what you want, then just drop the $_SERVER[] bit and it should work better.
Note that because it's a URL, you will need your PHP config to allow opening of remote files using fopen(). PHP isn't always configured this way by default as it can be a security risk. Your dev machine may also be configured differently to the system you will eventually deploy to. If you find you can't open a remote URL using fopen(), there are alternatives such as using CURL, but they're not quite as straightforward as a simple fopen() call.
Also, if you're reading the whole file, you may want to consider file_get_contents() rather than fopen() and fread(), as it replaces the whole thing into a single function call.
try this:
$file = # fopen($url,"r") or die ("Can't open HTTP_REFERER.");
Try
<?php
$dom = new DOMDocument();
$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo 'getting title for page: "' . $url . '"';
$dom->loadHTML($url);
$dom->getElementsByTagName('title');
if ($dom->length)
{
$title = $dom->item(0);
echo $title;
}
else
{
echo 'title tag not found';
}
?>
I can see your trying to track the referral's title
You need to use $_SERVER['HTTP_REFERER']; to get that
what you want to do is something like this
$referrer = (!empty($_SERVER['HTTP_REFERER']) && !substr($_SERVER['SERVER_NAME']) ? $_SERVER['HTTP_REFERER'] : false);
if($referrer)
{
try
{
if(false !== ($resource = fopen($referrer,"r")))
{
$contents = '';
while($contents .= fread($resource,128))
{}
if(preg_match('/<title>(.*?)<\/title>/is',$contents,$found))
{
echo 'Referrer: ' $found[1];
}
}
}catch(Exception $e){}
}