External stylesheet (css) in php - php

I have looked through 4-5 post on stackoverflow regarding this without solving my problem.
I have a php file and want to use the css stylesheet I have for the other HTML files.
Right now it looks like this:
<div style="height: 600px;">
<?php
echo "<link rel='stylesheet' type='text/css' href='stylesheet.css' media='all'/>";
session_start();
$admin = $_SESSION['admin'];
$author = $_SESSION['author'];
?>
<?php if ($admin == true AND $author == true) { ?>
<p id="confirmationText"><?echo "Thank you for your message! <br>You should have received a confirmation email. We will contact you soon."; ?></p>
<?php } ?>
</div>
It is not working; the p id="confirmationText" text does not get formatted. interestingly, when I just open the file locally without apache server, it will get HTML-formatted.
What to do?

CSS style links sometimes don't work properly if they aren't in the <head> ... </head> area. The best practice would be to just declare that echo statement up
there.
Try opening your page in a browser, and clicking 'inspect
element' on that place where your stylesheet is linked, and see if you can see any problems there. Often browsers just render things strangely. It can give your clues as to what's wrong.
Why are you linking the stylesheet before your session_start command? Perhaps this might just be causing an output buffer error. Try putting your link at the end of the PHP code.
Honestly, sometimes even the "best practice" and "cleanup" methods of fixing code, won't work. You can just use a little css hack. It's bad practice and a bit depreciated but I've been writing code for years and this just makes life so much easier: Instead of linking a stylesheet externally .... imbed the style directly.
Add this piece of code to your file:
<style>
<!-- change the path to the correct one, though. I have no clue where your file is, in the filesystem, I'm just guessing, here -->
<?php include($_SERVER['DOCUMENT_ROOT'] . "/stylesheet.css"); ?>
</style>

Can you open the stylesheet in your browser?
Also, why are you echoing a stylesheet in a div, and not in the head?

Related

PHP echo include variable in <head><title>

I am building a small personal website. To keep things small and to avoid writing things over and over again, I have a single header.php file that every sub-directory index.php file include's. So, every index.php file has these lines:
$title = someTitleVariableOrMethod;
include('/var/testsite/docroot/header.php');
And in my header.php file, I have these lines (I know I could probably improve the formatting, but first I want to get the title working).
<html>
<head>
<?php
if (isset($title)) {
echo '<title>' . $title . '</title>';
} else {
echo '<title>Sampletext</title>';
}
?>
<style>
//a bunch of irrelevant css
</style>
</head>
<body>
//this is the end of the header file, the rest is dealt with in the index.php file
But for some reason, the contents of the title and all my CSS show up at the start of <body> (I see this when I press F12 in browser) and NOTHING at all shows up in <head>. I just want the title contents to be put in the title tag. How could I fix this issue?
Thanks in advance.
Make sure that you are accessing the pages from a web server (e.g., XAMPP - then access via http://localhost/site/index.php). If you try to open them directly from your file system the PHP will simply be shown as you described. Also make sure the index.php has the closing body and html tags.
Sorry for the trouble, I found the issue! I was using passthru to get to content for the variable, which I didn't realize prints to the screen. My mistake!
EDIT: The site says I can't accept this answer for two days, but this is accepted

Store CSS in PHP variable?

I'm a novice php learner, I was experimenting how to link different php files dynamically. While experimenting, I realize I can create variables in my php files and make my template files echoes out the html I need without editing my template files......
for example:
Within about-me.php page, I have included my header.php and footer.php using
<?php include ('includes/header.html'); ?>
<?php include ('includes/footer.html'); ?>
then I create a variable
$page_title = 'CompanyABC';
and echo out in the header.php
$page_title = 'South Asia Exact';
Now my question is can I do this to my inline css also?
for example, I have create a variable, that store all my inline css:
$page_inlinecss = "#SAEcontentR div#certification_certificate {
margin:0 auto 0 auto;
width:580px;
height:464px;
}\n";
then I echo out in my header.php like so:
<style type="text/css">
<?php echo $page_inlinecss; ?>
</style>
I have tried it and it works, but I want to know is it the right way to do it?
There isn't a right way to do inline CSS
Your code will work, it will produce a valid page, and it will look absolutely fine to the user. BUT you shouldn't do it that way.
So, why shouldn't you do it that way?
Maintainability is the main reason that you shouldn't handle CSS this way. It is far easier to manage a separate CSS file than to pick through PHP code looking for CSS rules to change.
It looks like the data you're storing is static, the point of a variable is to store data that can change. Things like the name of the website (Company ABC) are unlikely to change during the execution of the script, so you should include them in the static HTML template.
On top of this are issues like caching (most browsers cache .css files, saving you bandwidth) and accessibility (screen readers may not know how to deal with inline styles & js).
How should you handle dynamic styles?
One way to handle dynamic styles (that is -- styles based on information which will be different on different page loads) with a combination of PHP and CSS is to define class styles in your external document and then use PHP to apply them.
For example, put this in styles.css:
span.greentext { color: #0f0; }
And this in your PHP file:
<span class='<?php echo ($someCondition) ? "greentext" : null; ?>'>Some text</span>
Or, if you have more styles to handle:
Alternatively, you could load a specific stylesheet upon a condition:
<?php if($someCondition): ?>
<link rel="stylesheet" href="styles/conditional.css" type="text/css" media="screen">
<?php endif; ?>
Hope this helps, and please don't use inline CSS, or variables, unless necessary. You'll thank yourself for it when you have to change the site 5 months down the line.
Can you do this? Yes.
Should you do this? Ehh. (No. was a bit harsh...)
Better to store the CSS filename in a php variable, then in the header add:
<link rel="stylesheet" href="<?php echo $this_page_style_sheet; ?>" />
There is no right or wrong in this case.
You may store the CSS in a string and echo it as you see fit. Or you may even embed it in your includes/header.html file. It's up to you.
Personally, if it is a collection of CSS rules, I would keep it in its own CSS file, and just echo the filename when needed.
$css_filename = "/path/to/rules.css";
// ... etc etc
<link rel="stylesheet" href="<?php echo $css_filename; ?>">
This is a beauty and a pitfall of the way the system works. You can do that, it works and it doesn't seem to present any immediate and glaring security issues. I don't know if that was an intended use of PHP, but it works so if it fits your situation you can use it. The pitfall comes when enough of these little workarounds are used that eventually a security issue could arise somewhere, but I don't recall CSS ever being used as a vector for an attack.
You can do this to generate dynamic css
file css.php
<?php
header("Content-Type: text/css");
echo 'p {color:red}';
?>
html (not complete but it should work cross browser)
<link rel="stylesheet" href="css.php" type="text/css" />
<p>This should be red</p>
Some more strict/uptight folks might say that proper CSS doesn't need variables, yadda yadda.
Personally I think if this works, then it's a clever way to add some ease-of-use to CSS. I'm all for it.

Optimum way to insert external html content HTML5 compliant? php vs HTML5 vs javascript methods

I know how simple this probably seems to you gurus, but I have been searching for over an hour to no avail...
Goal: Use a single footer file and menu file for all my webpages. Taking into account blocking, speed, etc. The content of my menu is pure html/css and the content of my footer is pure html/css. Would the optimal solution change based on the content being injected? e.g. If videos, jscript, etc. were involved.
Two part question:
1) Which method is optimal? Some kind of php include, using the tag, using jscript, etc.
2) How precisely is this achieved keeping HTML 5 standards? i.e. For the php method to work, does my calling webpage need to be .php and then does that make the HTML5 standard a moot point? e.g. If I want to inject footer.php into index.html, does my index file also have to be .php? Similarly for the tag, can the external file be an .html file(I don't like the idea of reloading all the header information with .css calls) or should it be .php?
Within the index.html file I have tried the following:
<object id="footerArea" width="100%" height="20%"
type="text/html" data="footer.html">
</object>
and
<?php include 'footer.php' ?>
Neither of these seem to work for me.
In case you are wondering... Here is the code for my footer I am trying to inject with sample data to make it shorter and easier to read:
<div class="footer box">
<p class="f-right t-right">
www.mysite.com<br />
Address: Medford, OR<br />
Phone: (541) 555-5555
</p>
<p class="f-left">
Copyright © 2011 My Name<br />
</p>
<p class="f-left" style="margin-left:20px;">
<a href="http://sampleurl.com" target="_blank">
<img style="border:0;width:88px;height:31px"
src="http://sampleurl.com"
alt="Valid CSS3!" />
</a>
</p>
<p class="f-left" style="margin-left:20px;">
<a href="http://sampleurl" target="_blank">
<img src="http://sample.png" width="228" height="50" alt="sample alt" title="sample title">
</a>
</p>
</div>
Please excuse my formatting. I am still new to posting code in forums. I tried my best :)
The extension of a filename you seen in a url has absolutely NOTHING with how that file will be treated by a browser when it's downloaded. It all comes down to the Content-type header that accompanies the file. A webmaster can trivially configure their server to treat all .exe files as plain HTML pages. They can also tell the webserver to run .html pages through the PHP parser. In fact, with "modern" SEO-optimized urls, you rarely see a file extension at all. It'll all be things like example.com/some/wonky/path, not example.com/page.php?id=wonky.
The fact that PHP has built and output a page also has nothing to do with HTML compliance. It comes down to whether the page the browser receives conforms to the standards. Are all tags properly closed? Attributes properly defined? Tags properly nested? Blah blah blah.
If you've built your code properly, the html that's output will be properly structured and be valid html. If it's not valid html, that's not PHP's fault - that's your fault for putting together code that doesn't produce the proper output.
The only time a file extension in a URL MIGHT be relevant is if the webserver outputs a generic content-type, e.g. "application/octet-stream". The browser MAY use a detectable file extension to guess at the content's type and try to treat it as such. But this is not guaranteed nor reliable.
This is what a PHP include should look like:
<?php include 'footer.php'?>
As far as I can see the code you have in your question is assigning the string "footer.php" to the variable include. However, rather than rolling your own template system, have you considered using something like Smarty?
If the called code like for a footer that is canned, you might want to create the simple footer like you want then include:
<?php
readfile("yourfile.htm");
?>
Something like the following should do what you want:
index.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css" />
<!-- other css, scripts etc -->
</head>
<body>
<!-- your main content here -->
<?php include 'footer.html' ?>
</body>
</html>
footer.html
<div class="footer box">
<!-- your footer content here -->
</div>
when you load index.php in the browser, and "view source", you should see the contents shown above, but the <?php include "footer.html" ?> should be replaced with the content from the file "footer.html".
You should not see the php code itself when you view source through the web-browser. If you do see php code in "view source", this indicates that your server isn't configured to run php properly.
For an alternate approach, which loads the content from the browser, and which doesn't use php, I'll point you to this related question and answer.

How to make php includes for html pages in web site

I'm the first to admit that I'm green when it comes to PHP coding. However many years ago, a colleague gave me a pattern to use for joining PHP with HTML to create web pages. Now, I am looking to revamp the site but I want to know if there is a better way to write it? Currently, I have an index.php page which has a layout similar to this:
<?php
if (! isset($HTTP_GET_VARS['content']) || ! $HTTP_GET_VARS['content']){
$content="home";
}
else
$content=$HTTP_GET_VARS['content'];
//1
if ($content == "home"){
$page_title="Page Title";
$keywords="Keywords found in Meta";
$desc="Description found in Meta";
$style="scripts/style.css";
$popupjs="none";
$storbutnjs="none";
$retreatjs="none";
$rolloverjs="scripts/rolloverjs.js";
$readform_chkjs="none";
$logo="req-files/logo.html";
$sidebar="req-files/sidebar.html";
$main="home.html";
}
//2
if ($content == "about"){
$page_title="Page Title";
$keywords="Keywords found in Meta";
$desc="Description found in Meta";
$style="scripts/style.css";
$popupjs="none";
$storbutnjs="none";
$retreatjs="none";
$rolloverjs="none";
$readform_chkjs="none";
$logo="req-files/logo.html";
$sidebar="req-files/sidebar.html";
$main="about.html";
}
include ("req-files/head.html");
include ($logo);
include ("req-files/navbar.html");
include ($sidebar);
include ($main);
/*include ("scripts/analytics.js");*/
include ("req-files/footer.html");
?>
So, if a person typed http://yourwebsite.com/?content=about They would get the whole About page built in the browser with all required meta, header, sidebar, footer, javascript, css, analytics, etc. Each of those required parts are html files, some may have php scripts for some of the $ callouts like Page Title, Keywords, etc.
One of my problems is when my client wants to change the name of one of the '($content == " ")' to something else. First, I can change the variable, but then I have to redirect the old name to the new name so that we don't lose page ranking.
For instance, http://yourwebsite.com/?content=about needs to be redirected to http://yourwebsite.com/?content=about-us.
Eventually, the client will redirect all or most pages to be more direct, http://yourwebsite.com/about-us. It is believed that this will make the rebuild go more smoothly when the site is turned into a WordPress website.
So, is there a better way to write this? Is there a better way to redirect URLs?
Thank you...
$HTTP_GET_VARS is deprecated. Please try to learn PHP from the official docs.
To answer your problem, another commonly used system is like this:
File: include.php
<?php
function topbanner($title) { ?>
<!doctype html>
<html>
<head>
<title><?php echo $title; ?></title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<header>
Site name, Logo, etc.
<header>
<?php }
function footer() { ?>
<footer>
©2012. Your company name. Best viewed in Mozilla Firefox.
</footer>
</body>
</html>
<?php }
Now, create html pages as you would normally do, but make sure the extensions are .php. In those pages, do this.
<?php require_once('include.php'); topbanner("Title of this page"); ?>
<h3>Welcome to this site</h3>
<p>Content content content</p>
<img src="image.jpg" />
<?php footer(); ?>
This is for simple pages. If you need more complex setup, follow the style of fork-cms to redirect pages using .htacess. Either way, pages are renamed means they lose indexing. Why do pages need to be renamed often?
http://php.net/manual/de/function.file-get-contents.php
so you can include html sites in php (var)
$page = file-get-contents('myHTMLSite.html');
and
str_replace('{header}', $header, $page);

PHP: Sending CSS into head instead of body (Joomla extension)

I'm using AutsonSlideShow extension for Joomla 1.7 which works just fine. The plugin has its downsides tho, as it writes CSS right into the body of the index.php file. I would like to change this for validation reasons. (There's more validation errors in it btw, if anybody maybe wants to double check that)
Is there any commands or ways to send the whole part of that css-code (includes php variables) to the head instead or make it available externally?
Here is an excerpt from the default.php which is the file that writes into my index.
<style type="text/css">
.box_skitter_large<?php echo $module->id;?> {width:<?php echo $slidewidth;?>px;height:<?php echo $slideheight; ?>px;}
<?php echo $margin;?>
.box_skitter_small {width:200px;height:200px;}
.box_skitter {border:<?php echo $border;?>px solid <?php echo $bordercolor;?>; background:<?php echo $backgroundcolor;?>}
</style>
It's just a cropdown of the whole part, but it's representative. If you check the sourcecode of this demo site of the plugin you can see the problem.
Greetings, Marian
ob_start();
?>
YOUR CSS HERE
<?php
$style = ob_get_contents();
ob_end_clean();
JFactory::getDocument()->addStyleDeclaration($style);

Categories