Adding and styling external file - php

There is a file on another site that I do not own, with a URL in the following format:
http://example.com/path/with/some/variables
I want to include this file in one of my own pages. I could use iframe to do this, but I also want to change the CSS of something within the included file. To my knowledge, I can't do this with this method.
However, I can't seem to be able to successfully add this via PHP either, with something like:
<?php include 'http://example.com/path/with/some/variables'; ?>
I'm not sure what other methods exist that can do this, but surely this must be possible.
Also, I'm aware of the security implications of using include in a situation like this.

Use readfile:
<?php readfile('http://example.com/path/with/some/variables'); ?>

Yeah, security limitations won't allow you do do this directly in an iframe, by manipulating the DOM of the iframed file.
To do it in PHP, you could create a PHP script to read the contents of the URL and add an external CSS file that you've created, to override whatever you want. So:
myreader.php:
$contents = file_get_contents("http://example.com/path/with/some/variables");
$contents = preg_replace("/<head>/", "<head>\n<link rel='stylesheet' type='text/css' href='mystyle.css'>", $contents, 1);
echo $contents;
and then create mystyle.css:
body {
color : red !important;
}
Finally, either just point your browser to myreader.php, or if you still want it in an iframe, point the iframe src to myreader.php.
PS: Stealing is wrong :)

You can use file_get_contents
<?php $content = file_get_contents('http://example.com/path/with/some/variables'); ?>
Here is the documentation file_get_contents

Related

file_get_contents not returning entire webpage

I've been trying to retrieve the contents of a webpage (http://3sk.tv) using file_get_contents. Unfortunately, the resulting output is missing many elements (images, formating, styling, etc...), and just basically looks nothing like the original page I'm trying to retrieve.
This has never happened before with any other URLs I have tried retrieve using this same method, but for some reason, this particular URL (http://3sk.tv) refuses to work properly.
The code I'm using is:
<?php
$homepage = file_get_contents('http://3sk.tv');
echo $homepage;
?>
Am I missing anything? All suggestions on how to get this working properly would be greatly appreciated. Thank you all for your time and consideration.
Thats normal behaviour, as you are only grabbing the file, and not related images, stylesheets etc...
I have one quick workaround to fix relative paths
http://www.w3schools.com/tags/tag_base.asp
Just add to your code <base> tag.
<?php
$homepage = file_get_contents('http://3sk.tv');
echo str_replace(
'<head>',
'<head><base href="http://3sk.tv" target="_blank">',
$homepage
);
?>
It's should help.
This is to be expected. If you look at the source code, you'll notice many places which do not have a full URL (ex lib/dropdown/dropdown.css). This tells the browser to assume http://3sk.tv/lib/dropdown/dropdown.css. However, on your website, it will be YOURURL.COM/lib/dropdown/dropdown.css, which does not exist. This will be the case for much of the content.
So, you can't just print another website's source and expect it to work. It needs to be the same URL.
The best way to embed another website is usually to just use an iframe or some alternative.
The webpage is not completely generated server-side, but it relies heavily on JavaScript after the HTML part loads. If you are looking for rendering the page as it looks in browser, you may need a headless browser instead - see e.g. this binding to PhantomJS: http://jonnnnyw.github.io/php-phantomjs/

PHP includes header or footer location

I was hoping someone could help. I have just started to dabble with PHP includes for time saving in the future. For example I want to change the footer and header on a web page once (using include) instead of copying and pasting the code 30 or 40 times - oh no... a typo start again.
Which brings me to the question(s) where is it best to place this script?
<?php include("includes/headernav.html"); ?>
Can it be placed in a div, or should it be placed at the top of your code under the body?
If I want to make an image/banner include module. Can I
<?php include("includes/image.jpg"); ?>
Or is best to wrap the image in html and apply like this?
<?php include("includes/imagewrapped.html"); ?>
Do not include .jpeg files directly, use a wrapper. Only use include with other PHP files.
As for including the header, do it any way that feels natural as long as it produces valid html. There is no particular reason to declare another div element.
Hope this helps:
<?php include("includes/ui_header.php"); ?>
My page content between header and footer
<?php include("includes/ui_footer.php"); ?>
You can probably save this as a function and call that function wherever you want to display.
It doesn't matter whether you put include in any place. However, it's better to put include in the top or bottom of your code
While including headers/footers/menus on the site, please keep in mind following things:
1) Your header/footer includes(blocks) should be wrapped inside a div.
2) This way then can be differentiated and any new change to them can be done easily.
3) Its always a good practice to include a wrapper div around an element as CSS can use it for styling.
4) Your header/footer includes (blocks) should have a flexibility that even we place them in header,footer or any sidebar, they should not disturb the UI.
1) Because you are including the HTML file, you probably need to include it where you want to display it.
2) Create a function in php where you send only image URL (maybe some other parameters) and function returns the HTML code (String) which you only echo on page where you want to display it. This way you can ensure, that all images will have the same code and styling.
for example
function generateImage($url=null) {
if (isset($url)) return '<img src='.$url.' style="width: 100px; height:100px; border: 1px;" />';
else return false;
}
The better way is to include always a php file.

Can I load javascript from a php file?

Can I do something like this?
<script src="/js/custom-user.php" type="text/javascript"></script>
The reason behind it is that I want the .php file to die() when the user is not logged in, so that other visitors (not authenticated) cannot see what the javascript looks like. Is it possible/safe to do like this?
Yes, but I do have two recommendations. First, it is better, in your circumstance, to only output the <script> if the user is logged in. Seriously, you don't want the thing which is outputting you js to really know or care about whether the user is logged in.
If you do output js in PHP, then you should include the appropriate header:
header("Content-type: text/javascript");
// either readFile or custom stuff here.
echo "alert('i canz have data!')";
// or, if you're less silly
readFile('/path/to/super-secret.js');
Actually, I once had CSS output by PHP (oh, you can do that too) which completely changed based on the get variable. I literally could have:
rel="stylesheet" type="text/css" href="css.php?v=#FF0000">
And it would use #FF0000 as a base color to completely re-define the color schemes in the website. I even went so far as to hook it in to imagemagick and re-color the site logo. It looked hideous because I'm not a designer, but it was really neat.
Certainly, so long as the php file being reference sends the appropriate content-type header when being downloaded.
Yes, you can do this, and it is safe.
In custom-user.php you will have to set a proper Content-Type header:
header('Content-Type: text/javascript');
And then output the javascript:
readfile('script.js');
Yes, but... You should better do it like this:
<?php
if ($loggedIn) { echo '<script src="/js/custom-user.js" type="text/javascript"></script>'; }
?>
That would prevent loading of empty file. All functions should be put in outer file, if you want some specific javascript changes, make a code in HEAD SCRIPT
Yes, that will work.
That's how JavaScript minifiers are able to dynamically serve minified scripts. (e.g. http://code.google.com/p/minify/)
You can but it will slow down your pages since every time someone accesses your page modphp will have to run your php/javascript script.

CodeIgniter - pass variables to CSS

I'm rewriting website on Code Igniter, and i need to load external TTF. MySQL db points path to that TTFs. Can I pass somehow these variables to CSS and make foreach loop to 'loads' these fonts.
I tried
$this->load->vars($data);
First, deal with serving dynamic CSS. My site has a controller called "resource" which allows me to serve CSS, JS, etc. (maybe images in the future). It loads views based upon the segments passed to it in the url.
So, when http://mysite.com/resource/css/main.css is requested:
My Resource controller (.../controllers/resource) handles any specifics of data handling (as is general with an MVC controller). It then loads:
A generic view: ".../views/resources/css.php", passing it the name of the desired css file. This view prints out the header, specifying the Content-Type (important!) and any other generic stuff. Then it proceeds to load:
The actual CSS file specified, here ".../views/resources/css/main.css.php".
It's a little overkill, but allows for a lot of flexibility, like you sound like you need.
Controller:
...
$segments = $this->uri->segment_array();
array_shift($segments); // remove the first two
array_shift($segments);
$content['stylesheet'] = $segments[0] . ".php"; //e.g. main.css.php
$content['data'] = array(); //Font data, etc
$this->load->view('resources/css.php', $content);
..
Generic resources/css.php
This loads up the actual .css.php stylesheet
<?php header("Content-Type: text/css"); // This is key! ?>
/* MySite CSS File (c) 2011 bla bla */
<?php
$this->load->view("resources/css/$stylesheet", $data);
echo "\n";
?>
Specific resources/css/main.css.php
<?php echo "/* I can use PHP in my CSS! */\n"; ?>
body { background-color: <?=$data['bg_color']?>; }
p { font-family: <?=$data['p_font_fam'];?>; }
You probably need to understand how you retrieve data from db and how you display them:
http://codeigniter.com/user_guide/database/index.html
good luck
EDIT:
what you need is probably something like that:
after you have retrieved the links from database and let's say you called them $ttf_links
<?php
foreach($ttf_links as $link){
echo "<link rel='stylesheet' type='text/css' href={$link['row_name']} media='screen' />"
}
?>
and then call the fonts you need in your css
Passing variables to a CSS doesn't work for as far as I know.
I have read something about CSS templating with PHP, but I can't find the link anymore. Will update this answer as soon as I found the link. But you could look for it yourself as well.
Update
Found it!: http://www.barelyfitz.com/projects/csscolor/
The easiest way I see you doing this is with file level CSS and changing values the usual way.
A workaround would be to use CSS in the page itself to load the fonts.
Here is the answer , I have implemented this and works fine
https://ellislab.com/forums/viewthread/220105/#1014374

Is there any way to find in which file - file is included

Am sure the question is vague.
Let me try to explain.
Assume zend frame work - PHP - jquery combination.
I include jquery files in layout.phtml.
i include some files in controller.php.
some file in view.phtml
Atlast when i run and view the page . Is there any way or any tool to find which file is included through which file (layout controller or view) ??
In addition can some one explain which is the best way include js files and where . using zend framework in layout or controller or view
The only way to find where a public, static asset (JS, CSS, image, etc) is included is to trawl through the source code (using something that can "find in files" would save time).
In regards to how and where to include such assets... for global includes (common stylesheets, scripts, etc), include these in your layouts.
For specific page includes, place these in your views.
The best way to include a static asset is using the appropriate view helper. These are generally displayed in your layout file, for example
<?php echo $this->doctype() ?>
<html>
<head>
<?php
echo $this->headMeta()->prependHttpEquiv('Content-Type', 'text/html; charset=' . $this->getEncoding());
// I use "prepend" here so it comes before any page specific stylesheets
echo $this->headLink()->prependStylesheet($this->baseUrl('/css/common.css'));
echo $this->headScript();
?>
</head>
<body>
<!-- content -->
<?php echo $this->inlineScript() ?>
</body>
</html>
You can then add to these placeholders in your view scripts, for example
<?php
// index/index.phtml
$this->inlineScript()->appendFile('https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js')
->appendFile($this->baseUrl('/js/my-jquery-script.js'));
To "include" a file means very different things in PHP (where it is analogous to copying and pasting source code from another file into the current file) and HTML/JavaScript (where you are referencing another file, which the browser must make a separate HTTP request to download). What do you consider "including"? Are image tags "including" the images? At least we can easily count those references by examining HTTP requests; from the client side, it's impossible to tell what include()s went into the source code behind the rendered output. Even naive source code searching couldn't tell you thanks to autoloading. As is, your question is not well enough defined to provide a clear answer.
Controversal answer:
You don't need that.
If you need that then it's something wrong with the way your designed your application.
Note: I've learned (trial and error) that 90% of things I don't know how to do and that seem to be impossible in ZF are a result of wrong application design.

Categories