<?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>Stephen McIntyre &#187; PHP</title>
	<atom:link href="http://stephenmcintyre.net/category/blog/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://stephenmcintyre.net</link>
	<description>Young Scottish Web Designer</description>
	<lastBuildDate>Thu, 10 Jun 2010 15:25:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Copying Images with allow_url_fopen Switched Off</title>
		<link>http://stephenmcintyre.net/blog/copy-images-allow_url_fopen-off/</link>
		<comments>http://stephenmcintyre.net/blog/copy-images-allow_url_fopen-off/#comments</comments>
		<pubDate>Thu, 20 May 2010 16:06:56 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=506</guid>
		<description><![CDATA[I&#8217;m working on a Facebook app that needs to collect images from an external server. Normally I could use a function like copy to do this, but this requires allow_url_fopen to be on and a lot of providers like mine have this turned off to tighten server security.
Eventually I came across file_get_contents to do the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a Facebook app that needs to collect images from an external server. Normally I could use a function like <a href="http://php.net/copy">copy</a> to do this, but this requires <code>allow_url_fopen</code> to be on and a lot of providers like mine have this turned off to tighten server security.</p>
<p>Eventually I came across <code>file_get_contents</code> to do the job. Since this also needs <code>allow_url_fopen</code> to be on, my post on <a href="/blog/fetch-content-using-php/">fetching page content with cURL</a> came to mind.</p>
<p>This does the same thing without restrictions on privacy and is available on most current setups.<span id="more-506"></span></p>
<h3>Fetching Raw Data with get_page_content</h3>
<p>We start off with our <code>get_page_content</code> method from the last post, which we use to collect page data. We&#8217;re looking to collect the raw data of an image, and here I&#8217;ve used the Google logo as an example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.google.co.uk/intl/en_com/images/srpr/logo1w.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'logo.png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The use of <code>get_page_content</code> is the same as in the last post.</p>
<p>For the last three lines we use a few functions for creating a new file.</p>
<p>The first line creates a handle to a file (which doesn&#8217;t have to exist), which here we&#8217;ve called &#8220;logo.png&#8221;. This is where our image will be copied to. The &#8220;w&#8221; parameter tells the script we are opening the file for it to be directly written to, and we save the resource into <code>$fp</code>.</p>
<p>The second line writes the content that we collected earlier into our image. The raw data will make it display properly since it&#8217;s being saved as an image file.</p>
<p>Calling <code>fclose</code> on the resource closes the handle to it and frees up memory.</p>
<h3>Mimicking the PHP Copy</h3>
<p>Now the script will work fine if you use it as-is, but for a more robust way of handling file copying we&#8217;re going to create a wrapper around it to be called similarly to the <code>copy</code> function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> copy_file<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source</span><span style="color: #339933;">,</span> <span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$source</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here we have a new function with a <code>$source</code> and <code>$destination</code> parameter, identical to <code>copy</code>.</p>
<h3>Handling Failed Requests</h3>
<p>To add even further similarity to the <code>allow_url_fopen</code> method I&#8217;ll add in some boolean return values.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> copy_the_file<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source</span><span style="color: #339933;">,</span> <span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$source</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$fw</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fw</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So now if there is no content in the external file or the new file can&#8217;t be written to your server, the function will return false.</p>
<p>You can add to this with some cURL error checking and a handler on <code>fopen</code>, but it depends how robust it would need to be on your own website.</p>
<p>The great thing is, this script doesn&#8217;t just work with images. Just change the destination file to anything else with a change of extension and it&#8217;ll work exactly the same.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/copy-images-allow_url_fopen-off/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Randomising Database Output</title>
		<link>http://stephenmcintyre.net/blog/randomising-database-output/</link>
		<comments>http://stephenmcintyre.net/blog/randomising-database-output/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 21:13:47 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=294</guid>
		<description><![CDATA[Collecting a random data set from your database can be useful for all kinds of data driven applications including analytics and &#8220;grey areas&#8221; in video game outcomes  (battles, gambling, etc.).
Here I will discuss the use and methods of randomisation in PHP with data from a MySQL database.
Collecting the Data
First we need to grab our [...]]]></description>
			<content:encoded><![CDATA[<p>Collecting a random data set from your database can be useful for all kinds of data driven applications including analytics and &#8220;grey areas&#8221; in video game outcomes  (battles, gambling, etc.).</p>
<p>Here I will discuss the use and methods of randomisation in PHP with data from a MySQL database.<span id="more-294"></span></p>
<h3>Collecting the Data</h3>
<p>First we need to grab our database rows.</p>
<p>For this we uses a simple <code>SELECT</code> statement, where in this example is taken from a &#8220;users&#8221; table with two fields &#8220;ID&#8221; and &#8220;name&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> MySQLi<span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span> DB_USER<span style="color: #339933;">,</span> DB_PASS<span style="color: #339933;">,</span> DB_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT `ID`, `name` FROM `users` ORDER BY `ID` ASC&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ID'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will print out all of the data from our database into an ordered list, arranged by ID from 1 upwards.</p>
<h3>Randomise with PHP</h3>
<p>To then take this data and shuffle its values, we can then take the conveniently named <a href="http://php.net/shuffle">shuffle</a> method in PHP and apply it to our <code>$rows</code> array.</p>
<p>Add this code to the above script at line 13. It takes the data and again prints it out into an ordered list, this time with a randomised data set.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Using the MySQL Query</h3>
<p>Now, since here we are already using a MySQL query, we can utilise the <a href="http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand">RAND()</a> method by adding it to our <code>$query</code> string.</p>
<p>This should be used as an alternative to the PHP shuffle method, so as such should not include the above line with shuffle in it (unless you want to randomise it twice &#8211; but it won&#8217;t make your data any more unique).</p>
<p>Replace your query string with this, on line 3.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT `ID`, `name` FROM `users` ORDER BY RAND()&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice it&#8217;s only the last part (<code>ORDER BY...</code>) that changes.</p>
<p>This code &#8211; or at least the idea &#8211; can be applied to, and is useful for, all programming languages. The syntax will vary between them but fundamentally the results will be the same.</p>
<p>Making a Facebook app? Why not award the user with a random item when they log in once a day, or show a rearranged list of their friends if it&#8217;s otherwise too large to show on a page.</p>
<p>Own a blog? Apply this to your homepage and show some old posts. It will also get their page rankings higher if they are being re-indexed by search engines. It&#8217;s like fresh content!</p>
<p>It will also help your visitors find posts they never knew they were looking for. &#8216;Cos we&#8217;re all just browsing after all <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/randomising-database-output/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UserTweets &#8211; A Simple PHP Twitter Class</title>
		<link>http://stephenmcintyre.net/blog/usertweets-ph-twitter-class/</link>
		<comments>http://stephenmcintyre.net/blog/usertweets-ph-twitter-class/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 21:20:04 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=445</guid>
		<description><![CDATA[For those that are unfamiliar with the Twitter API or are looking to quickly fetch tweets from a user&#8217;s profile, this class will help.
In PHP, all you do is include the class in your document and construct it with a Twitter username (with some options for extra tweaking).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
&#60;?php
class UserTweets
&#123;
  var $username = '';
  [...]]]></description>
			<content:encoded><![CDATA[<p>For those that are unfamiliar with the Twitter API or are looking to quickly fetch tweets from a user&#8217;s profile, this class will help.</p>
<p>In PHP, all you do is include the class in your document and construct it with a Twitter username (with some options for extra tweaking).<span id="more-445"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UserTweets
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$page_content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$encoded_content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$user_info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$result_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$connected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$request_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'since_id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'max_id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'count'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'page'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$option_set</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$option_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'screen_name='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request_options</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$option_set</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$option_string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$req</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://api.twitter.com/1/statuses/user_timeline.json?'</span><span style="color: #339933;">.</span><span style="color: #000088;">$option_string</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$connection</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connected</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result_count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_info</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>Usage</h3>
<p>To fetch a user&#8217;s tweets with default settings (simple method).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$tweets</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserTweets<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_bigSteve'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Setting options with names and values as defined in the <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline">Twitter API Wiki</a>. This example returns 5 results posted on or before the update with ID of 12402600172.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'max_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">12402600172</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'count'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tweets</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserTweets<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_bigSteve'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Displaying tweets in an ordered list.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Tweets&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&#8230;And user info. This data is stripped out of the <code>results</code> array and added into its own named <code>user_info</code> to reduce bloat.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;User Info&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_info</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$info</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' =&gt; '</span><span style="color: #339933;">.</span><span style="color: #000088;">$info</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This script will work on server setups with either the <a href="http://php.net/manual/en/book.curl.php">cURL</a> library or <a href="http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen">allow_url_fopen</a> turned on, taking preference over the faster and more secure cURL. If none are in use, most of the object properties will be empty (like <code>results</code> and <code>page_content</code>) and it will handle itself properly without printing errors to the page.</p>
<p>Hope this class will make it a bit easier for developers to start using the great <a href="http://apiwiki.twitter.com/Twitter-API-Documentation">Twitter API</a> in their own mashups.</p>
<p>Comments are welcome as always.</p>
<p>Have fun <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/usertweets-ph-twitter-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Send a URL Fragment Anchor to PHP</title>
		<link>http://stephenmcintyre.net/blog/fragment-anchor-php/</link>
		<comments>http://stephenmcintyre.net/blog/fragment-anchor-php/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 20:22:56 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=384</guid>
		<description><![CDATA[The URL fragment anchor (or accelerator, ID link or jump point) can be used to relocate the visitor to a specific part of a page. This is done by giving an element an ID then linking to it with a hash symbol (# or number/pound sign) like so:
http://example.com/page.php#content
The &#8220;content&#8221; part of this URL however never [...]]]></description>
			<content:encoded><![CDATA[<p>The URL fragment anchor (or accelerator, ID link or jump point) can be used to relocate the visitor to a specific <strong>part</strong> of a page. This is done by giving an element an ID then linking to it with a hash symbol (# or number/pound sign) like so:</p>
<p><code>http://example.com/page.php#content</code></p>
<p>The &#8220;content&#8221; part of this URL however <strong>never</strong> gets passed to PHP since it is only used client-side. However, with a little bit of help from JavaScript we can do exactly that.<span id="more-384"></span></p>
<p>JavaScript can be used to grab all of the URL data since it runs client-side, which makes it ideal. In the below example we collect the URL, check for the hash tag, and refresh the page replacing the fragment anchor data with a <strong>PHP parameter</strong>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #339933;">=</span> location.<span style="color: #660066;">href</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>parts.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> params <span style="color: #339933;">=</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> mark <span style="color: #339933;">=</span> <span style="color: #3366CC;">'?'</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>params.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        mark <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&amp;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    location.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> mark <span style="color: #339933;">+</span> <span style="color: #3366CC;">'fragment='</span> <span style="color: #339933;">+</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Line 2:</strong> If the URL has a fragment anchor, this will contain more than one element.<br />
<strong>Lines 5-10:</strong> Quick check for any existing PHP parameters, adding an ampersand (&#038;) instead if true.<br />
<strong>Line 11:</strong> Combine the URL data and redirect.</p>
<p>We will then be sent to a page like this:</p>
<p><code>http://example.com/page.php?fragment=content</code></p>
<p>A quick check in PHP then prints the parameter to the page.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fragment'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fragment'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Add this just below the JavaScript content from earlier.</p>
<p>For examples of this script being used in large websites, look for any Facebook URLs that have a &#8220;#!&#8221; in them, and paste that into another tab (try Photo pages). You will then notice that the browser redirects you to a page with a nicer URL but with the same content as you had before.</p>
<p>This is a very neat little trick that is particularly useful in photo galleries with JavaScript support, but has uses for a wide range of projects that control most of their page data client-side.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/fragment-anchor-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom PHP Session Class</title>
		<link>http://stephenmcintyre.net/blog/custom-php-session-class/</link>
		<comments>http://stephenmcintyre.net/blog/custom-php-session-class/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 21:50:54 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=378</guid>
		<description><![CDATA[PHP sessions are great for dealing with log in systems, tracking user activity, and allowing them to save data as they move around your site, like a shopping cart. Writing up what you need to run it though can be a bit painful, so that&#8217;s what lead me onto creating a custom session handling class [...]]]></description>
			<content:encoded><![CDATA[<p>PHP sessions are great for dealing with log in systems, tracking user activity, and allowing them to save data as they move around your site, like a shopping cart. Writing up what you need to run it though can be a bit painful, so that&#8217;s what lead me onto creating a custom session handling class in PHP.<span id="more-378"></span></p>
<p>The script handles sessions within a database to allow it to be more secure and analytically useful. It&#8217;s derived from an article by <a href="http://www.richardwillars.com/articles/php/storing-sessions-in-the-database/">Richard Willars</a> who I owe a lot to for the basis of this script. Anything that I don&#8217;t cover will be available on his website.</p>
<p>Here is the script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
  Session class by Stephen McIntyre
  http://stephenmcintyre.net
*/</span>
<span style="color: #000000; font-weight: bold;">class</span> Session
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$alive</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$dbc</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">session_set_save_handler</span><span style="color: #009900;">&#40;</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'open'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'close'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'read'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'write'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'destroy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'clean'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alive</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">session_write_close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alive</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ini_get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'session.use_cookies'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">session_get_cookie_params</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">session_name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">42000</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'domain'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'secure'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'httponly'</span><span style="color: #009900;">&#93;</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #990000;">session_destroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alive</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> open<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>    
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MYSQLi<span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span> DB_USERNAME<span style="color: #339933;">,</span> DB_PASSWORD<span style="color: #339933;">,</span> DB_NAME<span style="color: #009900;">&#41;</span>
      OR <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not connect to database.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> read<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sid</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sid</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT `data` FROM `sessions` WHERE `id` = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' LIMIT 1&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$fields</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> write<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sid</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;REPLACE INTO `sessions` (`id`, `data`) VALUES ('&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;')&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">affected_rows</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sid</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM `sessions` WHERE `id` = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$_SESSION</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">affected_rows</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> clean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$expire</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM `sessions` WHERE DATE_ADD(`sesh_last_accessed`, INTERVAL &quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$expire</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; SECOND) &lt; NOW()&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">affected_rows</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Before you try to use the class you have to set up your database. I have used exactly the same database setup as Richard here, but can easily be expanded into using user IDs and shopping carts as I mentioned before.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`sessions`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`data`</span> longtext <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`last_accessed`</span> timestamp <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CURRENT_TIMESTAMP <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CURRENT_TIMESTAMP<span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1;</pre></td></tr></table></div>

<p>If you use phpMyAdmin it&#8217;s as simple as adding a database as you normally would and pasting the above into the SQL query box.</p>
<p>For the class to connect to the database, you also need to create some definitions in PHP.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_USERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'root'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_PASSWORD'</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: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_NAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'penguin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_HOST'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This should be placed at the top of your PHP file, replacing the appropriate fields with your own database information.</p>
<p>That&#8217;s it set up. All you have to do now is call it in one line.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Session<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And set our session data with the <code>$_SESSION</code> variable as usual.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Session<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The user ID above will then be stored inside the &#8220;data&#8221; field in your database, in a serialised format that the class will be able to parse.</p>
<p>Since it&#8217;s handled from a class, the variable will also call <code>session_write_close();</code> by itself when the page ends, so no need to call this yourself. If you want to end it early though, you can always call the destructor manually.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Session<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ... extra code ...</span>
<span style="color: #000088;">$session</span><span style="color: #339933;">-&gt;</span>__destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The session can also be deleted (like when logging out) and to do this we just call the delete method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Session<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ... extra code ...</span>
<span style="color: #000088;">$session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>So there you have it, a simple session handling class you can use for any web project.</p>
<p>Also, if you do use this in your applications, please be kind enough to leave the extra header text at the top.</p>
<p>Thanks <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/custom-php-session-class/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Parsing the Kongregate Badge Feed with PHP</title>
		<link>http://stephenmcintyre.net/blog/kongregate-badge-feed-php/</link>
		<comments>http://stephenmcintyre.net/blog/kongregate-badge-feed-php/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 22:38:29 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[The Web]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=350</guid>
		<description><![CDATA[Back in September of last year, Jim Greer of Kongregate.com posted an article in the forums about a new badge data service served in JSON. This was great news for developers, we could finally grab data from our user accounts without having to do a web scrape on our user page.
However, this forum post seems [...]]]></description>
			<content:encoded><![CDATA[<p>Back in September of last year, <a href="http://www.kongregate.com/accounts/jimgreer">Jim Greer of Kongregate.com</a> posted an article in the forums about a new badge data service served in JSON. This was great news for developers, we could finally grab data from our user accounts without having to do a <a href="http://wikipedia.org/wiki/Web_scraping">web scrape</a> on our user page.</p>
<p>However, this <a href="http://www.kongregate.com/forums/4-programming/topics/58167-new-json-feed-of-badge-info">forum post</a> seems to be the only official documentation on what is a very interesting data feed, so it is with this reason that I decided to create this beginner&#8217;s tutorial on how to use it.<span id="more-350"></span></p>
<p>Since Kongregate is a website that hosts Flash games, this data would primarily be used to collect data into such games &#8211; like showing user badges embedded into a Flash dashboard. In this tutorial, I will take a different approach and show you how to collect that data in PHP and build your own activity stream.</p>
<h3>All Badges Feed</h3>
<p>Located at <code>http://www.kongregate.com/badges.json</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Collect 100 squiggles and pwn 10 spiders&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;users_count&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">285194</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Running Start&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;games&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.kongregate.com/games/DrNeroCF/fancy-pants-adventure-world-2&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Fancy Pants Adventure: World 2&quot;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;icon_url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://cdn2.kongregate.com/badge_icons/0000/0367/world2easy.png?1242937128&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;created_at&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2008/01/24 20:26:15 -0800&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;points&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;difficulty&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;easy&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">184</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is an example of an item from the list of game badges in the feed. It contains more than enough information to help make your own application. Each element is labeled so that you can index and call each item when you need to. Further down I&#8217;ll show you how to sort out the data in PHP.</p>
<h3>User Badges Feed</h3>
<p>Can be found at <code>http://www.kongregate.com/accounts/USERNAME/badges.json</code>, replacing USERNAME with your account login name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;badge_id&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">184</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;created_at&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2008/06/23 06:20:42 -0700&quot;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A much simpler looking JSON string. A user&#8217;s badge feed only contains a list of badge IDs and the date and time that the user achieved them. All other data can be found on the global badge feed above, we just match up the IDs.</p>
<h3>Grabbing the Data</h3>
<p>Last week I showed you <a href="/blog/fetch-content-using-php/">how to fetch page content using PHP</a>. We can reuse the <code>get_page_content</code> method here to collect our information from Kongregate. Be sure to add this method above all other code in your PHP file, before adding the content from the box below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$all_badges_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://www.kongregate.com/badges.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$all_badges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$all_badges_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_badges_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://www.kongregate.com/accounts/USERNAME/badges.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_badges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_badges_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This sets up two arrays (<code>$all_badges</code> and <code>$user_badges</code>) that we can use to loop through and match IDs.</p>
<h3>Displaying the Content</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_badges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$badge</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$all_badges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'badge_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span>
        <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'games'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span>
            <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' Badge'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/a&gt;'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;span&gt;'</span><span style="color: #339933;">.</span>
            <span style="color: #0000ff;">' : '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/span&gt;'</span><span style="color: #339933;">.</span>
        <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For each user badge in the <code>$user_badges</code> array we loop through our global badges to find a match and print an output of badge, link, and description to the page.</p>
<p>The break and unset in this code is useful for reducing memory usage on the script.</p>
<p>The break exits the current loop when the badges have been matched &#8211; since we won&#8217;t need to keep looking once we have found what we are looking for.</p>
<p>The unset removes the item from our list of all badges completely, since once it has been found once we won&#8217;t ever need to match it again since we can only have one of each badge.</p>
<h3>Finishing Up</h3>
<p>For a full copy-paste version of the code, feel free to skip to the end of this article, or view a <a href="/examples/kong-badges/">demo page</a> of this script.</p>
<p>Note: the demo page is a rendered example of the end result and does not update. For markup purposes only.</p>
<p>Also note that reading the JSON data directly from the server in this way is not recommended. For better use of this data, please store and manage the information within your own database setup.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$all_badges_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://www.kongregate.com/badges.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$all_badges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$all_badges_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_badges_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://www.kongregate.com/accounts/USERNAME/badges.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_badges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_badges_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_badges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$badge</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$all_badges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'badge_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span>
        <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'games'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span>
            <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' Badge'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/a&gt;'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;span&gt;'</span><span style="color: #339933;">.</span>
            <span style="color: #0000ff;">' : '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/span&gt;'</span><span style="color: #339933;">.</span>
        <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/kongregate-badge-feed-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Fetch Page Content Using PHP</title>
		<link>http://stephenmcintyre.net/blog/fetch-content-using-php/</link>
		<comments>http://stephenmcintyre.net/blog/fetch-content-using-php/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 15:45:20 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=342</guid>
		<description><![CDATA[The web has so many useful services now, many offering their own API that you can draw data from and create your own mashups with. Websites like Twitter, Facebook, Flickr, and Kongregate are just a few.
Their data output is most commonly formatted in RSS (for feed readers), and JSON (a lighter form than XML used [...]]]></description>
			<content:encoded><![CDATA[<p>The web has so many useful services now, many offering their own API that you can draw data from and create your own <a href="http://wikipedia.org/wiki/Mashup_%28web_application_hybrid%29">mashups</a> with. Websites like Twitter, Facebook, Flickr, and Kongregate are just a few.</p>
<p>Their data output is most commonly formatted in <a href="http://wikipedia.org/wiki/RSS">RSS</a> (for feed readers), and <a href="http://wikipedia.org/wiki/JSON">JSON</a> (a lighter form than XML used a lot for AJAX). In this post I&#8217;ll show you how to use the <a href="http://php.net/curl">cURL</a> library to collect data from any public web page. <span id="more-342"></span></p>
<p>First, I&#8217;ve set up a function that will take in the URL you want to use as a parameter and returns the content of the page. This will make it easier later on, particularly if you want to call more than one page in a single script (like feeding Twitter and Facebook status updates into one stream).</p>
<h3>Making a Connection</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 2: </strong> Initialises a cURL connection so we can start using it.</p>
<p><strong>Line 4: </strong> Sets the URL.<br />
<strong>Line 5: </strong> Sets headers to false, so that we don&#8217;t collect extra header data that we won&#8217;t use.<br />
<strong>Line 6: </strong> Sets the return transfer to true. Without setting this, the script will output the content directly to the page, which we don&#8217;t want.</p>
<p><strong>Line 8: </strong> Execute the connection using the options from lines 4-6 and store it into a holding variable. If we hadn&#8217;t set our return transfer to true, this would return empty.</p>
<p><strong>Lines 10-12: </strong> Close connection to the URL and return the holding variable.</p>
<p>So, for example, we can collect JSON formatted data from a user&#8217;s latest Twitter updates:</p>
<h3>Decoding the Data</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$twitter_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://api.twitter.com/1/statuses/user_timeline.json?screen_name=_bigSteve&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tweets</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$twitter_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Where <a href="http://php.net/json_decode">json_decode</a> with its second parameter as true puts our data into an associative array.</p>
<p>We can then display this data as we usually would, looping through the elements:</p>
<h3>Displaying the Text</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;ol&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">.</span>
    <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
    <span style="color: #0000ff;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/ol&gt;&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The <a href="http://php.net/htmlspecialchars">htmlspecialchars</a> method in there is very useful for escaping special characters into their HTML entities, so should be used here since we are sending output to the web page.</p>
<p>After the text we want to output, I&#8217;ve found the other two parameters to be useful in most cases. You can change these if you get some strange characters.</p>
<p>So when you run the script on your own website, it will display an ordered list of Twitter updates onto the page. You can take the info above and convert it to use data from other APIs, using the <code>get_page_content</code> method we created in particular.</p>
<p>If you&#8217;re looking to parse XML documents like RSS feeds, then <a href="http://php.net/manual/book.simplexml.php">SimpleXML</a> is a great library for that. When given the option though, it&#8217;s almost always best to go with JSON, so have a poke around for it in the API docs before you set off.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/fetch-content-using-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Let Your Database Do The Work For You</title>
		<link>http://stephenmcintyre.net/blog/let-your-database-do-it/</link>
		<comments>http://stephenmcintyre.net/blog/let-your-database-do-it/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 17:00:14 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=149</guid>
		<description><![CDATA[In the below examples I&#8217;ll use data from a fictional database table named &#8220;users&#8221;. We&#8217;re looking to return the 10 most recently added users with a date of birth later than January 1st 1970.

Bad practice
One way I&#8217;ve seen it done before is like this:

$query = mysql_query&#40; &#34;SELECT * FROM `users`&#34; &#41;;
while&#40; $row = mysql_fetch_assoc&#40; $query [...]]]></description>
			<content:encoded><![CDATA[<p>In the below examples I&#8217;ll use data from a fictional database table named &#8220;users&#8221;. We&#8217;re looking to return the 10 most recently added users with a date of birth later than January 1st 1970.<br />
<span id="more-149"></span></p>
<h2>Bad practice</h2>
<p>One way I&#8217;ve seen it done before is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;SELECT * FROM `users`&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$query</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dob</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$joined</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$dob</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$joined</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">krsort</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span><span style="color: #339933;">,</span> SORT_NUMERIC <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Name: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Join Date: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Date of Birth: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Good practice</h2>
<p>The alternative uses some SQL techniques to do all of that data sorting within the database query.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dob_limit</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;SELECT `name`,`joined`,`dob` FROM `users` WHERE `dob` &amp;gt;= <span style="color: #006699; font-weight: bold;">$dob_limit</span> ORDER BY `joined` DESC LIMIT 10&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$query</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Name: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Join Date: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Date of Birth: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, the latter script should look far simpler, is easier to write, and works much quicker and efficiently.</p>
<p>Note also that in the second script the fields after the <code>SELECT</code> statement have been <strong>specified</strong> rather than leaving a wild-card asterisk (<code>*</code>). The asterisk returns <strong>all</strong> data in the row that the SQL statement found a match in, and the more data we have in a row the more data could become redundant as we extract it.</p>
<p>For example, consider that the <code>users</code> table had extra fields containing first name, surname, website URL, small personal biography, home town, favourite film, favourite food, and so on. For each row you extract, all seven of those become <strong>unused</strong> pieces of data in the memory on your server and possibly even the computers of your users. Multiply that seven by each row in your database, then by how many times the query will be called, and things start to add up pretty quickly.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2009/12/db-users.jpg"><img class="aligncenter size-full wp-image-224" title="db-users" src="http://stephenmcintyre.net/wp-content/uploads/2009/12/db-users.jpg" alt="" width="300" height="201" /></a></p>
<p>The point is then that if you start with a <strong>clear idea</strong> on what you actually need in order to use your script, you can be specific in what data you need to collect from your database. Doing so decreases your page load times and lowers your bandwidth, usually with dramatic results.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/let-your-database-do-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The PHP Include</title>
		<link>http://stephenmcintyre.net/blog/the-php-include/</link>
		<comments>http://stephenmcintyre.net/blog/the-php-include/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 11:00:05 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=158</guid>
		<description><![CDATA[At the point that your site becomes more complex, you look to ways of simplifying your page code to give yourself less headaches. Ultimately you&#8217;re looking for a way to separate that header that you&#8217;ve copy-pasted so many times to different static web pages, into a page that all your other pages can call, and [...]]]></description>
			<content:encoded><![CDATA[<p>At the point that your site becomes more complex, you look to ways of simplifying your page code to give yourself less headaches. Ultimately you&#8217;re looking for a way to separate that header that you&#8217;ve copy-pasted so many times to different static web pages, into a page that all your other pages can call, and will stay the same.</p>
<p>If like me you hadn&#8217;t heard about PHP until this point where you are looking to add in other scripts to your web pages, then now is a very good time to learn about PHP includes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'header.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><span id="more-158"></span></p>
<p>One thing to note before I begin is that if you&#8217;re ever told to use a <code>frame</code> or <code>iframe</code> tag for this kind of thing, don&#8217;t. There&#8217;s plenty of articles on why this is, but the main reasons I don&#8217;t use them is that it doesn&#8217;t play nicely with the other scripts on the page (like JavaScript and CSS) and are a pain for error checking in web development.</p>
<p>PHP displays the page as if it had been generated from a single HTML script, and this after all is what we&#8217;re looking for.</p>
<p>Using a PHP include is as simple as pasting in the code before the jump. All you do is replace the &#8216;header.html&#8217; with a relative path to the file you want added in.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'header.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'path/to/menu.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'../footer.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Just remember to enclose them in the standard <code>&lt;?php</code> and <code>?&gt;</code> tags. Oh yeah, and you have to save whatever files that use them with a &#8216;.php&#8217; extension instead of &#8216;.html&#8217; so that it can read those special tags and run the code inside them.</p>
<p><strong>File: index.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;container&quot;&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'header.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'path/to/menu.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;p&gt;
  Some dummy content text goes here to fill space.
&lt;/p&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'../footer.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;/div&gt;</pre></td></tr></table></div>

<p><strong>File: header.html</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>My First PHP Page<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Hello World<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span></pre></td></tr></table></div>

<p><strong>File: menu.html</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Home<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Gallery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Contact<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></td></tr></table></div>

<p><strong>File: footer.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;p&gt;
  &lt;em&gt;
    Generated on 
    <span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'jS M Y \a\t g:i a'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;/em&gt;
&lt;/p&gt;</pre></td></tr></table></div>

<p>In &#8216;footer.php&#8217;, <a href="http://php.net/date">date</a> is a PHP method like <code>include</code> that generates a string of text from the time that you run it. The fancy letters in the brackets just make it return the right information you want it to.</p>
<p>The <a href="http://php.net/echo">echo</a> part takes the info from <code>date()</code> and writes it to the screen, so your end result is something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">Generated on 13th Feb 2009 at 23:31</pre></div></div>

<p>See it in action by <a href="/examples/php-include/files/index.php">viewing the full page</a>.</p>
<p><strong>Further reading</strong></p>
<p>As you can see, when used wisely PHP includes can be very useful and efficient for your web development and helps organise all your files. Other methods you can use are <code>require</code>, <code>include_once</code>, and <code>require_once</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'header.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'path/to/menu.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'../footer.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>include:</strong> Searches for the file in brackets. If found, adds data into script, otherwise ignores and continues.</p>
<p><strong>require:</strong> Similar to include, but if no file is found exits the script and displays an error. Useful for adding in files necessary to how your script will run.</p>
<p><strong>include_once and require_once:</strong> Will only add data in once. If there are multiple calls of these methods to the same file in one script, only the first call will be used. Useful for things like <a href="http://php.net/manual/en/language.oop5.php">PHP classes</a>.</p>
<p>These are used and written in almost exactly the same way as the <code>include</code> examples. At the time you need to consider using one of these, you will be able to tell from the list above which one will best suit your project.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/the-php-include/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
