Page 1 of 1

Dynamic Web Site in minutes with flat-file database Rate Topic: -----

#1 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Posted 21 December 2004 - 08:19 PM

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.
<?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.
<?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.
<?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.
<?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.
<?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

<?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");
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic Web Site

#2 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Post icon  Posted 20 July 2006 - 12:34 PM

Nice tutorial, but I got better solution:

<?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 :)
P.S. If I made some mistakes - sorry
Was This Post Helpful? 0
  • +
  • -

#3 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Posted 07 January 2007 - 08:30 AM

file_get_contents() funtion exists in php 4.3 and up. is easeir if you have it.

View Postk0b13r, on 20 Jul, 2006 - 12:34 PM, said:

Nice tutorial, but I got better solution:

<?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 :)
P.S. If I made some mistakes - sorry

Was This Post Helpful? 0
  • +
  • -

#4 ap0c0lyps3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 19-June 07

Posted 29 November 2007 - 01:48 AM

Isn't doing this
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 November 2007 - 01:50 AM

Was This Post Helpful? 0
  • +
  • -

#5 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Posted 29 November 2007 - 08:28 AM

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.)
Was This Post Helpful? 0
  • +
  • -

#6 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Posted 10 February 2008 - 01:07 PM

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.
<?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']);
}

?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1