Web Resources

DataExpressions inc

Website Development Resources

Bookmark our site
Google
 

PHP - Practical Examples

PHP and the IFRAME Tag

Synopsis

Frames are loathed out there is the web world. I personally like frames as they allow you to display non html content while still making other areas of your site visible. They also allow you to display content from other sites without users exiting your own. The problem with frames is that the search engines cannot crawl them so you have little chance of getting your site any recognition.

With Php and the Html IFRAME tag you can host non html content, and content from other sites without having to leave your site. And, just as important, you will not have to worry about not being crawled by the search engines.

In this example I am going to show you how to create a Php page that uses the IFRAME tag to host various content that only used to be possible using frames.

The Script

First, lets examine the code for this example. php_and_iframe.php

<p><a href="http://www.anglesanddangles.com/eweb/php_examples/PHP-IFRAME.php?xurl=spreadsheet">spreadsheet</a></p>
<p><a href="http://www.anglesanddangles.com/eweb/php_examples/PHP-IFRAME.php?xurl=exturl">external url</a></p>
<p><a href="http://www.anglesanddangles.com/eweb/php_examples/PHP-IFRAME.php?xurl=pdffile">pdf file</a></p>
<p><a href="http://www.anglesanddangles.com/eweb/php_examples/PHP-IFRAME.php?xurl=textfile">text file</a></p>

<?php 
$url = 'date_time.xls';
if($_GET['xurl'] == "textfile")
{
$url = 'Excel2a.txt';

else if ($_GET['xurl'] == "exturl")
{
$url = 'http://www.everydollarmatters.com'; 
}
else if ($_GET['xurl'] == "pdffile")
{
$url = 'Working_with_strings_in_Java.pdf'; 
}
else if ($_GET['xurl'] == "spreadsheet")
{
$url = 'date_time.xls'; 
}
else
{

}
?> 

<p> 
<?php
echo
"
<iframe src =$url height = 500px width = 600px frameborder = 1 scrolling = no>
</iframe>
";
?>
</p>  

 

If you examine the url links you will notice that they are linking back to the page we are on. We are passing back to the page a url argument named "xurl", setting it to some desired value.

PHP-IFRAME.php?xurl=textfile

In our Php code we are evaluating the returned argument.

if($_GET['xurl'] == "textfile")

Based upon this we set our variable to the desired url location.

$url = 'Excel2a.txt';

After we have determined which url we want we execute the Php code that will populate the IFRAME

  echo
"
<iframe src =$url height = 500px width = 600px frameborder = 1 scrolling = no>
</iframe>
";

I was more concerned with showing technique itself. As such you will need to use the scroll bars once the page reloads to go see that the new entity has loaded in the frame.

Script in Action

Click on any of the actions below and watch the contents load into the frame. Note: If your browser cannot display PDF or Excel files these will not work.

spreadsheet

external url

pdf file

text file


Click here to get the files in the using the IFRAME tag with Php.

With a bit of extra effort you can build pages that allow you to include external sites and non html content without the need for using frames. Also, using this technique you do not have to worry about the search engines not being able to properly index your site. As you can see the page holds enough content to be properly indexed. If you look at the ads that appear at the bottom of the page you will notice that they are unaffected by using this technique. All in all this is an awesome technique to include non html content to your site.

 









Copyright 2006-2007 by Data Expressions inc --- All Rights Reserved

Links