<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2712653655982926835</atom:id><lastBuildDate>Sun, 29 Sep 2024 02:15:37 +0000</lastBuildDate><category>php</category><category>c/c++</category><category>linux</category><category>practices</category><category>the right way</category><category>announce</category><category>asm</category><category>german</category><category>hacking</category><category>how things work</category><category>mysql</category><category>poc</category><category>reverse-engineering</category><category>romanian</category><category>setting up</category><title>OriginalCopy&#39;s (a.k.a. flavious) Blog</title><description></description><link>http://originalcopy-on.blogspot.com/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-6981423384219298135</guid><pubDate>Tue, 21 Apr 2009 13:48:00 +0000</pubDate><atom:updated>2009-10-29T12:48:21.362+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">asm</category><category domain="http://www.blogger.com/atom/ns#">c/c++</category><category domain="http://www.blogger.com/atom/ns#">hacking</category><category domain="http://www.blogger.com/atom/ns#">reverse-engineering</category><title>How to hack your own single-instance application, for dummies</title><description>&lt;span style=&quot;font-style: italic;&quot;&gt;This material is intended for educational purposes ONLY. I donnot assume any responsability for wanted or unwanted damages done by the reader with the gained knowledge from this article.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Today I&#39;m going to show you how to solve the most basic programming problem&lt;sup&gt;&lt;a name=&quot;footnotes&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt;: hacking a single-instance application. But what is a single-instance application? Well, it&#39;s an application which you can only start once.&lt;br /&gt;&lt;br /&gt;And since it would be illegal to hack Yahoo! Messenger (R), I&#39;ll create my own single-instance app which I&#39;ll hack afterwards :)&lt;br /&gt;&lt;br /&gt;First, the code of the app:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;#include &amp;lt;stdio.h&gt;&lt;br /&gt;#include &amp;lt;stdlib.h&gt;&lt;br /&gt;#include &amp;lt;windows.h&gt;&lt;br /&gt;&lt;br /&gt;int main(void) {&lt;br /&gt;HANDLE hmutex = CreateMutex(NULL,FALSE,&quot;some unique id&quot;);&lt;br /&gt;if (hmutex &amp;amp;&amp;amp; GetLastError () == ERROR_ALREADY_EXISTS) {&lt;br /&gt;ReleaseMutex (hmutex);&lt;br /&gt;printf(&quot;the program is already running&quot;);&lt;br /&gt;system(&quot;PAUSE&quot;);&lt;br /&gt;return EXIT_FAILURE;&lt;br /&gt;}&lt;br /&gt;printf(&quot;program&#39;s first instance&quot;);&lt;br /&gt;system(&quot;PAUSE&quot;);&lt;br /&gt;return EXIT_SUCCESS;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;According to the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms682411%28VS.85%29.aspx&quot;&gt;MSDN&lt;/a&gt;, CreateMutex returns NULL if the mutex already exists.&lt;br /&gt;&lt;br /&gt;So basically, all we need to do is locate the call to CreateMutex and replace it with mov eax,0, since the return value of a function is always stored in EAX.&lt;br /&gt;&lt;br /&gt;Follow these steps in ollydbg:&lt;br /&gt;&lt;br /&gt;1. Load the executable&lt;br /&gt;2. Select the binary code responsible for the call and fill it with NOPs (the no-operation operation available for any x86 CPU):&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;http://1.bp.blogspot.com/__cut8imEqJc/Se4GVOCICpI/AAAAAAAAARA/dcmDqzJkZZw/s1600-h/hack-1.png&quot;&gt;&lt;img style=&quot;cursor: pointer; width: 400px; height: 240px;&quot; src=&quot;http://1.bp.blogspot.com/__cut8imEqJc/Se4GVOCICpI/AAAAAAAAARA/dcmDqzJkZZw/s400/hack-1.png&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5327202370839906962&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;3. Double click the first NOP and replace it with the code that will make the following code think that CreateMutex was successfully called and returned 0 in EAX, then click assemble. The remaining NOPs are ok, they&#39;ll keep the eventual following pointers in place:&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;http://1.bp.blogspot.com/__cut8imEqJc/Se4GVvY2FCI/AAAAAAAAARI/L3qZAqO4zOY/s1600-h/hack-2.png&quot;&gt;&lt;img style=&quot;cursor: pointer; width: 400px; height: 86px;&quot; src=&quot;http://1.bp.blogspot.com/__cut8imEqJc/Se4GVvY2FCI/AAAAAAAAARI/L3qZAqO4zOY/s400/hack-2.png&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5327202379793568802&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;4. At this point, you could run your patched process. Please note, I said process. Exactly, the changes you&#39;ve made are now in RAM, but you want the changes to be permanent in the program. So, right click and copy all modifications to executable, then choose &quot;copy all&quot; when prompted:&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;http://2.bp.blogspot.com/__cut8imEqJc/Se4GV9XaWpI/AAAAAAAAARQ/NkPyA0XeXog/s1600-h/hack-3.png&quot;&gt;&lt;img style=&quot;cursor: pointer; width: 400px; height: 307px;&quot; src=&quot;http://2.bp.blogspot.com/__cut8imEqJc/Se4GV9XaWpI/AAAAAAAAARQ/NkPyA0XeXog/s400/hack-3.png&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5327202383545653906&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;5. A new window will appear. This is the binary code of the patched executable. Simply save the binary to the permanent storage:&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;http://2.bp.blogspot.com/__cut8imEqJc/Se4GV1GeSFI/AAAAAAAAARY/cHYJTHuhu4M/s1600-h/hack-4.png&quot;&gt;&lt;img style=&quot;cursor: pointer; width: 400px; height: 135px;&quot; src=&quot;http://2.bp.blogspot.com/__cut8imEqJc/Se4GV1GeSFI/AAAAAAAAARY/cHYJTHuhu4M/s400/hack-4.png&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5327202381327124562&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;6. Voilà! Now you may start as many instances of the patched executable as you like:&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;http://2.bp.blogspot.com/__cut8imEqJc/Se4GWDnRY5I/AAAAAAAAARg/T_11rNrowow/s1600-h/hack-5.png&quot;&gt;&lt;img style=&quot;cursor: pointer; width: 400px; height: 220px;&quot; src=&quot;http://2.bp.blogspot.com/__cut8imEqJc/Se4GWDnRY5I/AAAAAAAAARg/T_11rNrowow/s400/hack-5.png&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5327202385222787986&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I hope you&#39;ve enjoyed it&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://www.blogger.com/post-edit.g?blogID=2712653655982926835&amp;amp;postID=6981423384219298135#footnotes&quot;&gt;Footnotes&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;[1] According to myself, it&#39;s not the &quot;Hello, World!&quot; program any more :-))</description><link>http://originalcopy-on.blogspot.com/2009/04/how-to-hack-your-own-single-instance.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/__cut8imEqJc/Se4GVOCICpI/AAAAAAAAARA/dcmDqzJkZZw/s72-c/hack-1.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-5262451788562664816</guid><pubDate>Wed, 25 Mar 2009 18:53:00 +0000</pubDate><atom:updated>2009-03-25T19:56:24.001+01:00</atom:updated><title>Under the Hood: the Apache 2.2 / PHP 5 Request lifecycle</title><description>This article is going to describe what happens when a client initializes a new HTTP request for a .php ressource. In order to understand it, you need some C programming knowledge and a GNU/Linux box.&lt;br&gt;&lt;br&gt;I&amp;#39;m going to do it with hands-on examples. Although I&amp;#39;ll describe Apache and PHP specifics, the lecture should be interesting for intermediate C programmers wishing to learn new techniques for introspecting complex source code written by others and learning how it works, techniques which work for practically any open source project out there.&lt;br&gt; &lt;br&gt;Be careful, I &lt;b&gt;do&lt;/b&gt; build, compile and install everything as &lt;b&gt;root&lt;/b&gt; because I know what I&amp;#39;m doing.&lt;b&gt; If you don&amp;#39;t master the CLI, then don&amp;#39;t enter any of these commands. That being said, I don&amp;#39;t assume any responsability for any damage &lt;u&gt;you&lt;/u&gt; may do to your system by following my instructions.&lt;/b&gt;&lt;br&gt; &lt;br&gt;The first step is to compile all the programs involved with debugging information. Most of the software out there accepts such parameters by using the ./configure command. Also note that I&amp;#39;m going to do all the jobs in a temporary directory named /srv, so I don&amp;#39;t pollute my system.&lt;br&gt; &lt;br&gt;Every line below written with &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;constant-width&lt;/span&gt; fonts and beginning with # is a marker for whatever commands you&amp;#39;ll need to enter in your shell. The lines starting with &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;(gdb)&lt;/span&gt; denote input for the debugger.&lt;br&gt; &lt;br&gt;&lt;div style=&quot;text-align: left; margin-left: 40px;&quot;&gt;&lt;font size=&quot;4&quot;&gt;1. Laying out the directory structure:&lt;/font&gt;&lt;br&gt;&lt;/div&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # cd /&lt;br&gt;# mkdir -p /srv/_build/{apache,php}&lt;br&gt;# mkdir /srv/src&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;In /srv/src I&amp;#39;m going to download all the source codes I need, but I&amp;#39;ll compile them in /srv/_build/, in order to keep the source code clean. Later you&amp;#39;ll realize what&amp;#39;s the motivation for this.&lt;br&gt; &lt;br&gt;&lt;div style=&quot;margin-left: 40px;&quot;&gt;&lt;font size=&quot;4&quot;&gt;2. Download all the source archives.&lt;/font&gt;&lt;br&gt;&lt;/div&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # cd /srv/src&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;You may want to go to each software&amp;#39;s official homepage and check the closest mirror to your geographical location&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # wget -c &lt;a href=&quot;http://mirror.deri.at/apache/httpd/httpd-2.2.11.tar.gz&quot;&gt;http://mirror.deri.at/apache/httpd/httpd-2.2.11.tar.gz&lt;/a&gt;&lt;br&gt;# wget -c &lt;a href=&quot;http://at2.php.net/get/php-5.2.9.tar.bz2/from/this/mirror&quot;&gt;http://at2.php.net/get/php-5.2.9.tar.bz2/from/this/mirror&lt;/a&gt;&lt;br&gt; # tar xfz httpd-2.2.11.tar.gz&lt;br&gt;# tar xfj php-5.2.9.tar.bz2&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;&lt;div style=&quot;margin-left: 40px;&quot;&gt;&lt;font size=&quot;4&quot;&gt;3. Building apache for hackers&lt;/font&gt;&lt;br&gt;&lt;/div&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # cd /srv/_build/apache&lt;br&gt;&lt;/blockquote&gt;The PHP manual (&lt;a href=&quot;http://php.net/manual/en/install.unix.apache2.php&quot;&gt;http://php.net/manual/en/install.unix.apache2.php&lt;/a&gt;) mentions only the --enable-so flag. Additionally, a search through&lt;br&gt; &lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;# ../../src/httpd-2.2.11/configure --help|less&lt;br&gt;&lt;/blockquote&gt; for &amp;quot;debug&amp;quot; leads us to --enable-maintainer-mode. Also don&amp;#39;t forget we want to --prefix=/srv --with-mpm=prefork&lt;br&gt; so while in /srv/_build/apache type in:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # ../../src/httpd-2.2.11/configure --prefix=/srv --enable-maintainer-mode --enable-so --with-mpm=prefork&lt;br&gt;&lt;/blockquote&gt;Please read the docs for each option. It is quite important to understand what a MPM is, for instance.&lt;br&gt; &lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;# make &amp;amp;&amp;amp; make install&lt;br&gt;&lt;/blockquote&gt;&lt;div style=&quot;margin-left: 40px;&quot;&gt; &lt;font size=&quot;4&quot;&gt;4. Testing whether apache works or not&lt;/font&gt;&lt;br&gt;&lt;/div&gt;The command&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # ls /srv/bin/&lt;br&gt;&lt;/blockquote&gt;should show us apachectl and httpd, among others, so:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # /srv/bin/apachectl start&lt;br&gt;&lt;/blockquote&gt;If you see something like this:&lt;br&gt;&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;&quot; class=&quot;gmail_quote&quot;&gt;(98)Address already in use: make_sock: could not bind to address [::]:80&lt;br&gt; (98)Address already in use: make_sock: could not bind to address &lt;a href=&quot;http://0.0.0.0:80&quot;&gt;0.0.0.0:80&lt;/a&gt;&lt;br&gt;no listening sockets available, shutting down&lt;br&gt;Unable to open logs&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;Then it means the &amp;quot;clean&amp;quot; httpd of your distribution is already listening on port 80, so stop that one first, by calling apachectl on your path (find out the concrete path with the command &amp;quot;which apachectl&amp;quot;):&lt;br&gt; &lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;# apachectl stop&lt;br&gt;&lt;/blockquote&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;&quot; class=&quot;gmail_quote&quot;&gt; Note: apachectl and /srv/bin/apachectl are &lt;b&gt;not&lt;/b&gt; the same. The former manipulates the &amp;quot;original&amp;quot; httpd process, whereby the latter the one in /srv/bin which we&amp;#39;ve just compiled with debugging info.&lt;br&gt;&lt;/blockquote&gt; &lt;br&gt;If nothing listens on port 80 now, which you can find out by using&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # netstat -tlnp&lt;br&gt;&lt;/blockquote&gt;you can finally start the server now:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # /srv/bin/apachectl start&lt;br&gt;&lt;/blockquote&gt;Now point your browser to &lt;a href=&quot;http://localhost&quot;&gt;http://localhost&lt;/a&gt;, just to make sure everything runs smoothly&lt;br&gt;&lt;br&gt;&lt;div style=&quot;margin-left: 40px;&quot;&gt;&lt;font size=&quot;4&quot;&gt;5. Getting a taste for debugging&lt;/font&gt;&lt;br&gt; &lt;/div&gt;On this one I&amp;#39;m going to cheat a little, because I&amp;#39;m a noob and all I know&lt;br&gt;right now is what I&amp;#39;m trying to do: debugging apache&lt;br&gt;&lt;br&gt;So after firing up the browser and looking on the web for &lt;i&gt;apache debugging&lt;/i&gt;, I came across &lt;a href=&quot;http://httpd.apache.org/dev/debugging.html&quot;&gt;http://httpd.apache.org/dev/debugging.html&lt;/a&gt; which shows me how to start and debug httpd.&lt;br&gt; So first stop httpd&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;# /srv/bin/apachectl stop&lt;br&gt;&lt;/blockquote&gt; Then load the program in the debugger&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;# gdb /srv/bin/httpd&lt;br&gt; &lt;/blockquote&gt;At the gdb prompt, set the arguments properly, since we don&amp;#39;t want to mess with the local configuration from /etc/ or similar (just in case we already had apache installed on our system before following the instructions in this article):&lt;br&gt; &lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;(gdb) set args -X -f /srv/conf/httpd.conf&lt;br&gt;&lt;/blockquote&gt; Then run the process:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;(gdb) run&lt;br&gt;&lt;/blockquote&gt;Opening &lt;a href=&quot;http://localhost&quot;&gt;http://localhost&lt;/a&gt; should show the classical &amp;quot;It works!&amp;quot; message, the only difference is now we can inspect the process live, while it&amp;#39;s running.&lt;br&gt; But because we&amp;#39;ve started httpd with the -X parameter, the daemon keeps the keyboard busy, so press CTRL+C to get back to the gdb prompt:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; (gdb) info threads&lt;br&gt;&lt;/blockquote&gt;Should confirm that apache is running one single thread (that&amp;#39;s what -X does, you read the docs, didn&amp;#39;t you?):&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;   1 Thread 0x7f2fe9d6d740 (LWP 16856)  0x00007f2fe82a29f0 in __accept_nocancel () from /lib/libpthread.so.0&lt;br&gt;&lt;/blockquote&gt;This is important at least for our purpose, since we don&amp;#39;t want to mess with multithreading, only understand the execution flow. &lt;br&gt; To really see what &amp;quot;happened&amp;quot; so far, print out a backtrace:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; (gdb) bt&lt;br&gt;&lt;/blockquote&gt;Shows something like this:&lt;br&gt;&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; #0  0x00007f883f0fc9f0 in __accept_nocancel () from /lib/libpthread.so.0&lt;br&gt;#1  0x00007f883f96d362 in apr_socket_accept () from /usr/lib/libapr-1.so.0&lt;br&gt;#2  0x000000000047e95f in unixd_accept ()&lt;br&gt;#3  0x000000000047c6d1 in child_main ()&lt;br&gt; #4  0x000000000047c837 in make_child ()&lt;br&gt;#5  0x000000000047cdcc in ap_mpm_run ()&lt;br&gt;#6  0x0000000000428f36 in main ()&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;The only thing you should have knowledge about is the main() function. The last function&lt;br&gt; on the stack __accept_nocancel looks interesting, but it&amp;#39;s part of libpthread,&lt;br&gt;and since I&amp;#39;m a noob I don&amp;#39;t want to mess with threads yet, I&amp;#39;ll just ignore it - it&amp;#39;s prefixed with __, which in common programming standards means that the function is internal to pthreads and we shouldn&amp;#39;t care about it, unless we want to have a closer look at the pthread library itself.&lt;br&gt; I do know however, that APR stands for &amp;quot;apache portable runtime&amp;quot;, and I&amp;#39;d like to have a closer look at it, so I set a breakpoint on it:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; (gdb) break apr_socket_accept&lt;br&gt;&lt;/blockquote&gt;then kill the process and restart it&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; (gdb) kill&lt;br&gt;(gdb) run&lt;br&gt;&lt;/blockquote&gt;*Note: gdb can auto-complete even function names, so after you&amp;#39;ve typed in &amp;quot;break apr_socket_&amp;quot; you can press TAB to see a list of functions matching that name. Cool, huh? :)&lt;br&gt; Now go again to &lt;a href=&quot;http://localhost&quot;&gt;http://localhost&lt;/a&gt; and see what happens. Right, since the Linux kernel sees a request on port 80, it &amp;quot;knows&amp;quot; that it should be routed to the httpd process, so you don&amp;#39;t get a &amp;quot;connection timeout&amp;quot; or a similar error message from the browser. BUT the browser still waits for data, because in the gdb debugger, the process is suspended.&lt;br&gt; &lt;br&gt;So at the (gdb) prompt, continue the process. You should position both windows, the browser and the terminal with the debugging session such that you see what happens in the browser window when you type this:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; (gdb) continue&lt;br&gt;&lt;/blockquote&gt;You&amp;#39;d see something similar to this:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; Continuing.&lt;br&gt;&lt;br&gt;Breakpoint 1, 0x00007f2478ac1330 in apr_socket_accept () from /usr/lib/libapr-1.so.0&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;Exactly, that means the httpd process served the request and came back to the breakpoint we&amp;#39;ve set on apr_socket_accept(). Now we can create a new request, just for the fun of it :)&lt;br&gt; &lt;br&gt;You may ask yourself why did I show you that? Well, first of all I wanted to teach you some basic debugging techniques, and also give you a starting point for the steps you&amp;#39;ll have to follow when you&amp;#39;re going to debug the PHP runtime.&lt;br&gt; &lt;br&gt;So don&amp;#39;t worry, just recap the concepts of debugging, program, process, request, thread, MPM (multi-processing module) you&amp;#39;ve learned so far, &amp;#39;coz they&amp;#39;re damn interesting stuff! :)&lt;br&gt;&lt;br&gt;&lt;div style=&quot;margin-left: 40px;&quot;&gt; &lt;font size=&quot;4&quot;&gt;6. Compiling PHP for hackers&lt;/font&gt;&lt;br&gt;&lt;/div&gt;Wow, you&amp;#39;ve got that far? Congratulations!&lt;br&gt;Basically, all we need are the configure flags --enable-debug and --enable-maintainer-zts (zts stands for &lt;i&gt;zend thread safety&lt;/i&gt;). Additionally, we need to specify --prefix and --with-apxs just to keep our build out of the already existing system. &lt;br&gt; All the other flags are there to make the resulting object code lighter, with less debugging symbols, and you could strip much more from it, just read the help if you wish to:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # ../../src/php-5.2.9/configure --help|less&lt;br&gt;&lt;/blockquote&gt;This should introduce less disturbing details we shouldn&amp;#39;t care about while learning the basics.&lt;br&gt;&lt;br&gt;First, configure, compile and install PHP:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # cd /srv/_build/php&lt;br&gt;# ../../src/php-5.2.9/configure --prefix=/srv --with-apxs2=/srv/bin/apxs --enable-debug --enable-maintainer-zts --disable-cgi --enable-cli --without-pear --disable-xml --without-sqlite --without-mysql --disable-pdo --disable-libxml --disable-simplexml --disable-xmlreader --disable-xmlwriter --disable-dom --disable-spl&lt;br&gt; # make &amp;amp;&amp;amp; make install&lt;br&gt;&lt;/blockquote&gt;Just be patient, it may take a while. Just as a note, since right now I&amp;#39;m waiting myself and I&amp;#39;ve got nothing better to do than writing this article, do not rm -rf anything within /srv/_build, since you may want to recompile later on but with different flags, and you could use some of the already compiled object files (.o) to speed up the compilation process. Ok, PHP is now compiled, at least on my box, so we&amp;#39;re going to integrate /srv/lib/libphp5.so just as we&amp;#39;d integrate a &amp;quot;regular&amp;quot;, non-debugging version into apache&amp;#39;s /srv/conf/httpd.conf&lt;br&gt; At least on my system, it asks me to:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;# libtool --finish /srv/_build/php/libs&lt;br&gt; &lt;/blockquote&gt;&lt;br&gt;Now add the following line to /srv/conf/httpd.conf:&lt;br&gt;&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; AddType application/x-httpd-php php&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;Note that the php5 module has been automatically added by the PHP build system by using apxs. If it didn&amp;#39;t do it for your, then you will have to add that one too. Now create a new index.php file and add a phpinfo() call in it:&lt;br&gt; &lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;&quot; class=&quot;gmail_quote&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;# vim /srv/htdocs/index.php&lt;/span&gt;&lt;br&gt;&lt;/blockquote&gt; If you&amp;#39;ve set everything correctly, you should see the php info page :)&lt;br&gt;&lt;br&gt;&lt;div style=&quot;margin-left: 40px;&quot;&gt;&lt;font size=&quot;4&quot;&gt;7. How to analyse something when you don&amp;#39;t know where to start from&lt;/font&gt;&lt;br&gt;&lt;/div&gt;For example, how to see what exactly happens when there&amp;#39;s a file like this&lt;br&gt; &lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;&amp;lt;?php&lt;br&gt;phpinfo();&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;which gets &amp;quot;called&amp;quot; by the user? Of course, you may think it&amp;#39;s an impossible mission, since the PHP and apache code bases are so huge you&amp;#39;d have no chance to inspect it entirely in a lifetime. You may be right, BUT the good news is you don&amp;#39;t even need to.&lt;br&gt; &lt;br&gt;The first way to do it is to load httpd in the debugger as you did before, set a breakpoint on apr_socket_accept, and then step into the code. Now you see, my friend, why the entire hassle of debugging only apache itself first? If we didn&amp;#39;t do that, we couldn&amp;#39;t know now where to set that breakpoint.&lt;br&gt; &lt;br&gt;I&amp;#39;ll show you a demo :)&lt;br&gt;&lt;br&gt;Just in case you&amp;#39;ve left a daemon running:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # killall httpd&lt;br&gt;&lt;/blockquote&gt;Load into the debugger:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; # gdb /srv/bin/httpd&lt;br&gt;(gdb) set args -X -f /srv/conf/httpd.conf&lt;br&gt;(gdb) break apr_socket_accept&lt;br&gt;(gdb) run&lt;br&gt;&lt;/blockquote&gt;Visit &lt;a href=&quot;http://localhost/index.php&quot;&gt;http://localhost/index.php&lt;/a&gt;&lt;br&gt;You do remember why we&amp;#39;re doing the following, right?&lt;br&gt; &lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;(gdb) continue&lt;br&gt;&lt;/blockquote&gt;But the browser still seems to wait for something. If you look again at your debugging session, the execution flow of apache has ran again into apr_socket_accept, and again, and again, a couple of times. To &amp;quot;get out of it&amp;quot;, simple type continue at the gdb prompt a couple of times, until the phpinfo page is displayed.&lt;br&gt; &lt;br&gt;We could continue this way, but it&amp;#39;s frustrating for two reasons: first, I couldn&amp;#39;t get all the debugging symbols into apache without messing my current installation, which I didn&amp;#39;t want to do just for this one article, but you may play around with options like --with-included-apr and alike.&lt;br&gt; &lt;br&gt;However, I&amp;#39;ll show you a much faster approach to really see the backtrace of what happens when a script with a call to phpinfo() in it is called.&lt;br&gt;The solution is to make assumptions based on good programming practices, at least concerning function naming in C - you already realized that both apache httpd and php are written in C, didn&amp;#39;t you?&lt;br&gt; If you were a programmer, you&amp;#39;d most probably name the function phpinfo() which is exported into the runtime in a similar way. To search for it we can use the debugger&amp;#39;s command &amp;quot;info functions &amp;lt;regexp&amp;gt;&amp;quot;:&lt;br&gt; &lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;(gdb) info function phpinfo&lt;br&gt;All functions matching regular expression &amp;quot;phpinfo&amp;quot;:&lt;br&gt; &lt;br&gt;File /srv/src/php-5.2.9/ext/standard/info.c:&lt;br&gt;&lt;b&gt;void register_phpinfo_constants(int, int, void ***);&lt;/b&gt;&lt;br&gt;&lt;b&gt;void zif_phpinfo(int, zval *, zval **, zval *, int, void ***);&lt;/b&gt;&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;Great, so there are two functions in the php &amp;quot;executable&amp;quot; (in our scenario it&amp;#39;s an apache module) that match that name, &amp;quot;register_phpinfo_constants&amp;quot; and &amp;quot;zif_phpinfo&amp;quot;. From what you could guess out of the function name we can exclude the former with almost 100% certainty, but we&amp;#39;ll not make assumptions and set breakpoints for both of them&lt;br&gt; &lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;(gdb) break register_phpinfo_constants&lt;br&gt;Breakpoint 2 at 0x7fd8672554a7: file /srv/src/php-5.2.9/ext/standard/info.c, line 990.&lt;br&gt; (gdb) break zif_phpinfo &lt;br&gt;Breakpoint 3 at 0x7fd867255748: file /srv/src/php-5.2.9/ext/standard/info.c, line 1013.&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;Great, gdb also tells us the RAM address at which those functions start, as well as the source code file, toghether with the line number. What could we need more to simply have a look at the concrete implementation? :)&lt;br&gt; &lt;br&gt;Now&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;(gdb) continue&lt;br&gt;&lt;/blockquote&gt;and request again index.php. We&amp;#39;ll see which of those both functions get hit by breakpoints and we&amp;#39;re going to know which one is the &amp;quot;phpinfo()&amp;quot; function in the runtime&lt;br&gt; &lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;Breakpoint 3, zif_phpinfo (ht=0, return_value=0x1011f58, return_value_ptr=0x0, this_ptr=0x0, return_value_used=0, &lt;br&gt;     tsrm_ls=0xe98d50) at /srv/src/php-5.2.9/ext/standard/info.c:1013&lt;br&gt;1013        int argc = ZEND_NUM_ARGS();&lt;br&gt;Current language:  auto; currently c&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;Now you know :) Just a little secret I don&amp;#39;t expect you to know, the prefix &lt;b&gt;zif&lt;/b&gt; stands for &lt;i&gt;zend internal function&lt;/i&gt;. Press CTRL+C to get back to the gdb prompt, and do a backtrace:&lt;br&gt; &lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;(gdb) bt&lt;br&gt;#0  zif_phpinfo (ht=0, return_value=0x1011f58, return_value_ptr=0x0, this_ptr=0x0, return_value_used=0, &lt;br&gt;     tsrm_ls=0xe98d50) at /srv/src/php-5.2.9/ext/standard/info.c:1013&lt;br&gt;#1  0x00007fd86737a98a in zend_do_fcall_common_helper_SPEC (execute_data=0x7fff7151e040, tsrm_ls=0xe98d50)&lt;br&gt;    at /srv/src/php-5.2.9/Zend/zend_vm_execute.h:200&lt;br&gt; #2  0x00007fd867382f0b in ZEND_DO_FCALL_SPEC_CONST_HANDLER (execute_data=0x7fff7151e040, tsrm_ls=0xe98d50)&lt;br&gt;    at /srv/src/php-5.2.9/Zend/zend_vm_execute.h:1729&lt;br&gt;#3  0x00007fd86737a28d in execute (op_array=0x1011e10, tsrm_ls=0xe98d50)&lt;br&gt;     at /srv/src/php-5.2.9/Zend/zend_vm_execute.h:92&lt;br&gt;#4  0x00007fd867349d87 in zend_execute_scripts (type=8, tsrm_ls=0xe98d50, retval=0x0, file_count=3)&lt;br&gt;    at /srv/src/php-5.2.9/Zend/zend.c:1134&lt;br&gt;#5  0x00007fd8672c8f1f in php_execute_script (primary_file=0x7fff71520580, tsrm_ls=0xe98d50)&lt;br&gt;     at /srv/src/php-5.2.9/main/main.c:2023&lt;br&gt;#6  0x00007fd8673e4c4f in php_handler (r=0x103af78) at /srv/src/php-5.2.9/sapi/apache2handler/sapi_apache2.c:632&lt;br&gt;#7  0x0000000000441a6c in ap_run_handler ()&lt;br&gt;#8  0x0000000000442305 in ap_invoke_handler ()&lt;br&gt; #9  0x0000000000462c6c in ap_process_request ()&lt;br&gt;#10 0x000000000045fc78 in ap_process_http_connection ()&lt;br&gt;#11 0x000000000044ad92 in ap_run_process_connection ()&lt;br&gt;#12 0x000000000044b1c0 in ap_process_connection ()&lt;br&gt; #13 0x000000000047c754 in child_main ()&lt;br&gt;#14 0x000000000047c837 in make_child ()&lt;br&gt;#15 0x000000000047cdcc in ap_mpm_run ()&lt;br&gt;#16 0x0000000000428f36 in main ()&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;WOW, that&amp;#39;s a lot of things to digest :)&lt;br&gt; First a little bit of theory. What you call PHP is actually made of two components (ok, ok, I&amp;#39;m lying, there&amp;#39;s more, actually), the zend engine and the rest of the functions, most of which get exported into the runtime.&lt;br&gt; &lt;br&gt;When you request a script, the zend engine first parses that file. I&amp;#39;m not going to go into the details here, but if you&amp;#39;re interested feel free to buy yourself &amp;quot;The Dragon Book&amp;quot; by Ullman &amp;amp; co. On short, &amp;quot;parsing&amp;quot; consists of two phases, in the first the input is tokenized into indivisible entities. For example, in the PHP language, some tokens are &amp;quot;{&amp;quot;, &amp;quot;-&amp;quot;, &amp;quot;.&amp;quot;, &amp;quot;function&amp;quot;, &amp;quot;class&amp;quot; and any other keyword - that is, anything which cannot be further divided but which describes the language. The scanner (the right name for &amp;quot;the tokenizer&amp;quot;) feeds another component, the lexer, with the tokens it reads. The lexer&amp;#39;s job is to put tokens into contexts. For example, the series of tokens&lt;br&gt; &lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;$array, [, &amp;#39;message&amp;#39;, ], =, &amp;#39;hello world&amp;#39;&lt;br&gt; &lt;/blockquote&gt;&lt;br&gt;Gives a simple statement. The &amp;quot;problem&amp;quot; here is that, for example strings like &amp;#39;message&amp;#39; or &amp;#39;hello world&amp;#39; could mean multiple things. The lexer&amp;#39;s job is to deduce a string&amp;#39;s meaning from its &lt;b&gt;context&lt;/b&gt;, for example a string between [ and ] is the key for an array, while a string on the right side of the &amp;quot;=&amp;quot; operator is a constant string value. This is a rather complex process, so complex that people invented programs capable of generating so-called &amp;quot;&lt;b&gt;symbol tables&lt;/b&gt;&amp;quot; from &lt;b&gt;grammar files&lt;/b&gt; that describe the &lt;b&gt;syntax&lt;/b&gt; of the language. These programs are called &lt;b&gt;parser generators&lt;/b&gt;.&lt;br&gt; If you&amp;#39;re impacient and want to see a &lt;b&gt;lexer&lt;/b&gt; and a &lt;b&gt;parser generator&lt;/b&gt; in action, you could have a look at my blog entry &lt;a href=&quot;http://originalcopy-on.blogspot.com/2008/10/how-to-make-math-calculator-right-way.html&quot;&gt;How to create a math calculator in C&lt;/a&gt;.&lt;br&gt; &lt;br&gt;As I said, it&amp;#39;s a complex subject and I&amp;#39;ll move on leaving out many interesting details. Let&amp;#39;s look at how PHP deals with the execution of scripts. After the zend engine has tokenized the input, it generates so-called &lt;b&gt;OPCODES&lt;/b&gt; from them. These are similar to CPU opcodes you may know about from assembler (like INC, MOV, CALL, RET, etc), but at a higher level of abstraction and specific to PHP.&lt;br&gt; &lt;br&gt;These opcodes are executed by another component, the &lt;b&gt;Zend Virtual Machine&lt;/b&gt; (VM, as you can also see in file names like zend_vm_execute.h above), just like the binary code of an executable would be executed by the electronics of the CPU.&lt;br&gt; &lt;br&gt;Just as a side note, these opcodes are cached by opcode cachers like APC, thus avoiding repeated tokenization and compilation into vm opcodes - since as you know, the basic difference between interpreted and compiled languages is that in interpreted languages the compilation occures at each execution of the source code. Of course, saying &amp;quot;executing a script&amp;quot; is a coloquial expression, what is actually executed is the php binary, or in our scenarion, the apache daemon, in which case PHP is only a module integrated into it.&lt;br&gt; &lt;br&gt;&lt;font size=&quot;4&quot;&gt;A few more notes:&lt;/font&gt;&lt;br&gt;1. PHP can be seen as a monolithic block of functions. Most of the functions are provided by extensions bundled toghether with PHP (for example the gd library or tidy, and so on) and exported to the runtime. You can open up about any .c source code of PHP and look for &lt;b&gt;PHP_FUNCTION&lt;/b&gt;, and you&amp;#39;ll the find functions which are exported.&lt;br&gt; 2. This monolithic block of functions is called by external processes through so-called SAPIs (&lt;b&gt;server application programming interface&lt;/b&gt;). For example, when you call php from the command line, you&amp;#39;re using the CLI SAPI (you can find its source in &amp;quot;/srv/src/php-5.2.9/sapi/cli&amp;quot;. Similarly, the apache process communicates with the php module through the apache2handler SAPI)&lt;br&gt; 3. Feel free to ask, since I&amp;#39;m pretty bored right now of so much writing. But ask smartly, or I will ignore your comment :)&lt;br&gt;4. Try to look at the source code for a few days before asking. Use fgrep to find functions in the source code, set breakpoints on them, use backtrace. If you need a deeper look at how a function works, set a breakpoint on the function above it in the backtrace (or have a look at the source code, and set a breakpoint on &amp;quot;file.c:&amp;lt;line&amp;gt;&amp;quot;), kill the process and run it again.&lt;br&gt; &lt;br&gt;Happy hacking! :)</description><link>http://originalcopy-on.blogspot.com/2009/03/apache-22-php-5-request-lifecycle.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-7337456359463606592</guid><pubDate>Mon, 16 Mar 2009 22:10:00 +0000</pubDate><atom:updated>2009-03-16T23:10:14.315+01:00</atom:updated><title>How to decouple business logic from view logic and gain flexibility</title><description>A user asks: How can I improve the following code?&lt;br&gt;&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;  &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;lt;div id=&amp;quot;search_controls&amp;quot;&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;  &lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;$entities_tab = ( $search_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; != &amp;#39;entities&amp;#39;) ? &amp;quot;&amp;lt;a &lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;=\&amp;quot;index.&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;?tab=entities$&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_sm\&amp;quot;&amp;gt;Entities&amp;lt;/a&amp;gt;&amp;quot; : &amp;quot;Entities&amp;quot;;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; $entities_tab_class = ( $search_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; != &amp;#39;entities&amp;#39;) ? &amp;quot;inactive_tab&amp;quot; : &amp;quot;active_tab&amp;quot;;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; ?&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; class=&amp;quot;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; echo $entities_tab_class; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; echo $entities_tab; ?&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;  &lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;$products_tab = ( $search_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; != &amp;#39;products&amp;#39;) ? &amp;quot;&amp;lt;a &lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;=\&amp;quot;index.&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;?tab=products$&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_sm\&amp;quot;&amp;gt;Products&amp;lt;/a&amp;gt;&amp;quot; : &amp;quot;Products&amp;quot;;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; $products_tab_class = ( $search_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; != &amp;#39;products&amp;#39;) ? &amp;quot;inactive_tab&amp;quot; : &amp;quot;active_tab&amp;quot;;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; ?&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; class=&amp;quot;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; echo $products_tab_class; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; echo $products_tab; ?&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;  &lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;  &lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;$events_tab = ( $search_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; != &amp;#39;events&amp;#39;) ? &amp;quot;&amp;lt;a &lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;=\&amp;quot;index.&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;?tab=events$&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_sm\&amp;quot;&amp;gt;Events&amp;lt;/a&amp;gt;&amp;quot; : &amp;quot;Events&amp;quot;;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; $events_tab_class = ( $search_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; != &amp;#39;events&amp;#39;) ? &amp;quot;inactive_tab&amp;quot; : &amp;quot;active_tab&amp;quot;;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; ?&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; class=&amp;quot;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; echo $events_tab_class; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; echo $events_tab; ?&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;  &lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; = ( $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;param&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; == &amp;quot;b&amp;quot; or $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;param&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; == NULL ) ? &amp;quot;&amp;amp;&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;=a&amp;quot; : &amp;quot;&amp;amp;&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;=b&amp;quot; ;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_link_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;txt&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; = ( $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;param&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; == &amp;quot;b&amp;quot; or $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;param&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; == NULL ) ? &amp;quot;Advance Search&amp;quot; : &amp;quot;Basic Search&amp;quot; ;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; ?&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;    &amp;lt;a id=&amp;quot;link_search_type&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;=&amp;quot;index.&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; echo $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_tab.$&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;.$&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_sq; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; echo $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;sm&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;_link_&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;&quot;&gt;txt&lt;/span&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;  &lt;br style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;background-color: rgb(255, 255, 255);&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;&lt;br&gt;A: separate the business logic from the view logic. The business logic should be restricted to working on data structures.&lt;br&gt; In the example above, you&amp;#39;re generating some &amp;quot;search tabs&amp;quot; which look quite similar. So instead of mixing the decisional code,&lt;br&gt; like deciding what html code each $*_tab should contain ($entities_tab, $products_tab, $events_tab), you should aggregate&lt;br&gt;all these into one place, since they are all the same thing: tabs. For this, you could use classes or arrays.&lt;br&gt;  &lt;br&gt;For the sake of simplicity, I&amp;#39;ll use arrays.&lt;br&gt;&lt;br&gt;So first ask yourself what does every tab have? It has a label and an internal representation. For example, the entities tab above&lt;br&gt;has the label &amp;quot;Entities&amp;quot; and the internal name &amp;quot;entities&amp;quot;, which is used for URL generation. You could also consider that a tab&lt;br&gt; has a content, but in our minimal example all the tabs get their content based on the other two properties mentioned above&lt;br&gt;&lt;br&gt;So our $search_controls would look like this:&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt;  $search_controls = array(&lt;br&gt;    array(&lt;br&gt;        &amp;#39;name&amp;#39; =&amp;gt; &amp;#39;entities&amp;#39;,&lt;br&gt;        &amp;#39;label&amp;#39; =&amp;gt; &amp;#39;Entities&amp;#39;&lt;br&gt;    ),&lt;br&gt;    array(&lt;br&gt;        &amp;#39;name&amp;#39; =&amp;gt; &amp;#39;products&amp;#39;,&lt;br&gt;          &amp;#39;label&amp;#39; =&amp;gt; &amp;#39;Products&amp;#39;&lt;br&gt;    ),&lt;br&gt;    array(&lt;br&gt;        &amp;#39;name&amp;#39; =&amp;gt; &amp;#39;events&amp;#39;,&lt;br&gt;        &amp;#39;label&amp;#39; =&amp;gt; &amp;#39;events&amp;#39;&lt;br&gt;    )&lt;br&gt;);&lt;br&gt;&lt;/blockquote&gt;&lt;br&gt;Now we also have the &amp;quot;search control&amp;quot; itself: a &amp;lt;div&amp;gt; embedding the search controls and a link for the &amp;quot;search type&amp;quot;.&lt;br&gt;  Beside, a search control also has an &amp;quot;active tab&amp;quot; - that&amp;#39;s a name of an existing tab which should have special visualization hints. So let&amp;#39;s make up a $search_control containing this meta data (meta - which describes something, as such here &amp;quot;meta data&amp;quot; means additional data&lt;br&gt; which describes the data structure itself). So we&amp;#39;re going to come up with something like this:&lt;br&gt;&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;&quot; class=&quot;gmail_quote&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;$search_control = array(&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;    &amp;#39;active&amp;#39; =&amp;gt; &amp;#39;entities&amp;#39;,&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;    &amp;#39;id&amp;#39; =&amp;gt; &amp;#39;search_controls&amp;#39;,&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;    &amp;#39;controls&amp;#39; =&amp;gt; array(&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;        array(&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;            &amp;#39;name&amp;#39; =&amp;gt; &amp;#39;entities&amp;#39;,&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;            &amp;#39;label&amp;#39; =&amp;gt; &amp;#39;Entities&amp;#39;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;        ),&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;        array(&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;            &amp;#39;name&amp;#39; =&amp;gt; &amp;#39;products&amp;#39;,&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;            &amp;#39;label&amp;#39; =&amp;gt; &amp;#39;Products&amp;#39;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;        ),&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;        array(&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;            &amp;#39;name&amp;#39; =&amp;gt; &amp;#39;events&amp;#39;,&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;            &amp;#39;label&amp;#39; =&amp;gt; &amp;#39;events&amp;#39;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;        )&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;    )&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;);&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;/blockquote&gt;  &lt;br&gt;Now this looks nice. We have invented an internal representation for the data that&amp;#39;s going to be rendered.&lt;br&gt;But why would you want to do this, after all? The answer is it&amp;#39;s much more flexible. If sometime, in the future,&lt;br&gt;  you decide that you want to save the &amp;quot;search tabs&amp;quot; in your database, or let&amp;#39;s push it even further, if you &lt;br&gt;decide that every user can define his own favourite &amp;quot;search tabs&amp;quot;, you will be able to pull this data from the database&lt;br&gt;  without even touching the generation of the html code!&lt;br&gt;&lt;br&gt;Beside, you could use this to define new types of &amp;quot;controls&amp;quot; like the &amp;quot;search control&amp;quot; by using the same algorithm -&lt;br&gt;which means less code, which in turns means less hassle and less room for bugs!&lt;br&gt;  &lt;br&gt;The reusage of code is especially flexible if you&amp;#39;re not going to store the internal representation in multi-dimensional&lt;br&gt;arrays as above, but in classes.&lt;br&gt;&lt;br&gt;Now enough talking, here&amp;#39;s how a function for rendering the &amp;quot;search control&amp;quot; - or any such &amp;quot;control&amp;quot; for that matter of fact,&lt;br&gt;  could look like:&lt;br&gt;&lt;br&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;&quot; class=&quot;gmail_quote&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;function render_control($controls) {&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;    $r = &amp;#39;&amp;lt;div id=&amp;quot;&amp;#39;.$controls[&amp;#39;id&amp;#39;].&amp;#39;&amp;quot;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;&amp;gt;&amp;#39;;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;foreach&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;($controls[&amp;#39;controls&amp;#39;] as $control) {&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;        if($controls[&amp;#39;active&amp;#39;] === $control[&amp;#39;name&amp;#39;]) {&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;            $class = &amp;#39;active_tab&amp;#39;;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;            $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;innerHtml&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt; = $control[&amp;#39;label&amp;#39;];&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;        }&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;        else {&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;            $class = &amp;#39;inactive_tab&amp;#39;;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt; &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;            $&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;innerHtml&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt; = &amp;#39;&amp;lt;a &lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;=&amp;quot;index.&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;php&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;?tab=&amp;#39;.$control[&amp;#39;name&amp;#39;].&amp;#39;&amp;quot;&amp;gt;&amp;#39;.$control[&amp;#39;label&amp;#39;].&amp;#39;&amp;lt;/a&amp;gt;&amp;#39;;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;        }&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;        $r .= &amp;#39;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt; class=&amp;quot;&amp;#39;.$class.&amp;#39;&amp;quot;&amp;gt;&amp;#39;.$&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;innerHtml&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;.&amp;#39;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;&amp;gt;&amp;#39;;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;    }&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;    $r .= &amp;#39;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: courier new,monospace;&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&amp;#39;;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;  &lt;span style=&quot;font-family: courier new,monospace; background-color: rgb(255, 255, 255);&quot;&gt;    return $r;&lt;/span&gt;&lt;br style=&quot;font-family: courier new,monospace;&quot;&gt;&lt;span style=&quot;font-family: courier new,monospace;&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;/blockquote&gt; &lt;br&gt;&lt;br&gt;As you can see, I did leave out some things, like the &amp;quot;$url_sm&amp;quot;, since I don&amp;#39;t know how and where that variable comes from.&lt;br&gt; However, if you need more meta data in your control, simply invent a new parameter to the function or a new member in $controls, like&lt;br&gt;$controls[&amp;#39;id&amp;#39;]. You could also add a &amp;#39;title&amp;#39; for the &amp;lt;div&amp;gt; box itself, or more parameters which allow further customization&lt;br&gt; of the html tags used instead of the standard &amp;#39;&amp;lt;ul&amp;gt;&amp;#39;, &amp;#39;&amp;lt;li&amp;gt;&amp;#39;, &amp;#39;&amp;lt;a&amp;gt;&amp;#39; and so on.&lt;br&gt;&lt;br&gt;The sky is the limit!&lt;br&gt;&lt;br&gt;The lesson is: a smart programmer not only writes code, but he writes it in a reusable and flexible way. Now you can throw data at&lt;br&gt; render_control() without writing any piece of html, it will simply generate it.&lt;br&gt;&lt;br&gt;Isn&amp;#39;t that a relief? Be smart, write less, code more! </description><link>http://originalcopy-on.blogspot.com/2009/03/how-to-decouple-business-logic-from_16.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-4213061177005120596</guid><pubDate>Thu, 05 Mar 2009 21:37:00 +0000</pubDate><atom:updated>2009-03-05T22:48:38.439+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">php</category><category domain="http://www.blogger.com/atom/ns#">practices</category><title>Small PHP optimizations anyone can (and should) do</title><description>As a forum moderator on the PHP area of the &lt;a href=&quot;http://forum.softpedia.com&quot;&gt;softpedia forum&lt;/a&gt; (Romanian), I&#39;ve worked toghether with the community on putting together a list of do&#39;s and dont&#39;s throughout the time.&lt;br /&gt;&lt;br /&gt;Recently I&#39;ve run across &lt;a href=&quot;http://tools.yap-project.com/counter/click.php?id=8&quot;&gt;this&lt;/a&gt; post which is by far much more complete, as ABU NAWIM MOHAMMAD SAIFUL ISLAM from Bangladesh describes.&lt;br /&gt;&lt;br /&gt;Feel free to follow his advices, they&#39;re worth taking into account.</description><link>http://originalcopy-on.blogspot.com/2009/03/small-php-optimizations-anyone-can-and.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-1817215240236238990</guid><pubDate>Thu, 05 Mar 2009 14:10:00 +0000</pubDate><atom:updated>2009-10-29T12:41:22.301+01:00</atom:updated><title>Checking whether an entry in a zip archive is a file or a directory  with PHP</title><description>When processing .zip files with PHP, a common problem is differentiating between files and directories inside the archive. A simple code like this shows what the zip extension returns for each entry:&lt;br /&gt;&lt;br /&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; font-family: courier new,monospace;&quot; class=&quot;gmail_quote&quot;&gt; &amp;lt;?php&lt;br /&gt;$zip = zip_open(&#39;foo.zip&#39;);&lt;br /&gt;while($entry = zip_read($zip)) {&lt;br /&gt;    $entry_name = zip_entry_name($entry);&lt;br /&gt;    echo &#39;name: &#39;,$entry_name,PHP_EOL;&lt;br /&gt;}&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;After having a closer look at the output, it becomes obvious that directory entries end in a trailing slash, and as such, we&#39;ll obviously do something like this:&lt;br /&gt;&lt;br /&gt;&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;&quot; class=&quot;gmail_quote&quot;&gt;&lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;&amp;lt;?php&lt;/span&gt;  &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;$zip = zip_open(&#39;foo.zip&#39;);&lt;/span&gt; &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;while($entry = zip_read($zip)) {&lt;/span&gt;  &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;    $entry_name = zip_entry_name($entry);&lt;/span&gt; &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;    echo &#39;name: &#39;,$entry_name,PHP_EOL;&lt;/span&gt;  &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;    if(&#39;/&#39; === substr($entry_name,-1)) {&lt;/span&gt; &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;        echo &#39;is a file&#39;,PHP_EOL;&lt;/span&gt;  &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;    }&lt;/span&gt; &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;    else {&lt;/span&gt;  &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;        echo &#39;is a directory&#39;,PHP_EOL;&lt;/span&gt; &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;    }&lt;/span&gt;  &lt;span style=&quot;font-family:courier new,monospace;&quot;&gt;}&lt;/span&gt;&lt;/blockquote&gt;&lt;div&gt;&lt;br /&gt;Easy huh? :-)&lt;br /&gt;&lt;/div&gt;</description><link>http://originalcopy-on.blogspot.com/2009/03/checking-whether-entry-in-zip-archive.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-5065654829307502706</guid><pubDate>Tue, 03 Mar 2009 13:42:00 +0000</pubDate><atom:updated>2009-10-29T12:39:38.782+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">php</category><category domain="http://www.blogger.com/atom/ns#">practices</category><title>Fetching tree data with PHP from MySQL with only one query</title><description>Sometimes you may want to store some tree data in your database, for example navigation menus, where each of the &quot;node&quot; has children.&lt;br /&gt;&lt;br /&gt;The most obvious way of fetching it would be of course to model the fetching algorithm similar to the nature of the data itself: recursively.&lt;br /&gt;&lt;br /&gt;There is one problem with this method though, large trees will require dozens of queries, not to mention the storage in your client&#39;s runtime (eg. PHP).&lt;br /&gt;&lt;br /&gt;Here is another method for describing tree data, which requires &lt;span style=&quot;font-weight: bold;&quot;&gt;at most three queries&lt;/span&gt; to fetch just about anything you could fetch with hundreds if not thousands of recursive calls.&lt;br /&gt;&lt;br /&gt;First, the structure of the database table would look like this&lt;br /&gt;&lt;blockquote style=&quot;font-family: courier new;&quot;&gt;&lt;br /&gt;CREATE TABLE IF NOT EXISTS `&#39;.TABLE_NAV.&#39;` (&lt;br /&gt;`id` int(10) unsigned NOT NULL auto_increment,&lt;br /&gt;`order` int(10) unsigned NOT NULL COMMENT \&#39;in which order to sort within the same parent\&#39;,&lt;br /&gt;`indent` int(10) unsigned NOT NULL COMMENT \&#39;the indentation level\&#39;,&lt;br /&gt;`data` varchar(255) collate ascii_bin NOT NULL COMMENT \&#39;a message to show\&#39;,&lt;br /&gt;PRIMARY KEY  (`id`),&lt;br /&gt;KEY `order` (`order`,`indent`)&lt;br /&gt;) ENGINE=MyISAM DEFAULT CHARSET=ascii COLLATE=ascii_bin AUTO_INCREMENT=1;&lt;/blockquote&gt;The &quot;order&quot; column is there so you don&#39;t have to mess with the auto incrementing id column. It allows you to reorder the nodes individually.&lt;br /&gt;The &quot;indent&quot; defines the &quot;indentation&quot; of the node - you may think of it denoting the node&#39;s deepness inside a tree.&lt;br /&gt;&lt;br /&gt;The simplest code would then look like this:&lt;br /&gt;&lt;blockquote style=&quot;font-family: courier new;&quot;&gt;$res = mysql_query(&#39;SELECT * FROM `&#39;.TABLE_NAV.&#39;` ORDER BY `order`,`indent`&#39;);&lt;br /&gt;echo &#39;&amp;lt;div style=&quot;border: 1px solid black&quot;&amp;gt;&amp;lt;pre&amp;gt;&#39;,PHP_EOL;&lt;br /&gt;while($item = mysql_fetch_assoc($res)) {&lt;br /&gt;   echo str_repeat(&#39;   &#39;,$item[&#39;indent&#39;]), $item[&#39;data&#39;], PHP_EOL;&lt;br /&gt;}&lt;br /&gt;echo &#39;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&#39;,PHP_EOL;&lt;/blockquote&gt;I&#39;ve added some indentation so you can get a feel for it.&lt;br /&gt;&lt;br /&gt;You could also group the tree data into &quot;navigation panes&quot; by just adding a new column to it. Every node should then contain an id (not to be confounded with the id column, you just make up some common identifiers, a number would be the best) describing to which &quot;navigation pane&quot; that node belongs to.&lt;br /&gt;&lt;br /&gt;The upside of this method is efficiency while fetching the data. The downside is you will need more work when deleting or reordering subtrees. That shouldn&#39;t be an issue for common cases, as you usually don&#39;t modify or delete the tree that often.&lt;br /&gt;&lt;br /&gt;This organisatory model does not protect your data&#39;s consistency. For example, when reordering a node on it&#39;s own &quot;indentation level&quot;, you will also need to reorder its children (to increment/decrement `order` by the same amount). The same goes for `indent`.&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://tools.yap-project.com/counter/click.php?id=1&quot;&gt;Here&lt;/a&gt; is a working PoC, just&lt;br /&gt;&lt;ol&gt;&lt;li&gt;edit the &quot;&amp;lt;edit me&amp;gt;&quot; values&lt;edit&gt;&lt;edit me=&quot;&quot;&gt; in dbconf.php&lt;/edit&gt;&lt;/edit&gt;&lt;/li&gt;&lt;li&gt;run install.php - no message should appear :-)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;view index.php&lt;/li&gt;&lt;/ol&gt;I hope you&#39;ve found it useful and come back.&lt;br /&gt;&lt;br /&gt;Note: the presented code is only a proof of concept, and there&#39;s much room for improvement. As such, I don&#39;t assume any responsability for any harms the code may do to your system. I do, however, assume the responsability for the concept itself.</description><link>http://originalcopy-on.blogspot.com/2009/03/fetching-tree-data-with-php-from-mysql.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-5516531193020320989</guid><pubDate>Sun, 19 Oct 2008 12:37:00 +0000</pubDate><atom:updated>2009-10-29T12:36:35.971+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">how things work</category><category domain="http://www.blogger.com/atom/ns#">php</category><title>Understanding how the Internet and the Web works, for PHP programmers</title><description>In this article I&#39;ll try to explain how the platform on which you build websites works. Please read it carefully, take a deep breath from time to time, and try to brainstorm around with what you read.&lt;br /&gt;And please, PLEASE be very careful about the &lt;span style=&quot;font-weight: bold;&quot;&gt;terminology&lt;/span&gt;. It will help you a lot - for example when you ask for help and need to be precise in what you ask.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Internet is &quot;made&quot; of so-called &lt;span style=&quot;font-weight: bold;&quot;&gt;services&lt;/span&gt;. Some of the most known services are: the web (for inter-connected documents), pop3 or imap (for e-mail reading and writing), irc (for live chat), file transfers (FTP).&lt;br /&gt;&lt;br /&gt;In order to use an Internet service, you need a special program called &lt;span style=&quot;font-weight: bold;&quot;&gt;client&lt;/span&gt;, which is specially designed for that service. So you&#39;ve got a multitude of services, each with its own types of clients. The service called &lt;span style=&quot;font-weight: bold;&quot;&gt;World Wide Web&lt;/span&gt; for example, is so widely used, that humans gave a special name to web clients: &lt;span style=&quot;font-weight: bold;&quot;&gt;browsers&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Many software vendors created their own browsers. And so, now we have programs like: Microsoft Internet Explorer, Mozilla Firefox, Opera, Google Chrome, Safari, among the most used.&lt;br /&gt;&lt;br /&gt;But why do we need a client after all? Enter the world of protocols.&lt;br /&gt;Imagine the following scenario, which is seen in (almost) every service:&lt;br /&gt;&lt;br /&gt;There is a computer sitting somewhere on the network and waiting for requests. These computers are called &lt;span style=&quot;font-weight: bold;&quot;&gt;servers&lt;/span&gt;. On the server, which is the physical machine, there is a program that runs in background which processes the requests sent by the clients. This program is called &lt;span style=&quot;font-weight: bold;&quot;&gt;daemon&lt;/span&gt;. The administrator of the server may say colloquially that &quot;&lt;span style=&quot;font-style: italic;&quot;&gt;the server is up and running&lt;/span&gt;&quot;, but what she actually means is: &quot;&lt;span style=&quot;font-style: italic;&quot;&gt;the server is connected to the Internet and the daemon is listening for new requests&lt;/span&gt;&quot;.&lt;br /&gt;&lt;br /&gt;But keep in mind that every service is different (a little bit later I&#39;ll explain you why). So, just as we&#39;ve got different clients for different services, there are also different daemons for each service. Example of such daemons: &lt;span style=&quot;font-weight: bold;&quot;&gt;Apache&lt;/span&gt; and IIS for the www service, UnrealIRCd for live chat via IRC, sendmail for e-mail, etc. Remember: these are the actual executable files, just like &quot;firefox.exe&quot;. In contrast to that, the notions of client and server are generic classifications for types of software.&lt;br /&gt;&lt;br /&gt;Now, back to the initial question: why do we need clients and daemons for every existing service on the Internet? Because these two types of programs communicate in a language called protocol, and each service has its own protocols. You may wonder why are they different? Well, because every service has a different aim. For example, writing e-mail is not the same as publishing a document on the web: an e-mail needs one or more receivers, but a document on the web will be visible for everyone and doesn&#39;t have a receiver per se.&lt;br /&gt;&lt;br /&gt;For example, the service World Wide Web, or shortly &lt;span style=&quot;font-style: italic;&quot;&gt;the web&lt;/span&gt;, or &lt;span style=&quot;font-style: italic;&quot;&gt;www&lt;/span&gt;, uses a protocol called &lt;span style=&quot;font-weight: bold;&quot;&gt;h&lt;/span&gt;yper&lt;span style=&quot;font-weight: bold;&quot;&gt;t&lt;/span&gt;ext &lt;span style=&quot;font-weight: bold;&quot;&gt;t&lt;/span&gt;ransfer &lt;span style=&quot;font-weight: bold;&quot;&gt;p&lt;/span&gt;rotocol (abbr. http). This is the beginning of every &quot;web address&quot; you enter in your browser, i.e. &quot;http://&quot;. You may have also seen &quot;news://&quot;, &quot;mailto:&quot; or &quot;irc://&quot;, for different protocols out there, of which every is the communication language of a specific service. The address is called technically URL or URI.&lt;br /&gt;&lt;br /&gt;Usually, on a server (I repeat: this is the physical machine) there may be more than one daemon running concurrently and listening for requests. But how should the operating system of that server know which connection goes to which daemon? The OS itself has no notion of protocols, it only recognizes &quot;connection requests&quot; (at the TCP/IP level) and must forward them to the right program (to the right daemon).&lt;br /&gt;&lt;br /&gt;The secret lies in the so-called &lt;span style=&quot;font-weight: bold;&quot;&gt;ports&lt;/span&gt;. A port is a number between 1 and 65535. When the client (e.g. Firefox) initializes the TCP/IP connection, it also writes the number of the port &quot;via&quot; which it wants to connect. Saying &quot;via&quot; is not quite correct, since the port is only a number which serves association of programs (here: daemons) and incoming connections at the operating system (abbr. OS) level, but you may imagine a port as a &quot;communication channel&quot; though, for the sake of clearness.&lt;br /&gt;&lt;br /&gt;Do you have enough theory? Let&#39;s look at how the stuff I explained until now look in real life, with a hands-on example.&lt;br /&gt;&lt;br /&gt;I&#39;m going to show you what a browser does when you type in your address bar &quot;http://www.google.com&quot;. For this, I need to use a program called telnet. It is really basic, all it does is to create a &lt;span style=&quot;font-weight: bold;&quot;&gt;socket&lt;/span&gt; (read: a connection) via TCP/IP on the port I tell it to. It has no notion of protocols, but that&#39;s exactly what we need, since we&#39;re going to talk to the server in the language HTTP &lt;span style=&quot;font-style: italic;&quot;&gt;manually&lt;/span&gt; - something that the browser would do automatically for us.&lt;br /&gt;&lt;br /&gt;Open the CLI of your operating system (CLI - &lt;span style=&quot;font-style: italic;&quot;&gt;command line interface&lt;/span&gt;; in Windows XP, this can be achieved by Start -&gt; run -&gt; type in &quot;&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_0&quot;&gt;cmd&lt;/span&gt;&quot;; on *NIX, this is the shell, accessible through a terminal). A black and unfriendly window will appear.&lt;br /&gt;&lt;br /&gt;Type in the following:&lt;br /&gt;&lt;blockquote style=&quot;font-family: courier new;&quot;&gt;telnet google.com 80&lt;/blockquote&gt;80 is the standard port for the www service.&lt;br /&gt;&lt;br /&gt;After the connection is established, type this text, but type it quickly:&lt;br /&gt;&lt;blockquote&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;GET / HTTP/1.1&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;Host: www.google.com&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/blockquote&gt;Attention: the request is case sensitive, which means lower/UPPER case is important!&lt;br /&gt;Also note that you must press return twice after &quot;www.google.com&quot;, that is, &lt;span style=&quot;font-weight: bold;&quot;&gt;you must mark the end of the request with an empty line&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;The entire communication between the client (here: telnet) and the server (here: google.com) on port 80 would then look something like:&lt;br /&gt;&lt;blockquote&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;GET&lt;/span&gt; / HTTP/1.1&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Host&lt;/span&gt;: www.google.com&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;HTTP/1.1 302 Found&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Location&lt;/span&gt;: http://www.google.at/&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Cache-Control&lt;/span&gt;: private&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Content-Type&lt;/span&gt;: text/html; &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_1&quot;&gt;charset&lt;/span&gt;=&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_2&quot;&gt;UTF&lt;/span&gt;-8&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Set-Cookie&lt;/span&gt;: PREF=ID=f8&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_3&quot;&gt;df&lt;/span&gt;1f836&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_4&quot;&gt;de&lt;/span&gt;11e39:TM=1224414258:&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_5&quot;&gt;LM&lt;/span&gt;=1224414258:S=l1a-I88j2a0&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_6&quot;&gt;boHIM&lt;/span&gt;; expires=Tue, 19-Oct-2010 11:04:18 GMT; path=/; domain=.google.com&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Date&lt;/span&gt;: Sun, 19 Oct 2008 11:04:18 GMT&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Server&lt;/span&gt;: &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_7&quot;&gt;gws&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Content-Length&lt;/span&gt;: 218&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=utf-8&quot;&amp;gt;&lt;br /&gt;&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;&lt;br /&gt;&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;&lt;br /&gt;The document has moved&lt;br /&gt;&amp;lt;A HREF=&quot;http://www.google.at/&quot;&amp;gt;here&amp;lt;/A&amp;gt;.&lt;br /&gt;&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;&lt;br /&gt;Connection closed by foreign host.&lt;br /&gt;&lt;/span&gt;&lt;/blockquote&gt;The first line of our http request contains the request method (here we used GET; another method would be POST, which you may have heard of if you programmed in &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_25&quot;&gt;PHP&lt;/span&gt;). The slash &quot;/&quot; is the resource we request. If we wanted to access the URL http://www.google.com/support/, then we had to enter &quot;GET /support/ HTTP/1.1&quot;.&lt;br /&gt;&lt;br /&gt;&quot;HTTP&quot; stands for the protocol used, and 1.1 is the version of the protocol.&lt;br /&gt;&lt;br /&gt;The &lt;span style=&quot;font-weight: bold;&quot;&gt;HTTP field&lt;/span&gt; &quot;Host&quot; tells the google daemon that we&#39;re referring to the &quot;www&quot; server. That&#39;s the www in &quot;www.google.com&quot;, because google has other servers as well, like &quot;mail.google.com&quot;, and the daemon needs to know which one we&#39;re sending the request to. Note: this association happens now at the application level (at the daemon level), as opposed to the ports, which have a meaning at the OS level.&lt;br /&gt;&lt;br /&gt;What we sent to the server are called &lt;span style=&quot;font-weight: bold;&quot;&gt;the request headers&lt;/span&gt;. After them comes &lt;span style=&quot;font-weight: bold;&quot;&gt; the answer headers&lt;/span&gt;, and then &lt;span style=&quot;font-weight: bold;&quot;&gt;the answer&lt;/span&gt; itself, if any. All of these three sections are separated by an empty line. That&#39;s the reason you had to press return twice when sending the http request. The communication language http (i.e. the http protocol) specifies this.&lt;br /&gt;&lt;br /&gt;From the http response I can see it tells me it found what I asked from it, and the code is 302. Beside that, the server is kind enough to tell me that the location is http://www.google.at. The daemon detects, based on my &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_28&quot;&gt;IP&lt;/span&gt; address, that my geographical location is Austria, so may it do for you too. So you need to create a new HTTP request on port 80 to where it tells you to, just as I showed you above.&lt;br /&gt;&lt;br /&gt;You will finally get the &lt;span style=&quot;font-weight: bold;&quot;&gt;HTML&lt;/span&gt; code of the website. From this point onwards, a web browser would do things like:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;rendering&lt;/span&gt; the &lt;span style=&quot;font-weight: bold;&quot;&gt;markup code&lt;/span&gt; in its &lt;span style=&quot;font-weight: bold;&quot;&gt;canvas&lt;/span&gt;&lt;/li&gt;&lt;li&gt;looking for external resources like images, frames/&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_29&quot;&gt;iframes&lt;/span&gt;, javascript scripts, &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_30&quot;&gt;css&lt;/span&gt; style sheets etc, and creating new http requests for each of them; after this step, images would appear as being part of the html document, but in fact they are separate resources, with their own URLs&lt;br /&gt;&lt;/li&gt;&lt;li&gt;executing any client-side codes, like javascript scripts&lt;/li&gt;&lt;/ul&gt; But since we&#39;re using telnet as a client, which has no knowledge about what HTML means, it simply shows us the markup and then closes the connection.&lt;br /&gt;&lt;br /&gt;Feel free to play around with what you&#39;ve learned so far, and ask if something is unclear.&lt;br /&gt;Here are some questions which may help you brainstorming:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Why is javascript not a reliable way of validating input?&lt;/li&gt;&lt;li&gt;Why can&#39;t you trust some information in $_SERVER[], like $_SERVER[&#39;HTTP_USER_AGENT&#39;]?&lt;/li&gt;&lt;li&gt;Why does the error &quot;output already sent&quot; actually exist? ( you probably know it already, it appears when you don&#39;t call session_start() appropiately)&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;</description><link>http://originalcopy-on.blogspot.com/2008/10/understanding-how-internet-and-web.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-4376605651658003353</guid><pubDate>Wed, 15 Oct 2008 10:22:00 +0000</pubDate><atom:updated>2009-10-29T12:25:17.142+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">c/c++</category><category domain="http://www.blogger.com/atom/ns#">german</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">php</category><title>Die interne Funktionsweise von PHP</title><description>&lt;span style=&quot;font-weight:bold;&quot;&gt;&lt;br /&gt;Abstract&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This paper looks into the internal workings of PHP, a programming language that is used to generate and control the output of a webserver. In order to understand the fundamental internal processes of PHP and their causes, the structure of the Internet with focus on the Web is herein described. With this knowledge it is possible to easily understand the roles of PHP&#39;s components.&lt;br /&gt;The execution flow of PHP is dissected alongside the layout of its source code repository and a couple of key functions and their roles. Syntactic analysis of the PHP script at runtime is  pertinent to an understanding of the system and thus the importance of grammar is also adressed. Since PHP is very complex and a complete study would go beyond the scope of this paper, methods of independent analysis are also presented.&lt;br /&gt;The execution flow of PHP is dissected in parallel with the layout of the PHP source code repository and a couple of key functions and their roles, are presented. Syntactic analysis of the PHP script at runtime is pertinent to an understanding of the system as a whole and thus the importance of grammar is also adressed.&lt;br /&gt;Lastly, it is shown with an extension how the PHP-runtime can be enhanced.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Zusammmenfassung&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Diese Arbeit setzt sich mit der internen Funktionsweise von PHP auseinander, eine Programmiersprache die zur Generierung bzw. Steuerung der Ausgabe eines Webservers eingesetzt wird. Um grundlegende PHP-interne Vorgänge und ihre Ursachen zu verstehen wird zuerst der Aufbau des Internets mit Schwerpunkt auf das Web nähergebracht. Mit diesem Wissen können die Rollen der Komponenten, aus denen PHP besteht, leichter verstanden werden.&lt;br /&gt;Der Ausführungsfluss von PHP wird zeitgleich mit der Auslegung des PHP-Quelldepots untersucht und ein paar Schlüsselfunktionen und deren Rollen kennengelernt. Eine besondere Funktion zur Laufzeit spielt die Syntaxanalyse des PHP-Skriptums, und deshalb wird auch auf die Bedeutung der Grammatiken eingegangen. Da PHP sehr komplex ist und eine vollständige Untersuchung die Rahmen dieser Arbeit sprengen würde, werden Methoden zur selbstständigen Analyse des PHP präsentiert.&lt;br /&gt;Schließlich wird anhand einer praktischen Erweiterung gezeigt, wie die Laufzeitumgebung von PHP weiterentwickelt werden kann.&lt;br /&gt;&lt;br /&gt;Download it &lt;a href=&quot;http://snipurl.com/4dnri-oc&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;Sorry, it&#39;s in German only. Aber du kannst gerne bei der Übersetzung helfen, sodass viele andere Programmierer rund um den Globus davon profitieren können :-)</description><link>http://originalcopy-on.blogspot.com/2008/10/die-interne-funktionsweise-von-php.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-8735856125722908391</guid><pubDate>Tue, 14 Oct 2008 17:36:00 +0000</pubDate><atom:updated>2009-03-04T10:09:45.398+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">c/c++</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">poc</category><category domain="http://www.blogger.com/atom/ns#">the right way</category><title>How to Create a Math Calculator in C, the right Way</title><description>&lt;div dir=&quot;ltr&quot;&gt;Here is a small PoC on how to build a calculator with a lexer (re2c) and a parser generator (lemon) in C. It supports the four basic mathematical operations and parentheses.&lt;br /&gt;&lt;br /&gt;Thanks to &lt;a href=&quot;http://www.macvicar.net/&quot;&gt;MacVicar&lt;/a&gt; for his help.&lt;br /&gt;&lt;br /&gt;I&#39;m not sure if it works on other platforms beside Linux, as that&#39;s all I have (not!), but hey, feel free to improve it! :-)&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://tools.yap-project.com/counter/click.php?id=6&quot;&gt;calc-0.1&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;</description><link>http://originalcopy-on.blogspot.com/2008/10/how-to-make-math-calculator-right-way.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>10</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-3273203586399662214</guid><pubDate>Thu, 09 Oct 2008 21:51:00 +0000</pubDate><atom:updated>2008-10-14T23:47:12.863+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">php</category><category domain="http://www.blogger.com/atom/ns#">setting up</category><category domain="http://www.blogger.com/atom/ns#">the right way</category><title>How to set PHP on a development box, the right way</title><description>&lt;div dir=&quot;ltr&quot;&gt;PHP is not set by default for a development machine, but for public servers. This is a list of settings I recommend you to put in php.ini on your development box:&lt;br /&gt;&lt;blockquote&gt;error_reporting = E_ALL|E_STRICT&lt;br /&gt;display_errors = On&lt;br /&gt;short_open_tag = Off&lt;br /&gt;asp_tags = Off&lt;br /&gt;display_startup_errors = On&lt;br /&gt;&lt;br /&gt;magic_quotes_gpc = Off&lt;br /&gt;output_buffering = Off&lt;br /&gt;allow_call_time_pass_reference = Off&lt;br /&gt;zlib.output_compression = Off&lt;br /&gt;track_errors = On&lt;br /&gt;register_globals = Off&lt;br /&gt;date.timezone should be set according to your time zone. A list of timezones can be found at http://us3.php.net/timezones&lt;br /&gt;session.auto_start = 0&lt;br /&gt;tidy.clean_output = Off&lt;br /&gt;&lt;br /&gt;implicit_flush = Off&lt;br /&gt;log_errors = On&lt;br /&gt;ignore_repeated_errors = On&lt;br /&gt;report_memleaks = On&lt;br /&gt;&lt;/blockquote&gt;You may also want to test your scripts with&lt;br /&gt;&lt;blockquote&gt;safe_mode = On&lt;br /&gt;&lt;/blockquote&gt;Do not forget to restart the http daemon after saving your changes to php.ini.&lt;br /&gt;&lt;br /&gt;Hint: you don&#39;t have to look manually for every configuration directive, use your ascii text editor&#39;s &quot;find&quot; option, which can usually be achieved by pressing CTRL+F in the editor.&lt;br /&gt;&lt;/div&gt;</description><link>http://originalcopy-on.blogspot.com/2008/10/how-to-set-php-on-development-box-right_09.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2712653655982926835.post-8391779252053441153</guid><pubDate>Thu, 09 Oct 2008 10:00:00 +0000</pubDate><atom:updated>2008-10-09T14:03:03.676+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">announce</category><category domain="http://www.blogger.com/atom/ns#">romanian</category><title>My First Post</title><description>Toată lumea face în zilele noastre jurnalism de maidan (nu că cel &quot;profesional&quot; ar fi mai bun), deci am decis să încerc şi eu.&lt;br /&gt;&lt;br /&gt;Nu ştiu cât timp îi voi dedica, dar aici intenţionez să postez sfaturi de programare, tips and tricks, probleme de care m-am lovit şi soluţiile lor, gânduri despre ce ne înconjoară, ş.a.m.d.&lt;br /&gt;&lt;br /&gt;PS: nu, nu &quot;personal blog&quot;, şi nici &quot;m-am trezit, am mancat, m-am îmbrăcat ...&quot; ;)</description><link>http://originalcopy-on.blogspot.com/2008/10/my-first-post.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>2</thr:total></item></channel></rss>