<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Reflection Media &#187; PHP</title>
	<atom:link href="http://www.reflectionmedia.ro/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reflectionmedia.ro</link>
	<description>Custom WordPress Solutions</description>
	<lastBuildDate>Tue, 24 Jan 2012 10:34:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Making darkness or brightness transparent</title>
		<link>http://www.reflectionmedia.ro/2009/07/06/making-darkness-or-brightness-transparent/</link>
		<comments>http://www.reflectionmedia.ro/2009/07/06/making-darkness-or-brightness-transparent/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 10:28:08 +0000</pubDate>
		<dc:creator>Cristian</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[alpha]]></category>
		<category><![CDATA[brightness]]></category>
		<category><![CDATA[darkness]]></category>
		<category><![CDATA[transparency]]></category>

		<guid isPermaLink="false">http://www.reflectionmedia.ro/?p=428</guid>
		<description><![CDATA[Let&#8217;s say you have the picture of a flame and you want to change the background. In order to do that you first must make the background transparent. But how can you do that? You can do it by hand using a brush but it&#8217;s not quite the effect I was looking for. So I [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have the picture of a flame and you want to change the background. In order to do that you first must make the background transparent. But how can you do that? You can do it by hand using a brush but it&#8217;s not quite the effect I was looking for. So I figured there must be a way to automatically adjust the transparency of each pixel depending on it&#8217;s darkness.</p>
<p>This gave me the chance to play around with <a href="http://www.php.net/gd">PHP GD</a>, and I must say I&#8217;m quite impressed. It seems to be a really powerful image processing tool. <a href="http://www.reflectionmedia.ro/wp-content/uploads/2009/07/transparent.zip">Here</a> is the script I came up with, and <a href="http://www.reflectionmedia.ro/projects/gab/alpha/index.php">here</a> it is in action.</p>
<div id="attachment_471" class="wp-caption alignnone" style="width: 211px"><img src="http://www.reflectionmedia.ro/wp-content/uploads/2009/07/before-201x300.jpg" alt="Before" title="before" width="201" height="300" class="size-medium wp-image-471" /><p class="wp-caption-text">Before</p></div>
<div id="attachment_472" class="wp-caption alignnone" style="width: 211px"><img src="http://www.reflectionmedia.ro/wp-content/uploads/2009/07/after-201x300.jpg" alt="After" title="after" width="201" height="300" class="size-medium wp-image-472" /><p class="wp-caption-text">After</p></div>
<p>Pretty neat, huh? Here&#8217;s the script so you can see it without having to download the zip file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// $url: the URL of the image you want to be processed. It can also be a local filename.</span>
<span style="color: #666666; font-style: italic;">// $type: 0 for brightness, anything else for darkness.</span>
<span style="color: #666666; font-style: italic;">// $mode: 0 for theoretical mode, anything else for Photoshop mode.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> transparent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mode</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Create the old image</span>
<span style="color: #000088;">$old_image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatefromstring</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the new image of the same size as the old one</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x_size</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y_size</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$new_image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x_size</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Turn off alpha blending and set alpha flag for our new image</span>
<span style="color: #990000;">imagealphablending</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_image</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagesavealpha</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_image</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Do the following for each pixel</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$y_size</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$x_size</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// get it's color</span>
		<span style="color: #000088;">$rgb</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// and get the R, G and B components of the color</span>
		<span style="color: #000088;">$old_color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorsforindex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rgb</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$old_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'red'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$g</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$old_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'green'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$old_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blue'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Define brightness:</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$brightness</span> <span style="color: #339933;">=</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">,</span> <span style="color: #000088;">$g</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// photoshop mode OR</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$brightness</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">,</span> <span style="color: #000088;">$g</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">,</span> <span style="color: #000088;">$g</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// theoretical mode</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Define darkness:</span>
		<span style="color: #000088;">$darkness</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">255</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$brightness</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Generate transparence:</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$alpha</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$darkness</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// for darkness OR</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$alpha</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$brightness</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// for brightness</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Define the new color, same as the old one, but with alpha transparency</span>
		<span style="color: #000088;">$new_color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocatealpha</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$r</span><span style="color: #339933;">,</span> <span style="color: #000088;">$g</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #339933;">,</span> <span style="color: #000088;">$alpha</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Set that color to the pixel in the new image</span>
		<span style="color: #990000;">imagesetpixel</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output image to browser</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy all images in the end</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Update</strong>: There&#8217;s also a <a href="http://www.powerretouche.com/Transparency_plugin_tutorial.htm">Photoshop plugin</a> for doing something similar, but it costs 27 euros.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reflectionmedia.ro/2009/07/06/making-darkness-or-brightness-transparent/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>404 email alert</title>
		<link>http://www.reflectionmedia.ro/2009/05/29/404-email-alert404-email-alert/</link>
		<comments>http://www.reflectionmedia.ro/2009/05/29/404-email-alert404-email-alert/#comments</comments>
		<pubDate>Fri, 29 May 2009 09:44:26 +0000</pubDate>
		<dc:creator>Cristian</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[404]]></category>
		<category><![CDATA[alert]]></category>
		<category><![CDATA[email]]></category>

		<guid isPermaLink="false">http://www.reflectionmedia.ro/2009/05/404-email-alert404-email-alert/</guid>
		<description><![CDATA[Here is a little PHP code snippet to add to your 404 page, in order to get an email alert with all sorts of useful data, each time a 404 page is accessed. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a little PHP code snippet to add to your 404 page, in order to get an email alert with all sorts of useful data, each time a 404 page is accessed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_REFERER'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ref</span><span style="color: #339933;">=</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_REFERER'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$ref</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'none'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$sitename</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'Reflection Media'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$recipient</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'mail@gmail.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'New '</span><span style="color: #339933;">.</span><span style="color: #000088;">$sitename</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' 404 accessed'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail_body</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
A new 404 page has been accessed:
&nbsp;
The accessed URI was: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
The user'</span>s IP address<span style="color: #339933;">:</span> <span style="color: #0000ff;">'.$_SERVER['</span>REMOTE_ADDR<span style="color: #0000ff;">'].'</span>
The user<span style="color: #0000ff;">'s Internet browser: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
The URI from where your user came: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$ref</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'From: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$sitename</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' 404 page &lt;no-reply@mail.com&gt;rn'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$recipient</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mail_body</span><span style="color: #339933;">,</span> <span style="color: #000088;">$header</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.reflectionmedia.ro/2009/05/29/404-email-alert404-email-alert/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fun with PHP</title>
		<link>http://www.reflectionmedia.ro/2009/02/16/fun-with-php/</link>
		<comments>http://www.reflectionmedia.ro/2009/02/16/fun-with-php/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 10:09:51 +0000</pubDate>
		<dc:creator>Cristian</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.reflectionmedia.ro/?p=199</guid>
		<description><![CDATA[OK. So we know that instead of using the database, regular files can be used. It&#8217;s quite ineffective, but it&#8217;s possible. This means that the database structure and the regular file structure are somehow interchangeable. So&#8230; this would mean that the database can be used instead of regular files. Now, this is the obvious use [...]]]></description>
			<content:encoded><![CDATA[<p>OK. So we know that instead of using the database, regular files can be used. It&#8217;s quite ineffective, but it&#8217;s possible. This means that the database structure and the regular file structure are somehow interchangeable.</p>
<p>So&#8230; this would mean that the database can be used instead of regular files.</p>
<p>Now, this is the obvious use of the BLOB object, but it&#8217;s mainly used for media files. What I had in mind are actual web-pages, including PHP code, stored inside the database.<br />
<span id="more-199"></span><br />
The first obstacle I bumped into was that you can&#8217;t echo PHP code. I mean, you can do it, but it&#8217;s useless. The web server won&#8217;t parse your PHP twice, so it just sends PHP code to your browser, which is dumb.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt; ?php echo 'hello world'; ?&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// does not work</span></pre></td></tr></table></div>

<p>The first solution that came to mind was writing the code in a temporary file and right after that include it, <em>in the very same script</em>. I&#8217;d love to hear other solutions to this problem.</p>
<p>So, once I got this figured out, the rest was more or less a piece of cake.</p>
<p>All I had to do was:
<ul>
<li>Connect to the MySQL server, select the database and fetch the results;</li>
<li>Create a temporary file, write the database results there and then close it;</li>
<li>Include the temporary file right there, in the same script;</li>
<li>Delete the temporary file and close the MySQL connection.</li>
</ul>
<p>The table I used is a very simple one:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">CREATE TABLE <span style="color: #b1b100;">IF</span> NOT EXISTS `webpages` <span style="color: #009900;">&#40;</span>
  `name` varchar<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">default</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
  `content` varchar<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">default</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And here is the little script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a function I made for easily linking to other database web pages:</span>
<span style="color: #000000; font-weight: bold;">function</span> dblink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'http://'</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SCRIPT_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'?name='</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// function for including the text of a database web page:</span>
<span style="color: #000000; font-weight: bold;">function</span> dbinclude<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// getting the content of the web page from the database:</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM webpages WHERE name='<span style="color: #006699; font-weight: bold;">$name</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// creating a tempoeary file and writing the web page content to it:</span>
	<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;temp.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// include the text of the temporary file:</span>
	<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'temp.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// get the name of the database web page I want to include:</span>
<span style="color: #000088;">$name</span><span style="color: #339933;">=</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">==</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$name</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'home'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// connect to the database:</span>
<span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;your_username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;your_password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;your_database&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// include the content of the database web page:</span>
dbinclude<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// close the MySQL connection and delete the temporary file:</span>
<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'temp.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It&#8217;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.</p>
<p>You can test it <a href="http://www.reflectionmedia.ro/projects/littlescript/">here</a> and you can download the script and sql file from <a href="http://www.reflectionmedia.ro/projects/littlescript/files.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reflectionmedia.ro/2009/02/16/fun-with-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

