Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,392 Programmers. There are 1,404 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Dynamic Web Site

 
Reply to this topicStart new topic

> Dynamic Web Site, in minutes with flat-file database

SpaceMan
Group Icon



post 21 Dec, 2004 - 08:19 PM
Post #1


Just a quick and simple web site demo. i use it for quick install, or as a back up site is i break the mysql. many reasons i wrote it. some web hosts don't have sql database.
another one is my site looks for mysql pages to load if it does not exist it looks for php pages, then static pages, if none are found it displays 404.
This will cover the static page part of the function.

Assuming you have header tags and a header page and a footer page to include for the links, title etc.

first thing then is to have a pointer, were the files are.
CODE

<?php
// no trailing slash
$mycontentdir = "/server/root/to/file/dir";
?>


OK, now to get the the file and open it in read mode.
originally i did this with an include, eather way will work.
CODE

<?php
    if (is_file("$mycontentdir/$main_page")) {
$open = fopen("$mycontentdir/$main_page", "r");
$size = filesize("$mycontentdir/$main_page");
$content = fread($open, $size);
}
?>


now so that the file extension does not show, and to only allow viewing of extensions we choose. but will need to be above the open file function.
is important to keep everything in line properly. full code below.
CODE

<?php
$main_page = "" . $main_page . ".txt";
?>


OK, so now we need a way to view the opened content.
place it were you like, in a table etc.
CODE

<?php
print "$content ";
?>


so now we have a dynamic static page loader.
but, doh, it wont load anything till you click on a link?
OK, so if $main_page is empty, load this page. assuming you have a Home.txt page in the content dir.
placed at top of all the code.
CODE

<?php
if ($main_page == "") $main_page = "Home";
?>


so we built all the things needed, and now we put them together in proper order.
Also assuming again that you have header.txt and footer.txt in the content dir.
links, to load a page, ?main_page=Your Page-Name

CODE

<?php
$mycontentdir = "./content";
if ($main_page == "") $main_page = "Home";
@include("$mycontentdir/header.txt");

$main_page = "" . $main_page . ".txt";

    if (is_file("$mycontentdir/$main_page")) {
$open = fopen("$mycontentdir/$main_page", "r");
$size = filesize("$mycontentdir/$main_page");
$content = fread($open, $size);
}

print "$content ";
@include("$mycontentdir/footer.txt");
?>


Register to Make This Ad Go Away!

k0b13r
Group Icon



post 20 Jul, 2006 - 12:34 PM
Post #2
Nice tutorial, but I got better solution:

CODE

<?php
$mycontentdir = "./content";
@include("$mycontentdir/header.txt");
if ($_GET['main_page'] == "") $_GET['main_page'] = "Home";
echo file_get_contents($_GET['main_page'] . ".txt");
@include("$mycontentdir/footer.txt");
?>



A shorter version smile.gif
P.S. If I made some mistakes - sorry

SpaceMan
Group Icon



post 7 Jan, 2007 - 08:30 AM
Post #3
file_get_contents() funtion exists in php 4.3 and up. is easeir if you have it.

QUOTE(k0b13r @ 20 Jul, 2006 - 12:34 PM) *

Nice tutorial, but I got better solution:

CODE

<?php
$mycontentdir = "./content";
@include("$mycontentdir/header.txt");
if ($_GET['main_page'] == "") $_GET['main_page'] = "Home";
echo file_get_contents($_GET['main_page'] . ".txt");
@include("$mycontentdir/footer.txt");
?>



A shorter version smile.gif
P.S. If I made some mistakes - sorry


ap0c0lyps3
Group Icon



post 29 Nov, 2007 - 01:48 AM
Post #4
Isn't doing this
CODE
echo file_get_contents($_GET['main_page'] . ".txt");


not such a good idea?
If fopen on http:// / internet is enabled (can't remember the proper name)
won't someone be able to use XSS? If he provides a link to your website
with one of his files included. He could put his own content into it.

This post has been edited by ap0c0lyps3: 29 Nov, 2007 - 01:50 AM

snoj
Group Icon



post 29 Nov, 2007 - 08:28 AM
Post #5
Not in the code shown. That is because they are including a path to the files in question, thus if someone were to try a non-existent file they would likely just get a "files doesn't exist' error. (Which is why you always only allow defined possibilities for stuff such as this.)

SpaceMan
Group Icon



post 10 Feb, 2008 - 01:07 PM
Post #6
I rewrote this today.
but dont see an edit for the tuts. so i just add it here.

maybe more of a snipit now, lol.
CODE

<?php

//advatage of this page loader is you dont have to edit themed pages, just the content.
//is not login, no DB, edit, upload via FTP, all done
// no trailing slash , best below http server root.
define('CONTENT_DIR','/sites/test.bp/new_site/content');
define('DEFAULT_PAGE','Home');

//when calling the get_links('header',' -|- ') function.
//"header" is the name of the directory under LINKS_BASE_DIR
//so it creates a list based on files in the directory.
define('LINKS_BASE_DIR','/sites/test.bp/new_site/links');

function get_content($content){
  //clean out everything exept alhpa,numeric,underscore and space.
  // many reasons for this, security is most important.
  $content= eregi_replace("[^A-Z,0-9_ %]", '', $content);
  // we have 2 places to add extra stuff, tables, theme
  //here and the 2 functions
  //usually i add a border in table untill debug is completed.
  echo '<table align="center" width="80%" border="1">';
  // colspan=2 for a left colm
  echo '<tr><td colspan="2" align="center"><h2>Test Dynamic Content</h2></td></tr>';

  echo '<tr><td colspan=2 align="right">'.get_links('header',' -|- ').'</td></tr>';
  //no end table row till after content,but start a table data
  echo '<tr><td align="center" width="20%">'.get_links('left','<br>').'</td><td>';

  
  //if the file is not found, using custom page name display an error.
  if(!@include CONTENT_DIR.'/content.'.$content.'.txt')
    echo '<h2>404 Content Not found</h2>';
  
  //can even load links based on the page name.$content
  //just add the dir under the links dir same name as the page, put link files their.
  if($other_links = get_links($content,' -|- '))
    echo '<br> Other Links '.$other_links.'<br>';
  echo '</td></tr>';
  //add footer or theme end here
  echo '<tr><td align="center" colspan=2>'.get_links('footer',' -|- ').'</td></tr>';
  echo '<tr><td colspan=2 align="center">Powered By PHP<br></font></td></tr>';
  
  echo '</table>';
}

//can get links by files also, with an order by
//EL end link char's? if side menu, maybe want a new line.
//or header links, want a space or other char.
function get_links($links,$EL=''){
  //first *. adds order by, page name
  $list = glob(LINKS_BASE_DIR.'/'.$links."/*.*.txt");
  if($list)
  foreach ($list as $pagename){
      //get just the name we want to link.
      $page = explode('.',$pagename);
      $clean_name = eregi_replace("[^A-Z,0-9_ %]", '', $page[2]);
      //does the page still exist?
      if(is_file(CONTENT_DIR.'/content.'.$clean_name.'.txt'))
        $link_list .= '<a href="?Page='.$clean_name.'">'.$clean_name.'</a>'.$EL;
  }
  //return the link list and remove last ocurance of the end line.
  return(rtrim($link_list,$EL));
}

//page to display by default. or get the page asked for.
//using $_GET['Page'] URL would be ?Page=page name
//minus the content. and .txt, this insures no pages loaded other then named properly.
if(!$_GET['Page']){
  get_content(DEFAULT_PAGE);
} else {
  get_content($_GET['Page']);
}

?>


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 5/17/08 05:06AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month