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

<channel>
	<title>This Life of Brian &#187; PHP</title>
	<atom:link href="http://life-of-brian.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://life-of-brian.com</link>
	<description>My name is Brian.  Welcome to my life.</description>
	<lastBuildDate>Tue, 03 Jan 2012 03:09:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>New Project: Education Tax Credits</title>
		<link>http://life-of-brian.com/2009/02/new-project-education-tax-credits/</link>
		<comments>http://life-of-brian.com/2009/02/new-project-education-tax-credits/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 03:26:02 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Nerds at Work]]></category>
		<category><![CDATA[Online Publishing]]></category>
		<category><![CDATA[graduate school]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[website projects]]></category>

		<guid isPermaLink="false">http://life-of-brian.com/?p=1491</guid>
		<description><![CDATA[So I took some time today and finished the first of my mini website projects: Education Tax Credits. I came up with the idea while reading through the IRS documentation about the Hope Credit, Lifetime Learning Credit, and Tuition and Fees Deduction. I just started graduate school, and next year I&#8217;ll be taking advantage of [...]]]></description>
			<content:encoded><![CDATA[<p>So I took some time today and finished the first of my mini website projects: <a href="http://education-tax-credits.net/">Education Tax Credits</a>.</p>
<p>I came up with the idea while reading through the IRS documentation about the Hope Credit, Lifetime Learning Credit, and Tuition and Fees Deduction.</p>
<p>I just started graduate school, and next year I&#8217;ll be taking advantage of one of these tax benefits. While reading the IRS page, I thought it&#8217;d be nice if there was a simplified version that explained the important details without getting hung up on all the legalese.</p>
<p>I don&#8217;t know if I accomplished that, but I learned a lot about the tax credits while working on it!</p>
<p>In addition to summarizing the IRS documentation, I created a <a href="http://education-tax-credits.net/tax-benefits-for-higher-education/higher-education-tax-benefit-calculator/">simple calculator</a> to estimate how much each tax credit/deduction would net you on your tax return. It works nice, and I like the styling of it. If I get around to it, though, I want to make it work via AJAX (currently it posts to a php file and then redirects back to the page).</p>
<p>I also plan on posting a series of quick &#8220;<a href="http://education-tax-credits.net/articles-about-taxes-savings-and-higher-education/">Question of the Days</a>&#8221; about the tax credits and deductions.</p>
<p>I don&#8217;t plan on spending a lot of time on the site &#8211; maybe an hour or two each week adding QotDs, and a few hours promoting/linking throughout the web &#8211; and I&#8217;m curious to see how well it&#8217;ll do in SERPs and, more importantly, PR. Cause, you know, PR can equal cash.</p>
]]></content:encoded>
			<wfw:commentRss>http://life-of-brian.com/2009/02/new-project-education-tax-credits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary Code and Bitwise Operators (in PHP)</title>
		<link>http://life-of-brian.com/2008/04/binary-code-bitwise-operators-php/</link>
		<comments>http://life-of-brian.com/2008/04/binary-code-bitwise-operators-php/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 21:20:12 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Nerds at Work]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://life-of-brian.com/?p=319</guid>
		<description><![CDATA[At it&#8217;s core, all of the information on your computer is made up of bits &#8211; or 0&#8242;s and 1&#8242;s. There&#8217;s quite a bit of interpretation that goes on between that basic binary code and the information as it is displayed on your screen. However, you may find a time to work with data at [...]]]></description>
			<content:encoded><![CDATA[<p>At it&#8217;s core, all of the information on your computer is made up of bits &#8211; or 0&#8242;s and 1&#8242;s.  There&#8217;s quite a bit of interpretation that goes on between that basic binary code and the information as it is displayed on your screen.</p>
<p>However, you may find a time to work with data at the binary level in PHP (and many other programming languages).  Here&#8217;s a quick guide to understanding binary numbers and the common operators for handling them.<br />
<span id="more-319"></span></p>
<h3>&#8220;Normal&#8221; Numbers &#8211; Base Ten, Decimal System</h3>
<p>When you deal with numbers, you are probably using the &#8220;Base Ten&#8221; or &#8220;Decimal&#8221; number system.  These are numbers made up of a set of ten different digits &#8211; zero through nine.</p>
<p>A given quantity &#8211; like 512 &#8211; is created with a series of digits.  The position of the digit tells us how much the digit actually represents.  The &#8217;2&#8242; here is in the units place, so it simply represents 2.  The 1 is in the tens place, so it actually represents 10.  The 5 is in the hundreds place, so it represents 500.</p>
<p>The number could be re-written as:</p>
<pre>500 + 10 + 2 = 512</pre>
<p>You&#8217;ll notice a pattern here.  Each new place is worth ten times the previous place.  The third digit (the hundreds place) is worth 10 * 10.  If we added a fourth place, it would be worth 10 * 100 (1000).</p>
<p>This gives us a nifty little mathematical formula &#8211; which is why this is called &#8220;base ten&#8221; numbering.  The nth digit (where zero is the right-most digit) is worth 10^n.  Here&#8217;s the number 1024 re-written to illustrate this.</p>
<pre>(1 * 10^3) + (0 * 10^2) + (2 * 10^1) + (4 * 10^0) = 1024</pre>
<p>This is a lot of work to read a number that we intuitively know is worth 1,024.  However, it helps to understand how binary numbers work.</p>
<h3>Binary Numbers &#8211; The Base Two System</h3>
<p>While base ten numbers are simple enough for humans to work with, computers are designed to use a binary or base two numbering system.</p>
<p>The simplest unit of information is a binary bit &#8211; a 0 or a 1.</p>
<p>To create complex numbers, we can string together 0&#8242;s and 1&#8242;s.  For example, a number could be written as 0010 0101.</p>
<p>To understand what this value means, we can apply the same rules as we did above.  However, since this is a base two system instead of a base ten system, each place is multiplied by 2^n.</p>
<p>Let&#8217;s start with a smaller example &#8211; 1111.</p>
<pre>(1 * 2^3) + (1 * 2^2) + (1 * 2^1) + (1 * 2^0)
    = 8 + 4 + 2 + 1
    = 15</pre>
<p>These rules can be expanded to represent byte-sized numbers (8-bits, or 2^8, or 255) or even larger numbers.</p>
<h3>Manipulating Bits and Bytes &#8211; Bitwise Operators</h3>
<p>Most programming languages come with some built in operators to handle bits.  These help make very basic comparisons and manipulations of numbers.</p>
<p>These typically include the &#8216;and&#8217; or intersection operator, the &#8216;or&#8217; or union operator, and the &#8216;xor&#8217; or exclusive-or operator.  We&#8217;ll just take a look at the &#8216;and&#8217; and &#8216;or&#8217; operators right now.  [Note:  I use the terms intersection and union because I think they make it much easier to understand, if you are at all familiar with math sets and statistics.  They aren&#8217;t typical &#8216;coding&#8217; terms).</p>
<p>The &#8216;and&#8217; or intersection operator &#8211; the &#8216;&amp;&#8217; character in PHP &#8211; compares two binary numbers and returns a new binary number with a &#8217;1&#8242; in every unit in which <strong>both</strong> numbers have a &#8217;1&#8242;.  If both numbers have a value in the second place, then the return number will have a value in the second place.  If only one number has a value in the third place, then the return number will have no value in the third place.</p>
<pre>0111 &amp; 0101 = 0101
0101 &amp; 1010 = 0000
1111 &amp; 1111 = 1111</pre>
<p>If you imagine &#8217;0111&#8242; and &#8217;0101&#8242; as sets of numbers ({4, 2, 1} and {4, 1} respectively), then the result of a bitwise &#8216;and&#8217; operation is the simple intersection of the two sets.</p>
<p>The bitwise &#8216;or&#8217; operator &#8211; a | in PHP &#8211; returns a value in each place that <strong>either</strong> number has a value.  So if only one number has a value in the first place, the result will have a value in the first place.  A place will only have no value if <strong>neither</strong> number had a value there.</p>
<pre>0111 | 0101 = 0111
0101 1110 | 1010 0001 = 1111 1111
0000 1111 | 1111 0000 = 1111 1111</pre>
<p>These operators aren&#8217;t used very much, but it is important to recognize them.  If you don&#8217;t know what they are, it is easy to confuse them with the standard <a href="http://www.earn-web-cash.com/2008/02/16/php-logical-operator/">logical operators</a> &#8211; || and &amp;&amp;.</p>
<p>For example, the following two lines of code have <strong>very</strong> different meanings.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">124</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">36</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bitwise</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$logical</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the first comparison ($x &amp; $y), we&#8217;re finding the bitwise intersection of the two values.  This would be&#8230;</p>
<pre>0111 1100 &amp; 0010 0100 = 0010 0100</pre>
<p>In base ten terms, $bitwise would be equal to 36.  In the second example ($x &amp;&amp; $y), we&#8217;re looking for a simple boolean comparison.  In this case, both $x and $y have a value, so the comparison will return &#8217;1&#8242; or &#8216;true&#8217;.</p>
<h3>Is This Useful&#8230;?  Comparing Constant Flags</h3>
<p>You may be thinking to yourself, &#8220;When will I ever use this?&#8221;</p>
<p>While anyone working with computer code should understand how binary numbers work, there will probably be very few cases in which you manipulate numerical values in a binary format.  The rest of the world works with decimal numbers &#8211; and your programming language is designed to do the same thing.</p>
<p>However, there are some cases where you <strong>will</strong> want to use bitwise numbers and comparisons.  A perfect example is the use of constants and flags.</p>
<p>Let&#8217;s say that a function takes 16 parameters.  Each of these parameters is a simple &#8220;True&#8221; or &#8220;False.&#8221;</p>
<p>You could write the function take sixteen different parameters and use sixteen different variables.  However, this would make function calls quite long &#8211; especially if you usually only make two or three parameters true.</p>
<p>Instead, you can pass one value as a parameter.  This value &#8211; 16 bits long &#8211; includes 16 individual flags to tell the function whether a specific parameter is true or false.</p>
<p>If every parameter is set to true, this value would be &#8220;1111 1111 1111 1111&#8243;.  If only the first parameter should be set to true, this value would be &#8220;0000 0000 0000 0001&#8243;.</p>
<p>Each flag is represented by a specific place in this binary number.  The first flag is &#8220;0001,&#8221; the second flag is &#8220;0010,&#8221; the third flag is &#8220;0100,&#8221; etc.</p>
<p>Your code wouldn&#8217;t be very readable if you referred to each flag by its numerical value.  Instead, you can define constant values.  For example, FUNCTION_FLAG_ONE would represent &#8220;0001,&#8221; &#8220;FUNCTION_FLAG_TWO would represent &#8220;0010,&#8221; etc.  These constants would follow whatever naming convention you usually use.</p>
<p>The value of this is that you can easily set any number of flags with the use of the bitwise &#8216;or&#8217; operator.</p>
<p>For example, let&#8217;s say you want to set flags one, seven, and fifteen to true.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$flags</span> <span style="color: #339933;">=</span> FUNCTION_FLAG_ONE <span style="color: #339933;">|</span>
    FUNCTION_FLAG_SEVEN <span style="color: #339933;">|</span> FUNCTION_FLAG_FIFTEEN<span style="color: #339933;">;</span></pre></div></div>

<p>The result would be &#8217;0100 0000 0100 0001.&#8217;</p>
<p>Inside the function, you could then use the bitwise &#8216;and&#8217; operator to see which flags were set.  For example, this would let you check to see if the fifteenth flag was set to true.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fifteen</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$flags</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span> FUNCTION_FLAG_FIFTEEN<span style="color: #339933;">;</span></pre></div></div>

<p>Although your function&#8217;s code will have to do some legwork to find out which flags were set and which ones were not, this makes the function call itself <strong>much</strong> more efficient and readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://life-of-brian.com/2008/04/binary-code-bitwise-operators-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Write a PHP Script to Send a Trackback</title>
		<link>http://life-of-brian.com/2008/01/how-to-write-a-php-script-to-send-a-trackback/</link>
		<comments>http://life-of-brian.com/2008/01/how-to-write-a-php-script-to-send-a-trackback/#comments</comments>
		<pubDate>Wed, 09 Jan 2008 03:11:49 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Nerds at Work]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://life-of-brian.com/?p=545</guid>
		<description><![CDATA[Don&#8217;t know what a trackback is? Check out this set of articles for a bit more information. Assuming you do know what a trackback is, how do you send one &#8211; technically speaking? And how can we write a php script to send one? A trackback is a simple ping &#8211; an HTTP Request. It [...]]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t know what a trackback is?  Check out this <a href="http://www.earn-web-cash.com/web-design-guides/trackbacks/">set of articles</a> for a bit more information.</p>
<p>Assuming you do know what a trackback is, how do you send one &#8211; technically speaking?  And how can we write a php script to send one?</p>
<p>A trackback is a simple ping &#8211; an HTTP Request.  It is specially formatted with a limited amount of information.  The receiving server is set up to handle that information and use it to create the comment based on your trackback ping.<br />
<span id="more-545"></span></p>
<h3>The Trackback API As Per Moveable Type</h3>
<p>Since Moveable Type introduced the trackback, they were the ones responsible for writing the standards and API.  You can read through the <a href="http://www.sixapart.com/pronet/docs/trackback_spec">trackback technical specs on the SixApart website.</a></p>
<p>In order to send a trackback ping with a PHP script, there&#8217;s a few things we need to know&#8230;</p>
<ul>
<li>The ping is in the form of an HTTP Post Request.</li>
<li>The ping is sent to the trackback URI of the receiving post.</li>
<li>The request must have a content-type header defining it as <code>application/x-www-url-encoded</code></li>
<li>All of the parameters included in the content should be url encoded.</li>
<li>The standard parameters are &#8220;title,&#8221; &#8220;excerpt,&#8221; &#8220;url,&#8221; and &#8220;blog.&#8221;  Only the &#8220;url&#8221; parameter is required.</li>
<li>The server will send a short XML response &#8211; containing <code>&lt;error&gt;0&lt;/error&gt;</code> if there was no error, and <code>&lt;error&gt;1&lt;/error&gt;</code> along with a <code>&lt;message&gt;&lt;/message&gt;</code> element if there was an error.</li>
</ul>
<p>With that in mind, we can begin to write our script.  We need to do four things.</p>
<ul>
<li>Receive input from the user (the blog url, title, excerpt, etc).</li>
<li>Create the header and format the information correctly</li>
<li>Send the header and get a response</li>
<li>Read the XML to see if it was successful or not</li>
</ul>
<h3>Getting Input From the User</h3>
<p>Getting input is pretty simple.  We&#8217;ll create a form in HTML, set the action to the page containing the script, and set the method to post.</p>
<p>You&#8217;ll want a text input for the URL, title, and blog.  You could use either a text input or a small textarea box to take the excerpt.</p>
<p>Here&#8217;s some sample HTML to create the form.  You&#8217;d want to add some css to make it look pretty, but you can do that some other time.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;trackback-form&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;trackback.html&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">fieldset</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;trackback&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;trackback-uri&quot;</span>&gt;</span>Trackback URI:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;trackback-uri&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;trackback-uri&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span>&gt;</span>Post Title:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;url&quot;</span>&gt;</span>Post URL:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;excerpt&quot;</span>&gt;</span>Excerpt:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;excerpt&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;excerpt&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blog&quot;</span>&gt;</span>Blog Name:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blog&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blog&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Send Trackback Ping&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">fieldset</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>Simple&#8230; but effective.  Set the &#8220;action&#8221; to the name of the file with the form in it.  We&#8217;ll add the php script to the top, and the form will just return the information to itself.</p>
<h3>Reading the Information from the Form and Encoding It</h3>
<p>Once you hit submit, all of the form information will get sent to the file so that the script &#8211; at the beginning of the file &#8211; can handle it.</p>
<p>All of this form information is in the $_POST array.  The &#8216;key&#8217; for each element is the &#8216;name&#8217; attribute of the form element.</p>
<p>We&#8217;ll also need to use the urlencode() function.  This takes the input and escapes any special characters (like spaces) so that the input is in the proper format.</p>
<p>Here&#8217;s a snippet to read the $_POST variables, encode them, and store the information in some new variables.  Note that you&#8217;d probably want to include some error checking (i.e. check to see if the $_POST[] element is empty, and set an error message if it is).  I&#8217;ll leave that up to you.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$excerpt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'excerpt'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$blog_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blog'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$trackback_uri</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'trackback-uri'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now that have all of our information, we need to create the HTTP header.</p>
<p>You could format this as a special string, but php has a built in function &#8211; stream_context_create &#8211; that will take a formatted array and use it to send an HTTP header.  That&#8217;s what we&#8217;ll be using.</p>
<p>A HTTP header looks something like this&#8230;</p>
<pre>POST http://targeturl.com
Content-Type: application/x-www-form-urlencoded;
charset=utf-8&amp;paramvar=Param+Value&amp;paramvar2=Param+Value+2</pre>
<p>In order to use stream_context_create, we need to create an array in the following format.</p>
<ul>
<li><code>$params['http']['method']</code> is the method (POST)</li>
<li><code>$params['http']['header']</code> is the optional headers (in this case the Content-Type)</li>
<li><code>$params['http']['content']</code> is the actual content &#8211; the parameter variables and values.  This will be the string containing all of the trackback information (the post url, post title, blog name, and excerpt).</li>
</ul>
<p>So, to create the array we need, here&#8217;s a short snippet of code, picking up where we left off before&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'method'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'header'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Content-Type:
    application/x-www-form-urlencoded&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;title=<span style="color: #006699; font-weight: bold;">$title</span>&amp;amp;url=<span style="color: #006699; font-weight: bold;">$url</span>&amp;amp;
    blog_name=<span style="color: #006699; font-weight: bold;">$blog_name</span>&amp;amp;excerpt=<span style="color: #006699; font-weight: bold;">$excerpt</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note that because we used double-quotes (&#8221; &#8220;) when declaring <code>$params['http']['content']</code>, we could simply include the variables in the string &#8211; and PHP will punch in the values for us.</p>
<h3>Send the Header!  Pings Away!</h3>
<p>Now that we&#8217;ve created the array, we&#8217;re ready to create a context and actually send the HTTP request to the target website.</p>
<p>Like I said before, we&#8217;ll use the function <code>stream_context_create</code> to do the work.  Then we&#8217;ll fopen the target, write the header, fclose the target, and save the response.</p>
<p><code>stream_context_create</code> just needs one parameter &#8211; our formatted array.</p>
<p><code>fopen</code> will take a few parameters &#8211; the target URL ($trackback_uri), the mode (&#8216;rb&#8217; for read-only, binary), a boolean option which is false for us, and the context we created with <code>stream_context_create</code>.</p>
<p>Here&#8217;s the actual code snippet.  (Note:  You can only use a context in <code>fopen()</code> in PHP 5 or greater.  You&#8217;ll need to manually make the header if you&#8217;re using PHP 4)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//  Create the context from the formatted array</span>
<span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Open a stream to the url, save it in the pointer $fp</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trackback_uri</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Send the header and get the results, saved in $response</span>
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">stream_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Always close for good measure</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>At this point, you should be able to send trackbacks just fine.</p>
<h3>Read the Result Message &#8211; Error or No?</h3>
<p>Now we need to read the results (saved in $response) and find out if we were successful.</p>
<p>You could just echo it.  The xml tags will be hidden by the browser, so the user will just see the output.  However I like to have the script read it so that I can output a customized message.</p>
<p>One way to do this would be to build an xml parser and find out directly what&#8217;s in the  element.  This is a lot of work for one error message, though.</p>
<p>Instead, we can use our knowledge of the standard format of the message to good use.</p>
<p>First, we know that a success is always returned with .  If we do a stripos or substr search to see if that string is included in $response, we&#8217;ll know if the test was successful or not.</p>
<p>Then, we can isolate the  element and echo that.</p>
<p>Here&#8217;s the code I used to do it.  Not sure if it&#8217;s the most efficient way to do it, but it works&#8230; so why not?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//  If this is true, there was no error</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$outcome</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;All clear! Trackback
        was sent successfully.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Find the beginning and end of the message element</span>
    <span style="color: #000088;">$start_resp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$end_resp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//  substr will return the actual message element,</span>
    <span style="color: #666666; font-style: italic;">//    with the tags included</span>
    <span style="color: #000088;">$outcome</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start_resp</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$end_resp</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start_resp</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//  Replace the tags with blank space</span>
    <span style="color: #000088;">$outcome</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$outcome</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$outcome</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$outcome</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$outcome</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Error: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$outcome</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Echo the results.  I formatted 'error' to be red.</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;
&nbsp;
<span style="color: #006699; font-weight: bold;">$outcome</span>
&nbsp;
&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Recap</h3>
<p>Well, there you have it.  Put all that code together and you should have a functioning example.</p>
<p>Even if you&#8217;re not going to use this on your own site, this is a pretty cool learning activity.  You get to see how contexts and streams work, sending an HTTP header to get a response.</p>
<p>We also make use of the <code>urlencode()</code> function and use <code>stripos()</code> and <code>substr()</code> to do some half-assed xml parsing.  You could use a similar method for reading HTML and parsing tags.</p>
<p>Enjoy, and be sure to leave a comment if there&#8217;s something else you&#8217;d like to see explained in a tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://life-of-brian.com/2008/01/how-to-write-a-php-script-to-send-a-trackback/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>

