Full Version: Importing Pages To Php
Dream.In.Code > Programming Tutorials > PHP Tutorials
SpaceMan
Due to several reasons like, different database, or function calls not working when included into your dynamic site, other reasons. but to make it easy for me to load php add-ins to my dynamic theme i made a function.
sometimes need a cgi/perl page to import.
can setup ifelse as i did to look for pages of any type.

I am also able to specify the size, and no border.
for a few security reasons i have used full server url to see if the file exists, if it does, load into an iframe. and add a link to open in a new window if the browser does not suppor it.

config settings,
CODE

<?php
$iframe_height="600";
$iframe_width="550";
//full server root path
$iframe_contents_dir="/home/path/to/dir";
// http url to same dir as $iframe_contents_dir
$iframe_contents_url="http://localhost";
$iframe_style="style=\"width:$iframe_width; height:$iframe_height;\"";
?>


now we need to build the function,
this funtion does several things like if the page is not found, nothing happens, the else statement.
on my site, $main_page is the action, change to yours.
so to call a page pagename.php?main_page=import-page-name, no .php is a securty thing also, so that only the file extentions you say can be loaded.
to see settings for debugging un-comment print after else
CODE

<?php
function iframe_content($main_page) {
    global $main_page, $iframe_contents_url, $iframe_contents_dir, $iframe_style, $iframe_contentfile, $mypage;
    $iframe_contentfile = "$iframe_contents_dir/" .$main_page. ".php";
    if (is_file($iframe_contentfile)) {
$mypage = "$iframe_contents_url/" .$main_page. ".php";
print " <a href=\"$mypage\" target=\"_new\">Open in new Window</a><BR><iframe frameborder=\'0\' $iframe_style src=\"$mypage\"> </iframe>";
    } else {
//print "Page not found <BR> $iframe_contents_dir <BR>$iframe_contents_url";
}
}
?>


now we need a way to call the function, placed were you want the content to be displayed.
CODE

<?php
iframe_content($main_page);
?>


completed code,
CODE

<?php
$iframe_height="600";
$iframe_width="550";
//full server root path
$iframe_contents_dir="/home/path/to/dir";
// http url to same dir as $iframe_contents_dir
$iframe_contents_url="http://localhost";
$iframe_style="style=\"width:$iframe_width; height:$iframe_height;\"";


function iframe_content($main_page) {
    global $main_page, $iframe_contents_url, $iframe_contents_dir, $iframe_style, $iframe_contentfile, $mypage;
    $iframe_contentfile = "$iframe_contents_dir/" .$main_page. ".php";
    if (is_file($iframe_contentfile)) {
$mypage = "$iframe_contents_url/" .$main_page. ".php";
print " <a href=\"$mypage\" target=\"_new\">Open in new Window</a><BR><iframe frameborder=\'0\' $iframe_style src=\"$mypage\"> </iframe>";
    } else {
//print "Page not found<BR> $iframe_contents_dir <BR>$iframe_contents_url";
}
}
iframe_content($main_page);

?>


Have fun
Dave
no2pencil
This is a great way to add menus. Reusable code rocks.
paste120
Great tutorial.
Nice catch on the import page security, Cant tell you how many times ive seen it done wrong.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.