<?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>code-diesel &#187; functions</title>
	<atom:link href="http://www.codediesel.com/tag/functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Unpacking binary data in PHP</title>
		<link>http://www.codediesel.com/php/unpacking-binary-data/</link>
		<comments>http://www.codediesel.com/php/unpacking-binary-data/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 09:38:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2707</guid>
		<description><![CDATA[To set the stage we will start with a programming problem, this will keep the discussion anchored to a relevant context. The problem is this : We want to write a function that takes a image file as an argument and tells us whether the file is a GIF image; irrelevant with whatever the extension [...]]]></description>
			<content:encoded><![CDATA[<p>To set the stage we will start with a programming problem, this will keep the discussion anchored to a relevant context. The problem is this : We want to write a function that takes a image file as an argument and tells us whether the file is a GIF image; irrelevant with whatever the extension the file may have. We are not to use any GD library functions.</p>
<h4>A GIF file header</h4>
<p>With the requirement that we are not allowed to use any graphics functions, to solve the problem we need to get the relevant data from the GIF file itself. Unlike a HTML or XML or other text format files, a GIF file and most other image formats are stored in a binary format. Most binary files carry a header at the top of the file which provides the meta information regarding the particular file. We can use this information to find out the type of the file and other things, such as height an width in case of a GIF file. A typical raw GIF header is shown below, using a hex editor such as <a target="_blank" href="http://www.x-ways.net/winhex/index-m.html">WinHex</a>.<br />
<span id="more-2707"></span><br />
<a href="http://www.codediesel.com/wp-content/uploads/2010/09/winhex.gif"><img src="http://www.codediesel.com/wp-content/uploads/2010/09/winhex.gif" alt="" title="winhex" width="435" height="82" class="aligncenter size-full wp-image-2709" /></a></p>
<p>The detailed description of the header is given below.</p>

<div class="wp_codebox"><table><tr id="p27079"><td class="code" id="p2707code9"><pre class="text" style="font-family:monospace;">Offset   Length   Contents
  0      3 bytes  &quot;GIF&quot;
  3      3 bytes  &quot;87a&quot; or &quot;89a&quot;
  6      2 bytes  &lt;Logical Screen Width&gt;
  8      2 bytes  &lt;Logical Screen Height&gt;
 10      1 byte   bit 0:    Global Color Table Flag (GCTF)
                  bit 1..3: Color Resolution
                  bit 4:    Sort Flag to Global Color Table
                  bit 5..7: Size of Global Color Table: 2^(1+n)
 11      1 byte   &lt;Background Color Index&gt;
 12      1 byte   &lt;Pixel Aspect Ratio&gt;
 13      ? bytes  &lt;Global Color Table(0..255 x 3 bytes) if GCTF is one&gt;
         ? bytes  &lt;Blocks&gt;
         1 bytes  &lt;Trailer&gt; (0x3b)</pre></td></tr></table></div>

<p>So to check if the image file is a valid GIF, we need to check the starting 3 bytes of the header, which have the &#8216;GIF&#8217; marker, and the next 3 bytes, which give the version number; either &#8217;87a&#8217; or &#8217;89a&#8217;. It is for tasks such as these that the unpack() function is indispensable. Before we look at the solution, we will take a quick look at the unpack() function itself.</p>
<h4>Using the unpack() function</h4>
<p><a target="_blank" href="http://php.net/manual/en/function.unpack.php">unpack()</a> is the complement of <a target="_blank" href="http://www.php.net/manual/en/function.pack.php">pack()</a> &#8211; it transforms binary data into an associative array based on the format specified. It is somewhat along the lines of <em>sprintf</em>, transforming string data according to some given format. These two functions allow us to read and write buffers of binary data according to a specified format string. This easily enables a programmer to exchange data with programs written in other languages or other formats. Take a small example.</p>

<div class="wp_codebox"><table><tr id="p270710"><td class="code" id="p2707code10"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unpack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'C*'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'codediesel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will print the following, decimal codes for &#8216;codediesel&#8217; :</p>

<div class="wp_codebox"><table><tr id="p270711"><td class="code" id="p2707code11"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array</span>
  <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">99</span>
  <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">111</span>
  <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">100</span>
  <span style="color: #cc66cc;">4</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">101</span>
  <span style="color: #cc66cc;">5</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">100</span>
  <span style="color: #cc66cc;">6</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">105</span>
  <span style="color: #cc66cc;">7</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">101</span>
  <span style="color: #cc66cc;">8</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">115</span>
  <span style="color: #cc66cc;">9</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">101</span>
  <span style="color: #cc66cc;">10</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">108</span></pre></td></tr></table></div>

<p>In the above example the first argument is the format string and the second the actual data. The format string specifies how the data argument should be parsed. In this example the first part of the format &#8216;C&#8217;, specifies that we should treat the first character of the data as a unsigned byte. The next part &#8216;*&#8217;, tells the function to apply the previously specified format code to all the remaining characters.</p>
<p>Although this may look confusing, the next section provides a concrete example.</p>
<h4>Grabbing the header data</h4>
<p>Below is the  solution to our GIF problem using the unpack() function. The  <em>is_gif()</em> function will return true if the given file is in a GIF format.</p>

<div class="wp_codebox"><table><tr id="p270712"><td class="code" id="p2707code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_gif<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_file</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Open the image file in binary mode */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</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;">$image_file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Read 20 bytes from the top of the file */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Create a format specifier */</span>
    <span style="color: #000088;">$header_format</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A6version'</span><span style="color: #339933;">;</span>  <span style="color: #009933; font-style: italic;"># Get the first 6 bytes
</span>
    <span style="color: #009933; font-style: italic;">/* Unpack the header data */</span>
    <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unpack</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$header_format</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ver</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$header</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'version'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ver</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'GIF87a'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$ver</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'GIF89a'</span><span style="color: #009900;">&#41;</span>? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Run our example */</span>
<span style="color: #000000; font-weight: bold;">echo</span> is_gif<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aboutus.gif&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The important line to note is the format specifier. The &#8216;A6&#8242; characters specifies that the unpack() function is to get the the first 6 bytes of the data and interpret it as a string. The retrieved data is then stored in an associate array with the key named &#8216;version&#8217;.</p>
<p>Another example is given below. This returns some additional header data of the GIF file, including the image width and height.</p>

<div class="wp_codebox"><table><tr id="p270713"><td class="code" id="p2707code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_gif_header<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_file</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Open the image file in binary mode */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</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;">$image_file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Read 20 bytes from the top of the file */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Create a format specifier */</span>
    <span style="color: #000088;">$header_format</span> <span style="color: #339933;">=</span> 
            <span style="color: #0000ff;">'A6Version/'</span> <span style="color: #339933;">.</span> <span style="color: #009933; font-style: italic;"># Get the first 6 bytes
</span>            <span style="color: #0000ff;">'C2Width/'</span> <span style="color: #339933;">.</span>   <span style="color: #009933; font-style: italic;"># Get the next 2 bytes
</span>            <span style="color: #0000ff;">'C2Height/'</span> <span style="color: #339933;">.</span>  <span style="color: #009933; font-style: italic;"># Get the next 2 bytes
</span>            <span style="color: #0000ff;">'C1Flag/'</span> <span style="color: #339933;">.</span>    <span style="color: #009933; font-style: italic;"># Get the next 1 byte
</span>            <span style="color: #0000ff;">'@11/'</span> <span style="color: #339933;">.</span>       <span style="color: #009933; font-style: italic;"># Jump to the 12th byte
</span>            <span style="color: #0000ff;">'C1Aspect'</span><span style="color: #339933;">;</span>    <span style="color: #009933; font-style: italic;"># Get the next 1 byte
</span>
    <span style="color: #009933; font-style: italic;">/* Unpack the header data */</span>
    <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unpack</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$header_format</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ver</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$header</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Version'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ver</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'GIF87a'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$ver</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'GIF89a'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$header</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Run our example */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>get_gif_header<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aboutus.gif&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above example will print the following when run.</p>

<div class="wp_codebox"><table><tr id="p270714"><td class="code" id="p2707code14"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>Version<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> GIF89a
    <span style="color: #009900;">&#91;</span>Width1<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">97</span>
    <span style="color: #009900;">&#91;</span>Width2<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
    <span style="color: #009900;">&#91;</span>Height1<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">33</span>
    <span style="color: #009900;">&#91;</span>Height2<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
    <span style="color: #009900;">&#91;</span>Flag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">247</span>
    <span style="color: #009900;">&#91;</span>Aspect<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Below we will go into the details of how the format specifier works. I&#8217;ll split the format, giving the details for each character.</p>

<div class="wp_codebox"><table><tr id="p270715"><td class="code" id="p2707code15"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$header_format</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A6Version/C2Width/C2Height/C1Flag/@11/C1Aspect'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p270716"><td class="code" id="p2707code16"><pre class="text" style="font-family:monospace;">A - Read a byte and interpret it as a string. 
    Number of bytes to read is given next
6 - Read a total of 6 bytes, starting from position 0
Version - Name of key in the associative array where data 
    retrieved by 'A6' is stored
&nbsp;
/ - Start a new code format
C - Interpret the next data as an unsigned byte
2 - Read a total of 2 bytes
Width - Key in the associative array
&nbsp;
/ - Start a new code format
C - Interpret the data as an unsigned byte
2 - Read a total of 2 bytes
Height- Key in the associative array
&nbsp;
/ - Start a new code format
C - Interpret the data as an unsigned byte
1 - Read a total of 2 bytes
Flag - Key in the associative array
&nbsp;
/ - Start a new code format
@ - Move to the byte offset specified by the following number.
      Remember that the first position in the binary string is 0. 
11 - Move to position 11
&nbsp;
/ - Start a new code format
C - Interpret the data as an unsigned byte
1 - Read a total of 1 bytes
Aspect - Key in the associative array</pre></td></tr></table></div>

<p>More format options can be found <a target="_blank" href="http://www.php.net/manual/en/function.pack.php">here</a>. Although I&#8217;ve only shown a small example, the pack/unpack is capable of much complex work than presented here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/unpacking-binary-data/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Easy way to build GET query strings in php</title>
		<link>http://www.codediesel.com/php/building-get-query-strings-php/</link>
		<comments>http://www.codediesel.com/php/building-get-query-strings-php/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 14:10:58 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=127</guid>
		<description><![CDATA[Passing variables with a url is such a frequent thing programmers do that most of you may think this post is unwarranted. We call this method of passing variables as GET, the other being POST. It is one of those things which can be easily done in php. Lets take an example. You are to [...]]]></description>
			<content:encoded><![CDATA[<p>Passing variables with a url is such a frequent thing programmers do that most of you may think this post is unwarranted. We call this method of passing variables as GET, the other being POST. It is one of those things which can be easily done in php. Lets take an example. You are to query a database and for that you need to send three variables via GET &#8211; city, id, paid.<br />
The common way to pass them via GET is to construct a query string as below:<br />
<span id="more-127"></span></p>

<div class="wp_codebox"><table><tr id="p12723"><td class="code" id="p127code23"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* assume we want to pass this variables */</span>
<span style="color: #000088;">$city_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;new york&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$invoice_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3456</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$paid</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;city=<span style="color: #006699; font-weight: bold;">{$city_name}</span>&amp;id=<span style="color: #006699; font-weight: bold;">{$invoice_id}</span>&amp;paid=<span style="color: #006699; font-weight: bold;">{$paid}</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.com?&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The problem is that most php programmers get stuck using the above method. The method is fine for three to four variables, but for more than that the code gets hard to read and maintain, introducing subtle bugs in the constructed query.</p>
<p>The best way to pass GET variables is to use the <em>http_build_query()</em> function available from php version 5; which takes an array of variables and builds a nice URL encoded string which you can append to a url. And example is shown below.</p>

<div class="wp_codebox"><table><tr id="p12724"><td class="code" id="p127code24"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$city_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;new york&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$invoice_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3456</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$paid</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'city'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$city_name</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$invoice_id</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'paid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$paid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.com?&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the above example the array contained the variable names and their values. You can also pass an array containing only values and let the function fill the variable name using the array index and a variable you supply (as a second parameter). For example if you want to pass an array of six cities than you can do the following, the second parameter of the function being the query variable.</p>

<div class="wp_codebox"><table><tr id="p12725"><td class="code" id="p127code25"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paris'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'new york'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'florence'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'london'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'berlin'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'delhi'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.php?&quot;</span> <span style="color: #339933;">.</span>
        <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'city'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The generated url will look like this:</p>
<p>http://www.example.php/?city0=paris&#038;city1=new+york&#038;city2=florence&#038;city3=london&#038;city4=berlin&#038;city5=delhi</p>
<p>The third parameter to the function is not necessary as the function defaults to using &#8216;&#038;&#8217; as a separator. But I prefer making it explicit.<br />
You can also easily pass a complex array as below:</p>

<div class="wp_codebox"><table><tr id="p12726"><td class="code" id="p127code26"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$city_name</span>      <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;new york&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$invoice_id</span>     <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3456</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$currency_name</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;euro&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span>          <span style="color: #339933;">=</span> <span style="color: #cc66cc;">345</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$receipt_no</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;fgf44545&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'city'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$city_name</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$invoice_id</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'paid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'currency'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$currency_name</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'amount'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'receipt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$receipt_no</span><span style="color: #009900;">&#41;</span>                
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.php?&quot;</span> <span style="color: #339933;">.</span>
        <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which will create a url encoded query as following:</p>
<p>http://www.example.com?city=new+york&#038;id=3456&#038;paid%5Bcurrency%5D=euro&#038;paid%5Bamount%5D=345&#038;paid%5Breceipt%5D=fgf44545</p>
<p>To go the reverse way, we can parse the encoded url above using the code below:</p>

<div class="wp_codebox"><table><tr id="p12727"><td class="code" id="p127code27"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url_to_parse</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.com?city=new+york&amp;id=3456&amp;paid%5Bcurrency%5D=euro&amp;paid%5Bamount%5D=345&amp;paid%5Breceipt%5D=fgf44545&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$parsed_url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url_to_parse</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parsed_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* This will output the following array */</span>
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>scheme<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> http
    <span style="color: #009900;">&#91;</span>host<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> www<span style="color: #339933;">.</span>example<span style="color: #339933;">.</span>com
    <span style="color: #009900;">&#91;</span>query<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> city<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #339933;">+</span>york<span style="color: #339933;">&amp;</span>id<span style="color: #339933;">=</span><span style="color: #cc66cc;">3456</span><span style="color: #339933;">&amp;</span>paid<span style="color: #339933;">%</span>5Bcurrency<span style="color: #339933;">%</span>5D<span style="color: #339933;">=</span>euro<span style="color: #339933;">&amp;</span>paid<span style="color: #339933;">%</span>5Bamount<span style="color: #339933;">%</span>5D<span style="color: #339933;">=</span><span style="color: #cc66cc;">345</span><span style="color: #339933;">&amp;</span>paid<span style="color: #339933;">%</span>5Breceipt<span style="color: #339933;">%</span>5D<span style="color: #339933;">=</span>fgf44545
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Now we can further parse the url query string stored in the &#8216;query&#8217; variable above:</p>

<div class="wp_codebox"><table><tr id="p12728"><td class="code" id="p127code28"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$parsed_url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">parse_str</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url_query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* This will output the following array */</span>
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>city<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> york
    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3456</span>
    <span style="color: #009900;">&#91;</span>paid<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>currency<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> euro
            <span style="color: #009900;">&#91;</span>amount<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">345</span>
            <span style="color: #009900;">&#91;</span>receipt<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> fgf44545
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>As can be seen the http_build_query() function really makes constructing GET queries simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/building-get-query-strings-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Simple Pagination in PHP tutorial</title>
		<link>http://www.codediesel.com/php/simple-pagination-in-php/</link>
		<comments>http://www.codediesel.com/php/simple-pagination-in-php/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 05:03:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=118</guid>
		<description><![CDATA[PHP pagination tutorial using the pears pager library]]></description>
			<content:encoded><![CDATA[<p>Pagination is a frequent requirement in web development projects. Most PHP developers must have already implementated paging in one form or other in their projects. In this post we will see how to add pagination the easy way using PEAR&#8217;s Pager class. Note that in all the posts I use PHP 5.x.x, so if you are still stuck at version 4.x.x, its already time to upgrade.<br />
<span id="more-118"></span></p>
<p>Its always nice to see some working code first, before getting into the details. So here goes.</p>

<div class="wp_codebox"><table><tr id="p11829"><td class="code" id="p118code29"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Pager/Pager.php'</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* We will bypass the database connection code ... */</span>
<span style="color: #000088;">$sqlQuery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SOME SQL QUERY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$totalRows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalRows</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pager</span> <span style="color: #339933;">=</span> Pager<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above code will return the following pagination links.</p>
<p>1   |   2   |   3   |   4   |   5   |   6   |   7   |   8   |   9      »      [10]</p>
<p>Assuming the page where the above code is included is named &#8216;dataPage.php&#8217;; the pager links will have a url like follows:</p>
<p>dataPage.php?pageID=<em>n</em></p>
<p>where <em>n</em> is the page number.</p>
<p>You can see how easy it is using the Pager class. All the link building is handled by the class. Now lets go into the details.</p>
<h4>1. Installation</h4>
<p>If you are installing using the Pear installer, enter the following at the command prompt to install Pager.</p>

<div class="wp_codebox"><table><tr id="p11830"><td class="code" id="p118code30"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\pear install Pager</pre></td></tr></table></div>

<p>Or else you can just download the module from <a href="http://pear.php.net/package/Pager/download">here</a> and then copy it to your Pear includes directory, which will probably be &#8216;c:\<em>your php installation directory</em>\Pear&#8217;.</p>
<h4>2. A simple example</h4>
<p>In the following example we will create a pagination for a &#8216;logs&#8217; table.</p>

<div class="wp_codebox"><table><tr id="p11831"><td class="code" id="p118code31"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Include the Pear::Pager file */</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pager/Pager.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Replace this with your database details */</span>
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> username<span style="color: #339933;">,</span> password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>database name<span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* First we need to get the total rows in the table */</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT count(*) AS total FROM logs&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Total number of rows in the logs table */</span>
<span style="color: #000088;">$totalItems</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set some options for the Pager */</span>
<span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// Sliding or Jumping mode. See below.</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// Total rows to show per page</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// See below</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the Pager class with the above options */</span>
<span style="color: #000088;">$pager</span> <span style="color: #339933;">=</span> Pager<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the links */</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The following code will retreive the result using the pager options */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The function below will get the page offsets to be used with
the database query. For e.g if we are on the third page then the
$from variable will have the value of '21' (we are showing 10 items per 
page, remember) and the $to variable will have the value of '30'.
*/</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOffsetByPageId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* The MySQL 'LIMIT' clause index starts from '0', 
    so decrease the $from by 1 */</span>
<span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$from</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The number of rows to get per query */</span>
<span style="color: #000088;">$perPage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'perPage'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM alogs LIMIT <span style="color: #006699; font-weight: bold;">$from</span> , <span style="color: #006699; font-weight: bold;">$perPage</span>&quot;</span><span style="color: #339933;">,</span>
<span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">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_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009933; font-style: italic;">/* Do something with the query results */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the Pager class there are two modes to display the pagination: Sliding &amp; Jumping.</p>
<p>With Pager in &#8220;Sliding&#8221; mode the pagination links change smoothly, and the current page is always shown at the center of the &#8220;window&#8221; (except for the start and end pages). When in Sliding mode the delta option implies how many links to show on the left and right of the current page link.<br />
For e.g with a delta set to &#8217;3&#8242; the links are displayed as shown below, page 10 being the current page:</p>
<p>[1]      «      7   |   8   |   9   |   <strong>10</strong> |   11   |   12   |   13      »      [13]</p>
<p>With delta option set to &#8217;4&#8242; the links are shown as below. As you can see there are four links to the left and right of the current page.</p>
<p>[1]      «      5   |   6   |   7   |   8   |   <strong>9</strong> |   10   |   11   |   12   |   13      »      [13]</p>
<p>With the Pager in &#8220;Jumping&#8221; mode and delta set to &#8217;4&#8242; the Pager always shows the same 4 page links while you are on one of these pages.<br />
For e.g with a delta set to &#8217;4&#8242; the links are displayed as shown below, page 5 being the current page:</p>
<p>[1] &lt;&lt; Back <strong>5</strong> 6 7 8  Next &gt;&gt; [13]</p>
<p>If you are on page &#8217;9&#8242; the links displayed are as below:</p>
<p>[1] &lt;&lt; Back <strong>9</strong> 10 11 12  Next &gt;&gt; [13]</p>
<h4>3. More options</h4>
<p>You can also change the default separator to any character or image you like.</p>

<div class="wp_codebox"><table><tr id="p11832"><td class="code" id="p118code32"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'separator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">','</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will output the following:<br />
[1]      «      1   ,   2   ,   3   ,   4   ,   5   ,   6   ,   7   ,   8   ,   9      »      [13]</p>
<p>If you would like to add images to the &#8216;next&#8217; and &#8216;previous&#8217; links, use the following options:</p>

<div class="wp_codebox"><table><tr id="p11833"><td class="code" id="p118code33"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'nextImg'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;next.png&quot;  /&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'prevImg'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;prev.png&quot;  /&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which will display the links as shown below:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2008/10/pager2.gif"><img class="aligncenter size-full wp-image-121" style="border:1px solid #c0c0c0;" title="pager2" src="http://www.codediesel.com/wp-content/uploads/2008/10/pager2.gif" alt="" width="472" height="32" /></a></p>
<p>The default number of spaces before each separator is three. To change the number of spaces use the &#8216;spacesBeforeSeparator&#8217; option. For e.g:</p>

<div class="wp_codebox"><table><tr id="p11834"><td class="code" id="p118code34"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #0000ff;">'spacesBeforeSeparator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<h4>4. Styling</h4>
<p>You can style the links by putting them in a div tag and applying a css. A example css and its output is shown below.</p>
<p>Add a div tag:</p>

<div class="wp_codebox"><table><tr id="p11835"><td class="code" id="p118code35"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;pager&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Apply CSS:</p>

<div class="wp_codebox"><table><tr id="p11836"><td class="code" id="p118code36"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.pager</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.pager</span> a <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">17px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">17px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#0C74BA</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.pager</span> a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#c0c0c0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>The output:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2008/10/pager.gif"><img class="aligncenter size-full wp-image-119" style="border:1px solid #c0c0c0;" title="pager" src="http://www.codediesel.com/wp-content/uploads/2008/10/pager.gif" alt="pager" width="376" height="35" /></a></p>
<p>That&#8217;s it. There are many other options you can use with the Pager class, the details which you can find in the Pager <a href="http://pear.php.net/manual/en/package.html.pager.php" target="_blank">documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/simple-pagination-in-php/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>WHOIS directory lookup in PHP</title>
		<link>http://www.codediesel.com/php/whois-directory-lookup-in-php/</link>
		<comments>http://www.codediesel.com/php/whois-directory-lookup-in-php/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 12:23:11 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=104</guid>
		<description><![CDATA[Net_Whois lets you query a internet name directory service in a easy to use manner. WHOIS is a protocol which is widely used to determine the owner of a domain name, an IP address by querying an official name database. One possible application I can think of is to see if DNS servers are set [...]]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://pear.php.net/package/Net_Whois">Net_Whois</a> lets you query a internet name directory service in a easy to use manner. <a target="_blank" href="http://www.faqs.org/rfcs/rfc3912.html">WHOIS</a> is a protocol which is widely used to determine the owner of a domain name, an IP address by querying an official name database. One possible application I can think of is to see if DNS servers are set correctly for various domains I handle regularly.<br />
<span id="more-104"></span></p>
<h4>Installation</h4>
<p>Net_Whois being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p10439"><td class="code" id="p104code39"><pre class="text" style="font-family:monospace;">pear install Net_Whois-1.0.2</pre></td></tr></table></div>

<h4>Usage</h4>
<p>The current library uses the following servers to execute the query:<br />
whois.crsnic.net<br />
whois.networksolutions.com<br />
whois.nic.mil<br />
whois.nic.gov<br />
whois.arin.net<br />
whois.ripe.net<br />
whois.apnic.net<br />
whois.ripn.net<br />
whois.ra.net<br />
.whois-servers.net<br />
whois.6bone.net<br />
whois.registro.br</p>
<p>A example code is shown below.</p>

<div class="wp_codebox"><table><tr id="p10440"><td class="code" id="p104code40"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;Net/Whois.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$whois</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Net_Whois<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009933; font-style: italic;">/* You can add other NIC server as a second parameter. */</span>
    <span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$whois</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Note that the class returns a string, so you would need to use regular expressions to get to the relevant information. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/whois-directory-lookup-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ping a server using PHP</title>
		<link>http://www.codediesel.com/php/ping-a-server-using-php/</link>
		<comments>http://www.codediesel.com/php/ping-a-server-using-php/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 11:58:05 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=103</guid>
		<description><![CDATA[PEAR&#8217;s Net_Ping is a niffty wrapper class for executing ping calls from PHP. You can use it to check if a remote server is responding correctly. The library can be download from  here. Installation Net_Ping being a Pear package we will use the Pear installer to download and install it. I recommend to always use [...]]]></description>
			<content:encoded><![CDATA[<p>PEAR&#8217;s Net_Ping is a niffty wrapper class for executing ping calls from PHP. You can use it to check if a remote server is responding correctly. The library can be download from  <a target="_blank" href="http://pear.php.net/package/Net_Ping">here</a>. </p>
<h4>Installation</h4>
<p>Net_Ping being a Pear package we will use the Pear installer to download and install it. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p10344"><td class="code" id="p103code44"><pre class="text" style="font-family:monospace;">pear install Net_Ping-2.4.4</pre></td></tr></table></div>

<h4>Usage</h4>
<p>A example of using Net_Ping is given below.<br />
<span id="more-103"></span></p>

<div class="wp_codebox"><table><tr id="p10345"><td class="code" id="p103code45"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;Net/Ping.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ping</span> <span style="color: #339933;">=</span> Net_Ping<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ping</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$ping</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</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;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #009933; font-style: italic;">/* Number of packets to send */</span>
      <span style="color: #000088;">$ping</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setArgs</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'count'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$rawData</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ping</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ping</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'google.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rawData</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;">?&gt;</span></pre></td></tr></table></div>

<p>The output of the same is shown below.</p>

<div class="wp_codebox"><table><tr id="p10346"><td class="code" id="p103code46"><pre class="php" style="font-family:monospace;">Net_Ping_Result Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_icmp_sequence<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">310</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">300</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">318</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">301</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>_target_ip<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99
    <span style="color: #009900;">&#91;</span>_bytes_per_request<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">32</span>
    <span style="color: #009900;">&#91;</span>_bytes_total<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">128</span>
    <span style="color: #009900;">&#91;</span>_ttl<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">238</span>
    <span style="color: #009900;">&#91;</span>_raw_data<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Pinging google<span style="color: #339933;">.</span>com <span style="color: #009900;">&#91;</span>64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #009900;">&#93;</span> with <span style="color: #cc66cc;">32</span> bytes of data<span style="color: #339933;">:</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Reply from 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span> bytes<span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span> <span style="color: #990000;">time</span><span style="color: #339933;">=</span>310ms TTL<span style="color: #339933;">=</span><span style="color: #cc66cc;">238</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Reply from 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span> bytes<span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span> <span style="color: #990000;">time</span><span style="color: #339933;">=</span>300ms TTL<span style="color: #339933;">=</span><span style="color: #cc66cc;">238</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Reply from 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span> bytes<span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span> <span style="color: #990000;">time</span><span style="color: #339933;">=</span>318ms TTL<span style="color: #339933;">=</span><span style="color: #cc66cc;">238</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Reply from 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span> bytes<span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span> <span style="color: #990000;">time</span><span style="color: #339933;">=</span>301ms TTL<span style="color: #339933;">=</span><span style="color: #cc66cc;">238</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Ping statistics <span style="color: #000000; font-weight: bold;">for</span> 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span>     Packets<span style="color: #339933;">:</span> Sent <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> Received <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> Lost <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">%</span> loss<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Approximate <span style="color: #990000;">round</span> trip times in milli<span style="color: #339933;">-</span>seconds<span style="color: #339933;">:</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span>     Minimum <span style="color: #339933;">=</span> 300ms<span style="color: #339933;">,</span> Maximum <span style="color: #339933;">=</span> 318ms<span style="color: #339933;">,</span> Average <span style="color: #339933;">=</span> 307ms
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>_sysname<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> windows
    <span style="color: #009900;">&#91;</span>_round_trip<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #990000;">min</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">300</span>
            <span style="color: #009900;">&#91;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">318</span>
            <span style="color: #009900;">&#91;</span>avg<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">307</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>_transmitted<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
    <span style="color: #009900;">&#91;</span>_received<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
    <span style="color: #009900;">&#91;</span>_loss<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/ping-a-server-using-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Reading raw POST data in PHP</title>
		<link>http://www.codediesel.com/php/reading-raw-post-data-in-php/</link>
		<comments>http://www.codediesel.com/php/reading-raw-post-data-in-php/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 07:00:55 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=101</guid>
		<description><![CDATA[A quick tip for reading raw http POST data in PHP. For example if we have a xml posted to a page, we can read the raw data with the following code. $xml = file_get_contents&#40;'php://input'&#41;; We could use $HTTP_RAW_POST_DATA instead, but many times it does not work due to some php.ini settings. Note that &#8216;php://input&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>A quick tip for reading raw http POST data in PHP. For example if we have a xml posted to a page, we can read the raw data with the following code.</p>

<div class="wp_codebox"><table><tr id="p10148"><td class="code" id="p101code48"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php://input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We could use <em>$HTTP_RAW_POST_DATA</em> instead, but many times it does not work due to some php.ini settings. Note that &#8216;php://input&#8217; does not work with <em>enctype=&#8221;multipart/form-data&#8221;</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/reading-raw-post-data-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Google Pagerank in PHP</title>
		<link>http://www.codediesel.com/php/google-pagerank-in-php/</link>
		<comments>http://www.codediesel.com/php/google-pagerank-in-php/#comments</comments>
		<pubDate>Sat, 19 Apr 2008 08:59:08 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=16</guid>
		<description><![CDATA[Due to recent changes made by Google in its Page Rank processing the following library doesn&#8217;t work. However you can try to use the following api made available by fusionswift.com: http://fusionswift.com/201004/google-pagerank-script-in-php/ Mark Woodman has created a nifty class which lets you query Google pagerank info in PHP 5 and above. The complete classes are available [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>
Due to recent changes made by Google in its Page Rank processing the following library doesn&#8217;t work. However you can try to use the following api made available by fusionswift.com:<br />
<a target="_blank" rel="nofollow" href="http://fusionswift.com/201004/google-pagerank-script-in-php/">http://fusionswift.com/201004/google-pagerank-script-in-php/</a>
</p></blockquote>
<p>Mark Woodman has created a nifty class which lets you query Google pagerank info in PHP 5 and above. The complete classes are available <a title="popStats" href="http://code.google.com/p/popstats/" target="_blank">here</a>.</p>
<p>The minimum classes required are &#8216;<a rel="nofollow" href="http://popstats.googlecode.com/svn/trunk/cacher.class.php">cacher.class.php</a>&#8216; and &#8216;<a rel="nofollow" href="http://popstats.googlecode.com/svn/trunk/google_pagerank.class.php">google_pagerank.class.php</a>&#8216;.</p>
<p>Sample code is shown below.<span id="more-16"></span></p>

<div class="wp_codebox"><table><tr id="p1651"><td class="code" id="p16code51"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;popstats/google_pagerank.class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$rankObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GooglePageRank<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.someDomain.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pageRank</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rankObject</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pagerank</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pageRank</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The class also supports result caching on the local machine, so if a second query arrives within the cache time limit the result saved in the local file is returned rather then going out and querying Google.</p>
<p>The &#8216;GooglePageRank&#8217; class accepts cache time as a second parameter. So if you want to cache the result for 6 hours you set the second parameter for 21600 seconds (6 * 60 * 60):</p>

<div class="wp_codebox"><table><tr id="p1652"><td class="code" id="p16code52"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$rankObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GooglePageRank<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.someDomain.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">21600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The default is set to 24 hours, which is more then adequate for most purposes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/google-pagerank-in-php/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

