Fun with PHP

OK. So we know that instead of using the database, regular files can be used. It’s quite ineffective, but it’s possible. This means that the database structure and the regular file structure are somehow interchangeable.

So… this would mean that the database can be used instead of regular files.

Now, this is the obvious use of the BLOB object, but it’s mainly used for media files. What I had in mind are actual web-pages, including PHP code, stored inside the database.

The first obstacle I bumped into was that you can’t echo PHP code. I mean, you can do it, but it’s useless. The web server won’t parse your PHP twice, so it just sends PHP code to your browser, which is dumb.

1
2
echo "< ?php echo 'hello world'; ?>";
// does not work

The first solution that came to mind was writing the code in a temporary file and right after that include it, in the very same script. I’d love to hear other solutions to this problem.

So, once I got this figured out, the rest was more or less a piece of cake.

All I had to do was:

  • Connect to the MySQL server, select the database and fetch the results;
  • Create a temporary file, write the database results there and then close it;
  • Include the temporary file right there, in the same script;
  • Delete the temporary file and close the MySQL connection.

The table I used is a very simple one:

1
2
3
CREATE TABLE IF NOT EXISTS `webpages` (
  `name` varchar(10000) default NULL,
  `content` varchar(10000) default NULL);

And here is the little script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// a function I made for easily linking to other database web pages:
function dblink($name){
	echo 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'?name='.$name;
}
// function for including the text of a database web page:
function dbinclude($name){
	// getting the content of the web page from the database:
	$result = mysql_query("SELECT * FROM webpages WHERE name='$name'");
	$row = mysql_fetch_array($result);
	// creating a tempoeary file and writing the web page content to it:
	$handle = fopen("temp.php", "w");
	fwrite($handle, $row['content']);
	fclose($handle);
	// include the text of the temporary file:
	include('temp.php');
}
// get the name of the database web page I want to include:
$name=$_GET['name'];
if ($name=='') $name='home';
// connect to the database:
$con = mysql_connect("localhost","your_username","your_password");
mysql_select_db("your_database", $con);
// include the content of the database web page:
dbinclude($name);
// close the MySQL connection and delete the temporary file:
mysql_close($con);
unlink('temp.php');

It’s the only file a site needs. Everything else can be in the database. Of course, I do realize that the database too is made of files.

You can test it here and you can download the script and sql file from here.

This entry was posted in PHP and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Posted March 8, 2009 at 9:59 am | Permalink

    Sorry I can see no need for this?

  2. fcb
    Posted July 6, 2009 at 7:19 pm | Permalink

    also there exists one php eval() function

    just one line of code

    no hassle

    http://us2.php.net/manual/en/function.eval.php

    • Posted July 6, 2009 at 10:22 pm | Permalink

      how n00bish of me…

    • Posted July 7, 2009 at 9:27 am | Permalink

      The problem with the eval() function is that it’s not allowed on many web servers due to security issues. But then again this script doesn’t have much practical value so a bit of fun on the local server is always nice :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*