<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Spampw's Weblog</title>
	<atom:link href="http://spampw.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://spampw.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Mon, 11 Aug 2008 08:15:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='spampw.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Spampw's Weblog</title>
		<link>http://spampw.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://spampw.wordpress.com/osd.xml" title="Spampw&#039;s Weblog" />
	<atom:link rel='hub' href='http://spampw.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Start VB</title>
		<link>http://spampw.wordpress.com/2008/08/11/start-vb/</link>
		<comments>http://spampw.wordpress.com/2008/08/11/start-vb/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 08:06:12 +0000</pubDate>
		<dc:creator>spampw</dc:creator>
				<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://spampw.wordpress.com/2008/08/11/start-vb/</guid>
		<description><![CDATA[&#160; Figure 1-9 The New Project dialog box—so many choices Your New Project dialog box may differ slightly depending on the features you chose to install with Visual Studio. The available projects are grouped by the description in the Project types field. For instance, Figure 1-9 shows the various default project types you can create [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=17&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h4>&nbsp;</h4>
<p><img height="368" src="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig09.jpg" width="600">
<p><b>Figure 1-9</b> The New Project dialog box—so many choices</p>
<p><span id="more-17"></span>
<p>Your <b>New Project</b> dialog box may differ slightly depending on the features you chose to install with Visual Studio. The available projects are grouped by the description in the <b>Project types</b> field. For instance, Figure 1-9 shows the various default project types you can create in Visual Basic, including <i>Windows Applications</i> (standard desktop applications for the Windows platform, <i>Class Libraries</i> (a DLL of class-defined features), and <i>Console Applications</i> (command-line text-based applications). To create a new application, first select the project type, select the <b>Template</b> to use, and finally enter the name of the new project in the <b>Name</b> field. Clicking the <b>OK</b> button creates a new project.
<p>To use the sample Chapter 1 project, select the <i>Start-to-Finish Visual Basic 2005</i> entry within the <i>Visual Basic</i> project type, and then select <i>Chapter 1 Sample</i> from the <b>Template</b> field (see Figure 1-10). Finally, click <b>OK</b> to create the new sample project.
<p><img height="159" src="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig10.jpg" width="600">
<p><b>Figure 1-10</b> Selecting the Chapter 1 Sample project
<p>Once the project loads, access the program&#8217;s main form by double-clicking on the <i>Form1.vb</i> file in the <b>Solution Explorer</b> (see Figure 1-11).
<p><img height="401" src="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig11.jpg" width="600">
<p><b>Figure 1-11</b> The main form of the sample application
<p>This default presentation of Visual Studio Professional Edition includes three editing components: (1) the main editing area, where the view of &#8220;Form1&#8243; appears; (2) the <b>Solution Explorer</b> panel, which provides access to all files included in the project; and (3) the <b>Properties</b> panel, which lets you edit various aspects of the currently selected item in the main editor area or elsewhere in the user interface.
<p>The sample project is pretty basic. It includes one form with a single action button. Clicking this button in the running application displays a simple message. Run the project by pressing the F5 key. When the main form appears, clicking on the <b>Go Ahead, Click Me</b> button to display the message in Figure 1-12 (goal, sweet goal).
<p><img height="110" src="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig12.jpg" width="164">
<p><b>Figure 1-12</b> Hello again, world!
<p>So, how about all of that complex code I had to write to develop this multifaceted application? It&#8217;s all there for the viewing. From the <b>Solution Explorer</b> panel, right-click on the <i>Form1.vb</i> entry, and select <b>View Code</b> from the shortcut menu. (As with most source code samples presented in this book, I have had to slightly adjust the code so that it displays properly on the printed page. Generally, this involves splitting a long logical line into two or more shorter ones.)
<pre>Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles Button1.Click
   MsgBox("Hello, World!")
  End Sub
End Class</pre>
<p>We&#8217;ll get into the intricacies of such code in later chapters, but here is the gist.</p>
<ul>
<li>
<p>The main form, <b><code>Form1</code></b>, is represented in code by a class, named <code>Form1</code>.</p>
<li>
<p>The form includes a command button named <b><code>Button1</code></b> that exposes a <code>Click</code> event. This event is handled by the <code>Button1_Click</code> procedure, a member of the <code>Form1</code> class.</p>
<li>
<p>The &#8220;event handler,&#8221; <code>Button1_Click</code>, includes a single statement, a &#8220;<code>MsgBox</code>&#8221; statement. This statement does the heavy lifting by presenting the ever-friendly message box to the world.</p>
</li>
</ul>
<p>That&#8217;s all of the code that I wrote for &#8220;<i>Form1.vb</i>&#8220;. It sure seems pretty short for all the work it does. There has to be more code hiding somewhere. And sure enough, there are actually half-a-dozen or so more files included in the project. Visual Studio hides these by default, since it manages some or all of the content in these files on your behalf. To view the files, click on the &#8220;Show All Files&#8221; button (the second toolbar button from the left in the <b>Solution Explorer</b> panel). Look at all those files! To see the additional files associated with <code>Form1</code>, expand it by clicking on the plus sign to its left (see Figure 1-13).</p>
<p><img height="406" src="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig13.jpg" width="600"></p>
<p><b>Figure 1-13</b> Viewing hidden files through the Solution Explorer</p>
<p>Double-click on the <i>Form1.Designer.vb</i> entry to see the code that Visual Studio automatically wrote for this form. (Dramatic pause.) Wow! Look at all of that scary code. Actually, it&#8217;s not that bad. By the end of this book, you will have a firm grasp on all of it. Here in Chapter 1, it&#8217;s not really necessary to comprehend it all, but there are a few interesting lines to note. I&#8217;m including line numbers to make it easier to find the matching code in Visual Studio. If you want to view line numbers in Visual Studio (Professional Edition instructions listed here):</p>
<ol>
<li>
<p>Select the <b>Tools</b> <b>-&gt;</b> <b>Options</b> menu item to display Visual Studio&#8217;s options.</p>
<li>
<p>Select <b>Text Editor -&gt;</b> <b>Basic</b> <b>-&gt;</b> <b>Editor</b> from the tree-view to the left. If the <b>Show all settings</b> field is checked, the last component in the tree-view will be <b>General</b>, not <b>Editor</b>.</p>
<li>
<p>Select (check) the <b>Line Numbers</b> field on the right.</p>
<li>
<p>Click <b>OK</b> to apply the changes.</p>
</li>
</ol>
<p>If you&#8217;re new to Visual Basic or .NET programming, don&#8217;t worry now if all this code doesn&#8217;t make sense; it will all become clear as you pass through the pages of this book.
<pre><b> 1</b> &lt;Global.Microsoft.VisualBasic.CompilerServices. _
    DesignerGenerated()&gt; _
<b> 2</b> Partial Public Class Form1

<b>20</b> &lt;System.Diagnostics.DebuggerNonUserCode()&gt; _
<b>21</b> Protected Overloads Overrides Sub Dispose _
    (ByVal disposing As Boolean)</pre>
<p>These lines show <i>attributes</i> in action. These two attributes (<code>DesignerGenerated</code> and <code>DebuggerNonUserCode</code>) are somewhat like the <code>Obsolete</code> attribute discussed earlier, in that they provide some informational identity to the related code. <code>DesignerGenerated</code> modifies the entire section of <code>Form1</code>&#8216;s code, while <code>DebuggerNonUserCode</code> only modifies the <i>Dispose</i> member. For clarity, both attributes include their full namespace paths. The <code>Global</code> keyword at the beginning of the <code>DesignerGenerated</code> attribute is actually a Visual Basic keyword that says, &#8220;Start at the very tippy-top of the namespace hierarchy; this is not a relative path.&#8221;
<pre><b> 2</b> Partial Public Class Form1</pre>
<p>Did you see the word <code>Partial</code> right there on line 2? I know I did. Hey, wait a minute; &#8220;<code>Public Class Form1</code>&#8221; also appeared in the <i>Form1.vb</i> file, but without the <code>Partial</code> keyword. Visual Basic 2005 includes a new feature that lets you divide a single class (<code>Form1</code> in this case) among multiple source code files by including the <code>Partial</code> keyword with at least one of the parts. Pretty cool, eh? It allows Visual Studio to add complex initialization code for your form (as found in this <i>Form1.Designer.vb</i> file) without it bothering your main source code file (<i>Form1.vb</i>).
<pre><b> 3</b> Inherits System.Windows.Forms.Form</pre>
<p>The <code>Inherits</code> keyword defines the inheritance relationship between this new <code>Form1</code> class and the previously written <code>System.Windows.Forms.Form</code> class. <code>Form</code> is the &#8220;base&#8221; class, while <code>Form1</code> is the &#8220;derived&#8221; class; <code>Form1</code> inherits all of the functionality of the <code>Form</code> class, including its initial look and feel. I&#8217;ll discuss these class relationships in more detail in Chapter 8.
<pre><b>44</b> Friend WithEvents Button1 As System.Windows.Forms.Button</pre>
<p>Line 44 defines the <b>Go Ahead, Click Me</b> button that appears in the center of the form. All controls that appear on your form are separate instances of classes. (<code>Friend</code> is a declaration statement described in the next chapter.) The <code>WithEvents</code> keyword indicates that this instance of the <code>Button</code> class will respond to events, such as a user clicking on it with the mouse. This line doesn&#8217;t actually create an instance of the <code>Button</code> class; that happens back on line 22.
<pre><b>22</b> Me.Button1 = New System.Windows.Forms.Button</pre>
<p>The <code>New</code> keyword creates new instances of classes. In this case, that new instance is assigned to the <code>Button1</code> class member defined on line 44. At this moment, <code>Button1</code> is a default instance of the <code>Button</code> class; it doesn&#8217;t have any of its custom settings, such as its size and position, or the <b>Go Ahead, Click Me</b> display text. All of that is set in lines 27 to 31.
<pre><b>27</b> Me.Button1.Location = New System.Drawing.Point(64, 104)
<b>28</b> Me.Button1.Name = "Button1"
<b>29</b> Me.Button1.Size = New System.Drawing.Size(152, 23)
<b>30</b> Me.Button1.TabIndex = 0
<b>31</b> Me.Button1.Text = "Go Ahead, Click Me!"</pre>
<p>Finally, the button is &#8220;glued&#8221; onto the form on line 38.
<pre><b>38</b> Me.Controls.Add(Me.Button1)</pre>
<p>This adds the <code>Button1</code> instance to the list of <code>Controls</code> managed by <code>Form1</code>. The <code>Me</code> keyword used throughout this code refers to the <code>Form1</code> class itself, so <code>Me.Button1</code> refers to the <code>Button1</code> class member specifically in the current <code>Form1</code> class.</p>
<p>Most of the code in this file appears in the <code>InitializeComponent</code> member procedure.
<pre><b>21</b> Private Sub InitializeComponent()
    ...
<b>43</b> End Sub</pre>
<p>When Visual Basic creates an instance of <code>Form1</code> to display on the screen, it calls the <code>InitializeComponent</code> procedure to do the work of adding the controls to the form. Actually, Visual Basic calls the form&#8217;s <b>constructor</b>, which in turn calls <code>InitializeComponent</code>. Constructors are special class members that perform any needed initialization on a class instance. They are called automatically by .NET each time a class instance is created. In Visual Basic, all constructors use the name <b><code>New</code></b>, as with the following code:
<pre>Friend Class ClassWithConstructor
  Public Sub New()
   ' ----- All initialization code goes here.

  End Sub
End Class</pre>
<p>I&#8217;ll talk much more about constructors in Chapter 8, but for now, locate the constructor in the code for <code>Form1</code>. (Very long pause.) What? There is no constructor? So, if there isn&#8217;t a constructor, how is the <code>InitializeComponent</code> member ever called?</p>
<p>That&#8217;s what I&#8217;d like to know. Actually, when the Visual Basic compiler generates the IL code for <code>Form1</code>, it adds a constructor silently, a constructor that calls <code>InitializeComponent</code>. How about that! Why didn&#8217;t Microsoft simply include the constructor&#8217;s code right in the source code? It&#8217;s a simplicity-for-the-programmer thing. They needed to have a default constructor that would call <code>InitializeComponent</code>, but they didn&#8217;t want a conflict to arise if you added your own default constructor in the non-Designer file. So they hid all of the code until it came time to actually compile the form. Clearly, it&#8217;s all rather hush-hush, so let&#8217;s move on.</p>
<p>Well, that&#8217;s pretty much the entire code, at least the part that matters to us now. Although we will rarely, if ever, examine the Visual Studio-generated code for the forms in the Library project, it&#8217;s good to see what&#8217;s going on behind the scenes. If you were a Visual Basic 6 programmer, you probably looked at the source code for your forms through Notepad at one time or another. If you did, you noticed that the form and all of its controls were defined with a hierarchy of special commands, and not with actual Visual Basic code. In .NET, that&#8217;s all changed; the form and all of its controls are created with ordinary Visual Basic code, so you can access it all and see what is really going on.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/spampw.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/spampw.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spampw.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spampw.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spampw.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spampw.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spampw.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spampw.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spampw.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spampw.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spampw.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spampw.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spampw.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spampw.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spampw.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spampw.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=17&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spampw.wordpress.com/2008/08/11/start-vb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/961e029cd5e3ce38ca8466946620f9e4?s=96&#38;d=identicon" medium="image">
			<media:title type="html">spampw</media:title>
		</media:content>

		<media:content url="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig09.jpg" medium="image" />

		<media:content url="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig10.jpg" medium="image" />

		<media:content url="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig11.jpg" medium="image" />

		<media:content url="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig12.jpg" medium="image" />

		<media:content url="http://69.10.233.10/KB/books/StarttoFinishVB2005/01fig13.jpg" medium="image" />
	</item>
		<item>
		<title>short-hand condition</title>
		<link>http://spampw.wordpress.com/2008/08/07/short-hand-condition/</link>
		<comments>http://spampw.wordpress.com/2008/08/07/short-hand-condition/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 21:02:46 +0000</pubDate>
		<dc:creator>spampw</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://spampw.wordpress.com/2008/08/07/short-hand-condition/</guid>
		<description><![CDATA[(condition)?(true part):(false part); Example &#60;?php ($x=$y)?$x=5:$x=0; //Equavalent if($x=$y) &#160;&#160;&#160;&#160; $x=5; else &#160;&#160;&#160;&#160; $x=0; ?&#62;<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=15&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><em>(condition)?(true part):(false part);</em></strong></p>
<p><strong>Example</strong></p>
<blockquote><p>&lt;?php <br />($x=$y)?$x=5:$x=0; <br />//Equavalent <br />if($x=$y) <br />&nbsp;&nbsp;&nbsp;&nbsp; $x=5; <br />else <br />&nbsp;&nbsp;&nbsp;&nbsp; $x=0; <br />?&gt;</p>
</blockquote>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/spampw.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/spampw.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spampw.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spampw.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spampw.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spampw.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spampw.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spampw.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spampw.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spampw.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spampw.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spampw.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spampw.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spampw.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spampw.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spampw.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=15&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spampw.wordpress.com/2008/08/07/short-hand-condition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/961e029cd5e3ce38ca8466946620f9e4?s=96&#38;d=identicon" medium="image">
			<media:title type="html">spampw</media:title>
		</media:content>
	</item>
		<item>
		<title>ตรวจสอบ IP &amp; Hostname ด้วย PHP</title>
		<link>http://spampw.wordpress.com/2008/08/07/%e0%b8%95%e0%b8%a3%e0%b8%a7%e0%b8%88%e0%b8%aa%e0%b8%ad%e0%b8%9a-ip-hostname-%e0%b8%94%e0%b9%89%e0%b8%a7%e0%b8%a2-php/</link>
		<comments>http://spampw.wordpress.com/2008/08/07/%e0%b8%95%e0%b8%a3%e0%b8%a7%e0%b8%88%e0%b8%aa%e0%b8%ad%e0%b8%9a-ip-hostname-%e0%b8%94%e0%b9%89%e0%b8%a7%e0%b8%a2-php/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 20:51:34 +0000</pubDate>
		<dc:creator>spampw</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://spampw.wordpress.com/2008/08/07/%e0%b8%95%e0%b8%a3%e0%b8%a7%e0%b8%88%e0%b8%aa%e0%b8%ad%e0%b8%9a-ip-hostname-%e0%b8%94%e0%b9%89%e0%b8%a7%e0%b8%a2-php/</guid>
		<description><![CDATA[ในบางครั้งเราต้องการตรวจสอบว่าผู้ที่เข้ามาเยี่ยมชมเว็บไซต์ของเราหรือผู้ที่มาโพสข้อความในเวบบอร์ดของเรานั้นมาจากที่ใด ซึ่งวิธีการที่ง่ายและเป็นที่นิยมใช้กันก็คือการตรวจสอบ IP Address ของเครื่องผู้ชมที่เปิดดูเว็บไซต์ของเรานั่นเอง โดยหลักการในการตรวจสอบ IP Address นี้ทำได้โดยการตรวจจาก Predefined Variable ของเว็บเซิร์ฟเวอร์ที่เราใช้งานอยู่ ซึ่ง Predefined Variable นี้จะเป็นตัวแปรที่สร้างขึ้นมาโดยอัติโนมัติทุกครั้งที่มีการรันสคริปของ PHP Predefine Variable ที่เก็บค่า IP Address ของผู้ใช้ไว้ก็คือ REMOTE_ADDR ดังนั้นเมื่อเราต้องการตรวจสอบค่า IP Address ของผู้ใข้ เราจะใช้ฟังก์ชัน getenv ในการดึงเอาค่าของ Predefined Variable นี้มาเก็บไว้ในตัวแปร $ip ดังนี้ Code: &#60;? &#160;&#160;&#160; $ip=getenv(REMOTE_ADDR); &#160;&#160;&#160; print&#160; &#8220;Your IP Address is $ip&#8221;; ?&#62;&#160; ผลลัพธ์ที่ได้คือ Code: Your IP Address is 127.0.0.1 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=14&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ในบางครั้งเราต้องการตรวจสอบว่าผู้ที่เข้ามาเยี่ยมชมเว็บไซต์ของเราหรือผู้ที่มาโพสข้อ<br />ความในเวบบอร์ดของเรานั้นมาจากที่ใด ซึ่งวิธีการที่ง่ายและเป็นที่นิยมใช้กันก็คือ<br />การตรวจสอบ IP Address ของเครื่องผู้ชมที่เปิดดูเว็บไซต์ของเรานั่นเอง โดยหลักการในการตรวจสอบ IP Address นี้ทำได้โดยการตรวจจาก Predefined Variable ของเว็บเซิร์ฟเวอร์ที่เราใช้งานอยู่ ซึ่ง Predefined Variable นี้จะเป็นตัวแปรที่สร้างขึ้นมาโดยอัติโนมัติทุกครั้งที่มีการรันสคริปของ PHP Predefine Variable ที่เก็บค่า IP Address ของผู้ใช้ไว้ก็คือ REMOTE_ADDR ดังนั้นเมื่อเราต้องการตรวจสอบค่า IP Address ของผู้ใข้ เราจะใช้ฟังก์ชัน getenv ในการดึงเอาค่าของ Predefined Variable นี้มาเก็บไว้ในตัวแปร $ip ดังนี้</p>
<p><span id="more-14"></span>
<p>Code:
<p>&lt;? <br />&nbsp;&nbsp;&nbsp; $ip=getenv(REMOTE_ADDR); <br />&nbsp;&nbsp;&nbsp; print&nbsp; &#8220;Your IP Address is $ip&#8221;; <br />?&gt;&nbsp;
<p>ผลลัพธ์ที่ได้คือ
<p>Code:
<p>Your IP Address is 127.0.0.1
<p>จะเห็นได้ว่าโปรแกรมนี้สามารถตรวจสอบ IP Address ของผู้ใช้ได้ในระดับหนึ่ง แต่จะมีปัญหาในกรณีที่เครื่องผู้ใช้ได้ทำการปรับแต่งเครื่องให้ใช้ Proxy Server ซึ่งแทนที่เราจะได้ IP Address ของเครื่องผู้ใช้ เราก็จะได้รับ IP Address ของ Proxy Server แทน หรือในบางกรณีเราอาจจะได้ IP Address ของ Cache Server ของ ISP ที่ผู้ใช้ได้ใช้บริการอยู่ ดังนั้น ในกรณีที่เกิดเหตุการณ์นี้ตัวแปร REMOTE_ADDR จะไม่ได้เก็บ IP Address ที่แท้จริงของเครื่องผู้ใช้แต่จะเก็บ IP Address ของ Proxy Server หรือ Cache Server และจะเกิดตัวแปร HTTP_X_FORWARDED_FOR ขึ้นมา ซึ่งจะเป็นตัวแปรที่เก็บ IP Address ที่แท้จริงของเครื่องผู้ใช้แทน ดังนั้นเราจะทำการแก้โปรแกรมข้างต้นของเราเพื่อให้เก็บ IP Address ได้อย่างถูกต้อง ดังนี้
<p>Code:
<p>&lt;?php <br />&nbsp;&nbsp;&nbsp; if (getenv(HTTP_X_FORWARDED_FOR)) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $ip=getenv(HTTP_X_FORWARDED_FOR); <br />&nbsp;&nbsp;&nbsp; else <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $ip=getenv(REMOTE_ADDR); <br />&nbsp;&nbsp;&nbsp; print &#8220;Your IP Address is $ip&#8221;; <br />?&gt;
<p>ในโปรแกรมของเรานั้นเริ่มต้น เราจะทำการเช็คดูก่อนว่าตัวแปร HTTP_X_FORWARDED_FOR นั้นมีการเซ็ตไว้หรือเปล่า ถ้ามีการเซ็ตไว้แสดงว่ามีการใช้ Proxy Server หรือ Cache Server ดังนั้นตัวแปร HTTP_X_FORWARDED_FOR ก็จะเก็บ IP Address ที่แท้จริงไว้ ดังนั้นเราก็จะใช้ค่าจากตัวแปรนี้ แต่ถ้าไม่มีการเซ็ตตัวแปรนี้ไว้ เราก็จะเก็บค่า IP Address จากตัวแปร REMOTE_ADDR ตามปกติ<br />สำหรับชื่อของ Predefined Variable ที่เราใช้ในที่นี้คือ REMOTE_ADDR และ HTTP_X_FORWARDED_FOR อาจจะมีชื่อที่แตกต่างกันไปได้ ขึ้นอยู่กับว่าเราใช้เว็บเซิร์ฟเวอร์อะไร โดยในที่นี้เราจะอ้างอิงจาก Apache Webserver เป็นหลัก โดยถ้าเราใช้ [/b]Web Server อื่นนอกจาก Apache และต้องการตรวจสอบว่ามี Predefined Variable อะไรบ้างก็สามารถตรวจสอบได้ โดยใช้ฟังก์ชัน phpinfo() เพื่อตรวจสอบดังนี้
<p>Code:
<p>&lt;? <br />&nbsp;&nbsp;&nbsp; phpinfo(); <br />?&gt;&nbsp;
<p>โดยฟังก์ชันนี้จะแสดงตัวแปรที่เป็น Predefined Variable ทั้งหมดออกมา</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/spampw.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/spampw.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spampw.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spampw.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spampw.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spampw.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spampw.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spampw.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spampw.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spampw.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spampw.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spampw.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spampw.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spampw.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spampw.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spampw.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=14&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spampw.wordpress.com/2008/08/07/%e0%b8%95%e0%b8%a3%e0%b8%a7%e0%b8%88%e0%b8%aa%e0%b8%ad%e0%b8%9a-ip-hostname-%e0%b8%94%e0%b9%89%e0%b8%a7%e0%b8%a2-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/961e029cd5e3ce38ca8466946620f9e4?s=96&#38;d=identicon" medium="image">
			<media:title type="html">spampw</media:title>
		</media:content>
	</item>
		<item>
		<title>What the &quot;@&quot; mean in php</title>
		<link>http://spampw.wordpress.com/2008/08/07/what-the-mean-in-php/</link>
		<comments>http://spampw.wordpress.com/2008/08/07/what-the-mean-in-php/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 15:44:41 +0000</pubDate>
		<dc:creator>spampw</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://spampw.wordpress.com/2008/08/07/what-the-mean-in-php/</guid>
		<description><![CDATA[The @ surpresses warning and error messages. For example, if you run the command is_file(&#8220;myfile.txt&#8221;) nad myfile.txt does not exist, you get a warning message. If you run @is_file(&#8220;myfile.txt&#8221;) you wont get this warning message. คือทำให้ไม่แสดง Warning และ Error ถ้ามันเกิดขึ้น<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=12&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The @ surpresses warning and error messages.</p>
<p>For example, if you run the command is_file(&#8220;myfile.txt&#8221;) nad myfile.txt does not exist, you get a warning message. If you run @is_file(&#8220;myfile.txt&#8221;) you wont get this warning message.</p>
<p>คือทำให้ไม่แสดง Warning และ Error ถ้ามันเกิดขึ้น</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/spampw.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/spampw.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spampw.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spampw.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spampw.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spampw.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spampw.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spampw.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spampw.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spampw.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spampw.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spampw.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spampw.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spampw.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spampw.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spampw.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=12&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spampw.wordpress.com/2008/08/07/what-the-mean-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/961e029cd5e3ce38ca8466946620f9e4?s=96&#38;d=identicon" medium="image">
			<media:title type="html">spampw</media:title>
		</media:content>
	</item>
		<item>
		<title>Margins and Padding</title>
		<link>http://spampw.wordpress.com/2008/08/03/margins-and-padding/</link>
		<comments>http://spampw.wordpress.com/2008/08/03/margins-and-padding/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 10:13:58 +0000</pubDate>
		<dc:creator>spampw</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://spampw.wordpress.com/2008/08/03/margins-and-padding/</guid>
		<description><![CDATA[margin and padding are the two most commonly used properties for spacing-out elements. A margin is the space outside of the element, whereas padding is the space inside the element. Change the CSS code for h2 to the following: h2 { font-size: 1.5em; background-color: #ccc; margin: 1em; padding: 3em; } You will see that this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=9&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://htmldog.com/reference/cssproperties/margin/"><code>margin</code></a></strong> and <strong><a href="http://htmldog.com/reference/cssproperties/padding/"><code>padding</code></a></strong> are the two most commonly used properties for spacing-out elements. A margin is the space <strong>outside</strong> of the element, whereas padding is the space <strong>inside</strong> the element.</p>
<p>Change the CSS code for <code>h2</code> to the following:</p>
<p><code>h2 {     <br />font-size: 1.5em;      <br /> <strong>background-color: #ccc;</strong>      <br /> <strong>margin: 1em;</strong>      <br /> <strong>padding: 3em;</strong>      <br />}</code></p>
<p> <span id="more-9"></span>
<p>You will see that this leaves one-character width space around the secondary header and the header itself is fat from the three-character width padding.</p>
<p>The four sides of an element can also be set individually. <strong><a href="http://htmldog.com/reference/cssproperties/margin/"><code>margin-top</code></a></strong>, <strong><a href="http://htmldog.com/reference/cssproperties/margin/"><code>margin-right</code></a></strong>, <strong><a href="http://htmldog.com/reference/cssproperties/margin/"><code>margin-bottom</code></a></strong>, <strong><a href="http://htmldog.com/reference/cssproperties/margin/"><code>margin-left</code></a></strong>, <strong><a href="http://htmldog.com/reference/cssproperties/padding/"><code>padding-top</code></a></strong>, <strong><a href="http://htmldog.com/reference/cssproperties/padding/"><code>padding-right</code></a></strong>, <strong><a href="http://htmldog.com/reference/cssproperties/padding/"><code>padding-bottom</code></a></strong> and <strong><a href="http://htmldog.com/reference/cssproperties/padding/"><code>padding-left</code></a></strong> are the self-explanatory properties you can use.</p>
<h4>The Box Model</h4>
<p>Margins, padding and borders (see <a href="http://htmldog.com/guides/cssbeginner/borders/">next page</a>) are all part of what&#8217;s known as the <strong>Box Model</strong>. The Box Model works like this: in the middle you have the content area (let&#8217;s say an image), surrounding that you have the padding, surrounding that you have the border and surrounding that you have the margin. It can be visually represented like this:</p>
</p>
<p><a href="http://spampw.files.wordpress.com/2008/08/image.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="231" alt="image" src="http://spampw.files.wordpress.com/2008/08/image-thumb.png?w=236&#038;h=231" width="236" border="0" /></a> </p>
<p>You don&#8217;t have to use all of these, but it can be helpful to remember that the box model can be applied to every element on the page, and that&#8217;s a powerful thing!</p>
<p>from: <a title="http://htmldog.com/" href="http://htmldog.com/">http://htmldog.com/</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/spampw.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/spampw.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spampw.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spampw.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spampw.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spampw.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spampw.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spampw.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spampw.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spampw.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spampw.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spampw.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spampw.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spampw.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spampw.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spampw.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=9&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spampw.wordpress.com/2008/08/03/margins-and-padding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/961e029cd5e3ce38ca8466946620f9e4?s=96&#38;d=identicon" medium="image">
			<media:title type="html">spampw</media:title>
		</media:content>

		<media:content url="http://spampw.files.wordpress.com/2008/08/image-thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>PHP Form Handling</title>
		<link>http://spampw.wordpress.com/2008/08/01/php-form-handling/</link>
		<comments>http://spampw.wordpress.com/2008/08/01/php-form-handling/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 12:25:19 +0000</pubDate>
		<dc:creator>spampw</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://spampw.wordpress.com/2008/08/01/php-form-handling/</guid>
		<description><![CDATA[If your PHP program is a dynamic web page (and it probably is) and your PHP program is dealing with user input (and it probably is), then you need to work with HTML forms. Here are some tips for simplifying, securing, and organizing your form-handling PHP code. 1. Use $_SERVER['PHP_SELF'] as a form action. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=4&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If your PHP program is a dynamic web page (and it probably is) and your PHP program is dealing with user input (and it probably is), then you need to work with HTML forms. Here are some tips for simplifying, securing, and organizing your form-handling PHP code.</p>
<p> <span id="more-4"></span>
</p>
<h5>1. Use <code>$_SERVER['PHP_SELF']</code> as a form action.</h5>
<p>The <code>$_SERVER</code> auto-global array holds various useful server- and request-specific info. The <code>PHP_SELF</code> element of <code>$_SERVER</code> holds the filename of the currently executing script (relative to your web site&#8217;s document root directory). So, supplying <code>$_SERVER['PHP_SELF']</code> as the <code>action</code> attribute of the form tag makes the form submit to the same page that displayed it. This lets you put the logic to handle the form in the same page as the logic that displays it. For many simple forms, this keeps things easy to manage.</p>
<h5>2. Put <code>[]</code> at the end of the name of a multivalued form parameter.</h5>
<p>When you&#8217;ve got a form element like <code>&lt;select multiple&gt;</code> that can submit multiple values to the server, put <code>[]</code> at the end of the form element name so that the PHP interpreter knows to accept multiple values.</p>
<p>For example, consider this form:</p>
<pre><code>&lt;form method=&quot;POST&quot;  action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot;&gt;
Pick some desserts: &lt;select name=&quot;sweet[]&quot; multiple&gt;
&lt;option value=&quot;puff&quot;&gt; Sesame Seed Puff&lt;/option&gt;
&lt;option value=&quot;square&quot;&gt; Coconut Milk Gelatin Square&lt;/option&gt;
&lt;option value=&quot;cake&quot;&gt; Brown Sugar Cake&lt;/option&gt;
&lt;option value=&quot;ricemeat&quot;&gt; Sweet Rice and Meat&lt;/option&gt;
&lt;/select&gt;
&lt;input type=&quot;submit&quot; name=&quot;Order&quot;&gt;
&lt;/form&gt;</code></pre>
<p>If you pick <code>Sesame Seed Puff</code> and <code>Brown Sugar Cake</code> and submit the form, then <code>$_POST['sweet']</code> is itself an array. <code>$_POST['sweet'][0]</code> is <code>puff</code> and <code>$_POST['sweet'][1]</code> is <code>cake</code>. That&#8217;s because the <code>name</code> attribute of the <code>&lt;select&gt;</code> element is <code>sweet[]</code>. If the name was just <code>sweet</code>, then <code>$_POST['sweet']</code> would be a string, holding only one of the selected values.</p>
<h5>3. Test for form submission with a hidden element.</h5>
<p>Include a hidden variable named, say, <code>_submit_check</code> in your forms like this:</p>
<pre><code>&lt;input type=&quot;hidden&quot; name=&quot;_submit_check&quot; value=&quot;1&quot;/&gt;
</code></pre>
<p>Then, to test whether the form has been submitted, look for the <code>_submit_check</code> element in <code>$_POST</code>:</p>
<pre><code>if (array_key_exists('_submit_check', $_POST)) {
     /* ... do something with the form parameters ... */
}
</code></pre>
<p>Testing for the presence of a hidden element avoids problems that can result from browsers&#8217; varying behaviors when a user submits a form by pressing the Enter key instead of clicking a submit button.</p>
<h5>4. Divide your form handling into three parts: showing, validating, and processing.</h5>
<p>Logically, the life cycle of a web form usually comprises three steps: showing the form, validating the submitted form parameters, and then processing the submitted form parameters to generate appropriate output.</p>
<p>Dedicate a function to each of these steps: showing, validating, and processing. With this modular design, deciding when each step needs to happen is straightforward.</p>
<p>On many pages, the logical flow goes like this:</p>
<ul>
<li>If the request isn&#8217;t a form submission, show the form. </li>
<li>If the request is a form submission, validate the submitted parameters. </li>
<li>If the submitted parameters are valid, process them. </li>
<li>If the submitted parameters are invalid, show the form. </li>
</ul>
<p>With a function-based structure, the code to accomplish this looks something like:</p>
<pre><code>if (array_key_exists('_submit_check',$_POST)) {
     // If validate_form() returns errors, pass them to show_form()
     if ($form_errors = validate_form()) {
         show_form($form_errors);
     } else {
         // The submitted data is valid, so process it
         process_form();
     }
} else {
     // The form wasn't submitted, so display
     show_form();
}</code></pre>
<p>The page either displays the form (possibly with error messages) or displays the results of processing the form.</p>
<p>On other pages, particularly search pages, the logical flow goes like this instead:</p>
<ul>
<li>If the request is a form submission, validate the submitted parameters. </li>
<li>Display the form. </li>
<li>If the request is a form submission, process the submitted parameters. </li>
</ul>
<p>With a function-based structure, the code to accomplish this looks something like:</p>
<pre><code>// Check for errors if the form was submitted
$form_errors = array_key_exists('_submit_check',$_POST) ?
                validate_form() : null;

// Always display the form
show_form($form_errors);

// Display results if the form was submitted
if (array_key_exists('_submit_check', $_POST)) {
     process_form();
}
</code></pre>
<p>Displaying the form above the processing output is useful for pages where users might want to adjust the form parameters based on the results. For example, if a product search for toasters that cost between $150 and $300 reveals only two choices, a user can adjust the price range and resubmit the form for a new search without going to a separate page.</p>
<h5>5. Validate numbers with <code>strval()</code> and <code>intval()</code> or <code>floatval()</code>.</h5>
<p>Usually, the ability to switch a variable smoothly between holding a string or a number is a great convenience in your PHP programs. However, that makes form validation a little harder. To check whether a submitted form parameter is a valid integer, use <code>strval()</code> and <code>intval()</code> together like this:</p>
<pre><code>if ($_POST['age'] != strval(intval($_POST['age'])) {
     $errors[] = 'Please enter a valid age.';
}</code></pre>
<p>If <code>$_POST['age']</code> isn&#8217;t an integer, then <code>intval()</code> changes its value to something else. Adding <code>strval()</code> to the mix ensures that the comparison using the <code>!=</code> operator doesn&#8217;t do any silent, behind-the-scenes conversion.</p>
<p>Similarly, to check whether a submitted form parameter is a valid floating-point number, use <code>floatval()</code> instead of <code>intval()</code>:</p>
<pre><code>if ($_POST['price'] != strval(floatval($_POST['price']))) {
     $errors[] = 'Please enter a valid price.';
}</code></pre>
<h5>6. Entity-escape form data before printing it.</h5>
<p>Printing data that comes from an external source (like form input) without properly encoding it leaves you vulnerable to the common, devastating, and embarrassing &quot;cross-site scripting attack.&quot;</p>
<p>Pass external data through <code>htmlentities()</code> before printing it, like this:</p>
<pre><code>print &quot;Your monkey's name is: &quot; .
htmlentities($_POST['monkey_name']); </code></pre>
<p>Read more about cross-site scripting at <a href="http://www.owasp.org/documentation/topten/a4.html">http://www.owasp.org/documentation/topten/a4.html</a>.</p>
<h5>7. Print form elements&#8217; defaults with helper functions.</h5>
<p>Printing out appropriate HTML for individual form elements is boring and repetitive. Fortunately, computers are quite good at boring and repetitive tasks. Encapsulate logic for printing HTML form elements in functions. Then, call those functions whenever you need to print a form element. Chapter 6 of <i>Learning PHP 5</i> includes functions for a number of form elements. Here are a few samples: </p>
<pre><code>// print a single-line text box
function input_text($element_name, $values) {
     print '&lt;input type=&quot;text&quot; name=&quot;' . $element_name .'&quot; value=&quot;';
     print htmlentities($values[$element_name]) . '&quot;&gt;';
}

//print a textarea
function input_textarea($element_name, $values) {
     print '&lt;textarea name=&quot;' . $element_name .'&quot;&gt;';
     print htmlentities($values[$element_name]) . '&lt;/textarea&gt;';
}

//print a radio button or checkbox
function input_radiocheck($type, $element_name,
                           $values, $element_value) {
     print '&lt;input type=&quot;' . $type . '&quot; name=&quot;' .
           $element_name .'&quot; value=&quot;' . $element_value . '&quot; ';
     if ($element_value == $values[$element_name]) {
         print ' checked=&quot;checked&quot;';
     }
     print '/&gt;';
}

//print a submit button
function input_submit($element_name, $label) {
     print '&lt;input type=&quot;submit&quot; name=&quot;' . $element_name .'&quot; value=&quot;';
     print htmlentities($label) .'&quot;/&gt;';
}</code></pre>
<p>These functions are called like this:</p>
<pre><code>print '&lt;form method=&quot;POST&quot; action=&quot; . $_SERVER['PHP_SELF'] . '&quot;&gt;';
print 'Name: '; input_text('name', $_POST);
print '&lt;br/&gt;';
print 'Description: ';
input_textarea('description', $_POST);
print '&lt;br/&gt;';
print 'Advanced?';
input_radiocheck('check','editor', $_POST, 'yes');
print '&lt;br/&gt;';
print 'Size: Big ';
input_radiocheck('radio','size', $_POST, 'big');
print ' Small ';
input_radiocheck('radio','size', $_POST, 'small');
print '&lt;br/&gt;';
input_submit('submit', 'Save');</code></pre>
<p>The functions are easily extendable to add your own layout or support for arbitrary attributes for each element.</p>
<h5>8. Investigate HTML_QuickForm for advanced form processing.</h5>
<p>For more advanced form handling, check out the PEAR module <a href="http://pear.php.net/package/HTML_QuickForm">HTML_QuickForm</a>. It provides methods for the flexible and structured creation, validation, and display of HTML forms. HTML_QuickForm frees you from doing the grunt work of displaying defaults for form elements, encoding HTML entities, and duplicating validation code. Its built-in layout engine is customizable, and you can integrate with template engines like Smarty. </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/spampw.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/spampw.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/spampw.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/spampw.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/spampw.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/spampw.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/spampw.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/spampw.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/spampw.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/spampw.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/spampw.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/spampw.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/spampw.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/spampw.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/spampw.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/spampw.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=spampw.wordpress.com&amp;blog=4387568&amp;post=4&amp;subd=spampw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://spampw.wordpress.com/2008/08/01/php-form-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/961e029cd5e3ce38ca8466946620f9e4?s=96&#38;d=identicon" medium="image">
			<media:title type="html">spampw</media:title>
		</media:content>
	</item>
	</channel>
</rss>
