I think this should be a fairly simple question for anyone who knows PHP. I am more of a designer and still in the process of learning PHP.
I have a site I built which includes a PHP header and footer include. In the header file (which is used by ALL pages in the site) I have links to CSS files, JavaScript files, etc.
Right now I am using this solution for the links and includes in the header...
http://<?php echo $_SERVER['SERVER_NAME']; ?>/css/style.css
The issue with that is that some of the pages in the site are SSL pages and need to use a HTTPS prefix, not HTTP. That being said, I'm wondering if there is a simple way to link to CSS files, JavaScripts, images, etc. within the header file other than what I'm currently using?
<link rel="stylesheet" href="/css/style.css">
If that won't work for you, write a PHP function.
function base_url()
{
if($_SERVER['HTTPS'])
return "https://mysite.com";
else
return "http://mysite.com";
}
and in your html code.
<a href="<?php echo base_url(); ?>/css/ssl_stylesheet.css">
Or you can use a relative protocol: //<?php echo $_SERVER['SERVER_NAME']; ?>/css/style.css
Related
I'm recently doing a website for a school project. In order to organize my work, I create a tree folder that keeps all the work organized. It is similar like this:
Back-Office
Pages
Home
home_test1.php
home_test2.php
home_test3.php
Login
Folder_Login
login.php
logout.php
Resources
CSS
style_home.css
style_navbar.css
style_footer.css
JS
script_home.css
script_navbar.css
Sections
navbar.php
footer.php
After all, with the require() method available in PHP, I want to call the "navbar.php" file to the "home_test1.php", "home_test2.php" and "home_test3.php", but the CSS style that is connected with the file "navbar.php" ("style_navbar.php"), doesn't display.
I've tried to change the path of the CSS style in the file "navbar.php" when I require() to the other file ("home_test1.php") and the CSS style shows up, but wont display in other file with a different path. How can I make this work dynamically? Sorry for long post and bad English grammar.
Thank you in advance.
You need to set your css and js files with absolute path instead of relative path
$dir = realpath($_SERVER["DOCUMENT_ROOT"]);
<link rel="stylesheet" href="<?php echo $dir.'/resources/css/style_home.css'; ?>" >
Without physically seeing you code it is quite hard to debug however there is an "obvious" answer that I'll suggest as a starting point.
The important thing to remember is that PHP and HTML are processed in completely different places. PHP executes on the server and should be used to build a full HTML "document" which it gives to the client/browser. The client/browser then reads the document provided and renders it according to HTML standards.
Calling require() will tell PHP to get the file and slot its contents directly where it was called and as it is a CSS file it will need to sit within the style tags. With a lot of modern browsers, if you use require on a file outside of the html tags, the content will be dumped at the top of the screen or simply ignored due to invalid syntax.
Alternatively if you would like to simply use tell the browser to include the CSS file, you could use the good old method of using <link rel="stylesheet" href="/path/to/file">. It's good to know when and when not to use PHP.
PS: You have .css files in your JS directory.
In PHP, there is a global variable containing various details related to the server. It's called $_SERVER. It contains also the root:-
$_SERVER['DOCUMENT_ROOT']
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
<link rel="stylesheet" href="<?php echo $path.= '/Resources/CSS/style_navbar.css';?>" />
?>
I'm developing a website for my conclusion work at school. I'm using XAMPP v3.2.1. to localhost the site.
My site's folder's are configured just like this in htdocs folder:
ibnm
css
js
img
...
site
about
midia
...
includes.php
index.php (HOME PAGE)
My problem starts here: on index.php I'm including includes.php, that's a simple file with define() functions to the folders of my site so I can print the constant on the HTML tags of the site as URL (just like below)
//includes.php
<?php
define("css", "localhost/ibnm/css");
?>
//index.php
<?php
include_once("includes.php");
?>
<link href="<?= css; ?>/bootstrap.css" rel="stylesheet">
But when I do this, the CSS don't function. When I see an <a> tag with the previously defined URL on page it looks like
localhost/ibnm/site/localhost/ibnm/css
instead of
localhost/ibnm/css
It's confunsing 'cause if the <a> tag doesn't have any value (href="") it output localhost/ibnm/site/.
What can be wrong? XAMPP or coding?
Any url is not starts with http then browser will assume that its relative path so it will append to your current path, thats why your getting localhost/ibnm/site/localhost/ibnm/css.
And one small correction in your code, its not good idea to hard code server name in the code, better to get server name dynamically. So that you no need to change while deploying your site in real server.
//includes.php
<?php
define("css", $host='http://'.$_SERVER['SERVER_NAME'].'/ibnm/css');
?>
This is because the browser think localhost is a folder, and then do this ontop of the current path, to fix is just add http:// before the localhost
define("css", "http://localhost/ibnm/css");
Instead of using absolute path you can just add one slash before CSS path -
<link href="/<?= css; ?>/bootstrap.css" rel="stylesheet">
I am using an iframe in wordpress, but the page inside iframe not supporting wordpress functions.
the page inside iframe is from the same domain.
Here is what i am trying to do:
I am trying to call a css inside iframe page via wordpress function. Below is how i am using iframe tag :
<iframe id="iframeBox" src="<?php echo get_template_directory_uri();?>/iframe/scoreboard_summary.php" sandbox="" FRAMEBORDER="no" ></iframe>
Here is the css calling tag in my page scoreboard_summary.php :
<link type="text/css" rel="stylesheet" href="<?php echo get_template_directory_uri();?>/style.css">
the function get_template_directory_uri() is not working ?
Please let me know what should i need to do?
Thanks in advance.
iframes are just webpages pulled in and shown in a div. You can't interact with them in that way. PHP has already spit out what was defined by the loops and rules you set. You would want to look into ajax for something like that if you want to talk to the server and bring up new data at an interval, and skip the iframe.
<?php echo 'do this thing' ?> just turns into this thing after the page is loaded. There isn't much to hook into.
If you are just trying to get the stylesheet and apply it to you your page, then I would suggest getting the whole, standard, URL for that stylesheet and skip the template directory uri etc.
Maybe you can explain it better with some code or an example like this one, it's an iframe that refreshes at an interval. Good Luck!
http://jsfiddle.net/sheriffderek/3C9qP/
With Kohana, using a Templating system, what is the correct way to link to the style sheets, javascript files and most importantly images?
Shall I add <?php echo url::base() ?> in front of links? This surely does work but doesn't seem like the correct way to do things.
What is the correct way?
Put the assets in your DOCROOT somewhere: DOCROOT/assets/images/
Use any of the following:
url::base().'assets/images/thing.png
url::site('assets/images/thing.png')
<base href="<?=url::base()?> (then use normal relative links in your html)
I'm rather new to PHP programming but I thought I'd do it right from the beginning, so I came across this fine pdf Web Performance Boot Camp where he suggests:
All sites should always prepare for CDNized static content
and this is how:
<img src=”<?php echo CDN(‘/i/left-menu-background.gif’) ?>”
etc., he also gave an example of how the CDN function? should look like:
sub CDN { return #_[1]; }
or (when you finally have your static content on a CDN)
sub CDN { return ‘http://s.company.net’ . #_[1]; }
(but that's not valid php, right? it looks more like perl...)
Anyway, this goes on with how to rewrite the header like:
<link type="text/css" rel="stylesheet" href="<?php echo $this->CDN("c/".$this->css_file) ?>" />
But honestly, I have no idea how to do it right. So, my question is, how to I prepare my (php) site for a CDN? Where do I put the sub CDN function? How should it look in valid php? How/Where do I include it? Do I have to put a
<?php require('../cdn.php'); ?>
at the beginning of every html/php file I create (that uses scripts/css/static images/etc.)? Thanks for reading this.
If you're expecting to use a CDN in the future, this is not a stupid idea.
A simple function would look like this:
function getURL($url) // Name it whatever you want
{
// Choose one of the following:
return $url; // If you're local
return "http://s.company.net/".$url; // If you're on a CDN or static server
}
and the markup:
<link type="text/css" rel="stylesheet" href="<?php echo getURL("c/".$this->css_file) ?>" />
Do I have to put... at the
beginning of every html/php file I
create (that uses scripts/css/static
images/etc.)?
Yes. It might be wise to include some sort of central bootstrap file (some frameworks call it bootstrap.php) for future shared PHP settings that you may need to introduce. That bootstrap file would then, in turn, include the cdn.php.