<?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>SayNoToFlash &#187; Snippets</title>
	<atom:link href="http://www.saynotoflash.com/archives/category/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.saynotoflash.com</link>
	<description>PHP, Symfony, Web Development</description>
	<lastBuildDate>Tue, 17 Jan 2012 13:27:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Symfony Parallel Content Delivery &#8211; UPDATE</title>
		<link>http://www.saynotoflash.com/archives/symfony-parallel-content-delivery/</link>
		<comments>http://www.saynotoflash.com/archives/symfony-parallel-content-delivery/#comments</comments>
		<pubDate>Thu, 19 May 2011 17:30:39 +0000</pubDate>
		<dc:creator>jestep</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.saynotoflash.com/?p=477</guid>
		<description><![CDATA[UPDATED &#8211; 05-19-2011 I updated the script to make sure that it serves the same image from the same domain on each request. It&#8217;s counter productive to send the same image from different url&#8217;s as this increases the download time. This fix does not hash or internally check the image to see if it is [...]]]></description>
			<content:encoded><![CDATA[<p>UPDATED &#8211; 05-19-2011</p>
<p>I updated the script to make sure that it serves the same image from the same domain on each request. It&#8217;s counter productive to send the same image from different url&#8217;s as this increases the download time. This fix does not hash or internally check the image to see if it is identical to another image with a different name. This also does not store anything in a file or database, so it&#8217;s possible that the image will be served from a different url on a different page / request.</p>
<p>This is a drop in replacement for the original script. The interface for cdn_image_url and cdn_image_tag methods did not change.</p>
<p>Original Information</p>
<p>In order to reduce page latency, I&#8217;ve been implementing a parallel content delivery network on a large symfony project. This code should work on symfony and most any other php based site. What it does is allow a developer to specify a number of subdomains / domains for content delivery, such as content1.mydomain, content2.mydomain, etc&#8230; When serving images in this manner, one can greatly reduce page loading time by using multiple domains for images even if the domains are on the same server.</p>
<p>This script uses a singleton hack to keep a cursor of the current domain that the image is being served from. It then delivers the image in sequential order from the list of domains that the developer has pre-specified. A developer can specify as many domains as they want, and if the page is being served over a secure connection, the script will automatically include secure versions of the images. <em>NOTE: make sure the domains have ssl installed correctly or errors will occur.</em></p>
<p>Add this script to a file in /myproject/lib/.</p>

<div class="wp_syntax"><div class="code"><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;">class</span> Cdn <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$current_cdn</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$is_secure</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$prefix_match</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;">private</span> static <span style="color: #000088;">$instance</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$domain</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'mydomain.com'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$cdn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'www.'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cd1.'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cd2.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <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>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current_cdn</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_PORT'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'443'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_secure</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'https://'</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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> cdn_image_url<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_path</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$cdn</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_cdn</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_path</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;">is_secure</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cdn</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$domain</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$image_path</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> cdn_image_tag<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_path</span><span style="color: #339933;">,</span> <span style="color: #000088;">$option_array</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>
&nbsp;
        <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$cdn</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_cdn</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_path</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: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_array</span><span style="color: #009900;">&#41;</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;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$attribute</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$options</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$attribute</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; '</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: #0000ff;">'&lt;img src=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_secure</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cdn</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$domain</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$image_path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' /&gt;'</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> get_cdn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_path</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cdn</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current_cdn</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current_cdn</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_path</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prefix_match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #000088;">$cdn</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prefix_match</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$image_path</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>
&nbsp;
            <span style="color: #000088;">$cdn</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$cdn</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current_cdn</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prefix_match</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$image_path</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cdn</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current_cdn</span><span style="color: #339933;">++;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$cdn</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<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: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instance</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cdn<span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instance</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then you can call your images using 1 of 2 methods throughout the project and they will be delivered sequentially through all of the domains in your CDN.</p>
<p>If you would just like the image url:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #000088;">$my_url</span> <span style="color: #339933;">=</span> Cdn<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cdn_image_url</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/my_image.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you want the entire tag:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #b1b100;">echo</span> Cdn<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cdn_image_tag</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/swiped-box-b.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can also add attributes to the tag:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #b1b100;">echo</span> Cdn<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cdn_image_tag</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/swiped-box-b.jpg'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'My Image'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If your images are hosted on completely different domains, simply edit the $cdn property to include the full domain, and take out the self::$domain property from the appropriate method: cdn_image_url or cdn_image_tag.</p>
<p>This is a very basic class for delivering images from different domains. It does not take things into account like serving the same image from the same domain every time to take advantage of caching, or checking to see if the image actually exists. However, it should provide a basis for a more complete content delivery script.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saynotoflash.com/archives/symfony-parallel-content-delivery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Google chart over HTTPS/SSL</title>
		<link>http://www.saynotoflash.com/archives/google-chart-over-httpsssl/</link>
		<comments>http://www.saynotoflash.com/archives/google-chart-over-httpsssl/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 16:17:14 +0000</pubDate>
		<dc:creator>jestep</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.saynotoflash.com/?p=403</guid>
		<description><![CDATA[The google charts API does not support the https protocol. If your website is being delivered through a secure connection, the chart will cause a SSL error. Here&#8217;s a quick way to deliver google chart images over ssl. To start off with, the chart image must be delivered from a secure connection. Google doesn&#8217;t allow [...]]]></description>
			<content:encoded><![CDATA[<p>The google charts API does not support the https protocol. If your website is being delivered through a secure connection, the chart will cause a SSL error. Here&#8217;s a quick way to deliver google chart images over ssl.</p>
<p>To start off with, the chart image must be delivered from a secure connection. Google doesn&#8217;t allow this plain and simple, so we need to figure out how to host it from our own site. We accomplish this by fetching the image from google using the standard API, writing it to a file, and then calling it on our own script. We basically make a image handling proxy.</p>
<p>Let&#8217;s take a simple google chart to experiment with.</p>
<p><img src="http://chart.apis.google.com/chart?chs=500x50&#038;chf=bg,s,ffffff&#038;cht=ls&#038;chd=t:23.52,20.58,26.47,23.52,23.52,23.52,100.00,0.00,23.52,23.52,27.94,20.58,23.52&#038;chco=0066ff" alt="" /></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$chart_image</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://chart.apis.google.com/chart?chs=500x50&amp;chf=bg,s,ffffff&amp;cht=ls&amp;chd=t:23.52,20.58,26.47,23.52,23.52,23.52,100.00,0.00,23.52,23.52,27.94,20.58,23.52&amp;chco=0066ff'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next we need to make a function to fetch and save the google chart locally. It will check the chart against the local copy and save it if the chart doesn&#8217;t exist, or the image has changed. This way we aren&#8217;t re-writing the same chart on every request, but if the chart changes, it will be updated appropriately.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> saveImage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart_url</span><span style="color: #339933;">,</span><span style="color: #000088;">$path</span><span style="color: #339933;">,</span><span style="color: #000088;">$file_name</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: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">md5_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #990000;">md5_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">,</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$file_name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Lastly we tie it all together so that it is usable in our application. Im using this within a class, but this could just be used as a function as well. Your image directory will need to be writable for this to work.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000088;">$local_image_path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/path/to/images/charts/'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'some_chart_image.png'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$chart_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://chart.apis.google.com/chart?chs=500x50&amp;chf=bg,s,ffffff&amp;cht=ls&amp;chd=t:23.52,20.58,26.47,23.52,23.52,23.52,100.00,0.00,23.52,23.52,27.94,20.58,23.52&amp;chco=0066ff'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">saveImage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart_url</span> <span style="color: #339933;">,</span><span style="color: #000088;">$local_image_path</span><span style="color: #339933;">,</span><span style="color: #000088;">$image_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You&#8217;ll need to implement your own error handling, and adjust this to meet the paths and specifics of your server, but the image can now be called from:<br />
&lt;img src=&quot;/images/charts/some_chart_image.png&quot; alt=&quot;&quot; /&gt;</p>
<p>If you need help creating your base chart image, <a href="http://www.clabberhead.com/googlechartgenerator.html">this tool is a great place to start</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saynotoflash.com/archives/google-chart-over-httpsssl/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>US States Snippet and SQL Dump</title>
		<link>http://www.saynotoflash.com/archives/us-states-snippet-and-sql-dump/</link>
		<comments>http://www.saynotoflash.com/archives/us-states-snippet-and-sql-dump/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 18:24:28 +0000</pubDate>
		<dc:creator>jestep</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.saynotoflash.com/?p=261</guid>
		<description><![CDATA[Here&#8217;s some US states snippets. Included are php arrays, and a MySQL states dump&#8230; PHP Snippets: //states and abbreviations snippets $states = array&#40;'Alaska','Alabama','Arkansas','Arizona','California','Colorado','Connecticut','Delaware','Florida','Georgia','Hawaii','Iowa','Idaho','Illinois','Indiana','Kansas','Kentucky','Louisiana','Massachusetts','Maryland','Maine','Michigan','Minnesota','Missouri','Mississippi','Montana','North Carolina','North Dakota','Nebraska','New Hampshire','New Jersey','New Mexico','Nevada','New York','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Virginia','Vermont','Washington','Wisconsin','West Virginia','Wyoming'&#41;; &#160; $abbreviations = array&#40;'AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY'&#41;; &#160; $states_assoc = array&#40; 'AL'=&#62;&#34;Alabama&#34;, 'AK'=&#62;&#34;Alaska&#34;, 'AZ'=&#62;&#34;Arizona&#34;, 'AR'=&#62;&#34;Arkansas&#34;, 'CA'=&#62;&#34;California&#34;, 'CO'=&#62;&#34;Colorado&#34;, 'CT'=&#62;&#34;Connecticut&#34;, 'DE'=&#62;&#34;Delaware&#34;, 'DC'=&#62;&#34;District Of Columbia&#34;, 'FL'=&#62;&#34;Florida&#34;, 'GA'=&#62;&#34;Georgia&#34;, 'HI'=&#62;&#34;Hawaii&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s some US states snippets. Included are php arrays, and a MySQL states dump&#8230;</p>
<p><span id="more-261"></span><strong>PHP Snippets:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//states and abbreviations snippets</span>
<span style="color: #000088;">$states</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Alaska'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Alabama'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Arkansas'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Arizona'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'California'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Colorado'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Connecticut'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Delaware'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Florida'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Georgia'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Hawaii'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Iowa'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Idaho'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Illinois'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Indiana'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Kansas'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Kentucky'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Louisiana'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Massachusetts'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Maryland'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Maine'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Michigan'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Minnesota'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Missouri'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Mississippi'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Montana'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'North Carolina'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'North Dakota'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Nebraska'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'New Hampshire'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'New Jersey'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'New Mexico'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Nevada'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'New York'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Ohio'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Oklahoma'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Oregon'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Pennsylvania'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Rhode Island'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'South Carolina'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'South Dakota'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Tennessee'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Texas'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Utah'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Virginia'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Vermont'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Washington'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Wisconsin'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'West Virginia'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Wyoming'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$abbreviations</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AK'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'AL'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'AR'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'AZ'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'CA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'CO'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'CT'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'DE'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'FL'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'GA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'HI'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'IA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ID'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'IL'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'IN'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'KS'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'KY'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'LA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'MA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'MD'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ME'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'MI'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'MN'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'MO'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'MS'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'MT'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'NC'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ND'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'NE'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'NH'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'NJ'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'NM'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'NV'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'NY'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'OH'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'OK'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'OR'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'PA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'RI'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'SC'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'SD'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'TN'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'TX'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'UT'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'VA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'VT'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'WA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'WI'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'WV'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'WY'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$states_assoc</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'AL'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Alabama&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'AK'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Alaska&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'AZ'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Arizona&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'AR'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Arkansas&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'CA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;California&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'CO'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Colorado&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'CT'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Connecticut&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'DE'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Delaware&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'DC'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;District Of Columbia&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'FL'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Florida&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'GA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Georgia&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'HI'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Hawaii&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'ID'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Idaho&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'IL'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Illinois&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'IN'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Indiana&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'IA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Iowa&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'KS'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Kansas&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'KY'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Kentucky&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'LA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Louisiana&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'ME'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Maine&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'MD'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Maryland&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'MA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Massachusetts&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'MI'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Michigan&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'MN'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Minnesota&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'MS'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Mississippi&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'MO'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Missouri&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'MT'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Montana&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'NE'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Nebraska&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'NV'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Nevada&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'NH'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;New Hampshire&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'NJ'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;New Jersey&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'NM'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;New Mexico&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'NY'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;New York&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'NC'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;North Carolina&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'ND'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;North Dakota&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'OH'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Ohio&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'OK'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Oklahoma&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'OR'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Oregon&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'PA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Pennsylvania&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'RI'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Rhode Island&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'SC'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;South Carolina&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'SD'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;South Dakota&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'TN'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Tennessee&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'TX'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Texas&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'UT'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Utah&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'VT'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Vermont&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'VA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Virginia&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'WA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Washington&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'WV'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;West Virginia&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'WI'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Wisconsin&quot;</span><span style="color: #339933;">,</span>  
<span style="color: #0000ff;">'WY'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Wyoming&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>MySQL Snippets:</strong><br />
[sql]<br />
&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
&#8211; Table structure for states<br />
&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
CREATE TABLE `states` (<br />
  `state_id` tinyint(3) unsigned NOT NULL,<br />
  `state_name` varchar(22) NOT NULL,<br />
  `state_abbreviation` char(2) NOT NULL,<br />
  PRIMARY KEY  (`state_id`),<br />
  UNIQUE KEY `state_abbreviation` (`state_abbreviation`)<br />
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;</p>
<p>&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
&#8211; Records<br />
&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
INSERT INTO `states` VALUES (&#8217;1&#8242;, &#8216;Alabama&#8217;, &#8216;AL&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;2&#8242;, &#8216;Alaska&#8217;, &#8216;AK&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;3&#8242;, &#8216;Arizona&#8217;, &#8216;AZ&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;4&#8242;, &#8216;Arkansas&#8217;, &#8216;AR&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;5&#8242;, &#8216;California&#8217;, &#8216;CA&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;6&#8242;, &#8216;Colorado&#8217;, &#8216;CO&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;7&#8242;, &#8216;Connecticut&#8217;, &#8216;CT&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;8&#8242;, &#8216;Delaware&#8217;, &#8216;DE&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;9&#8242;, &#8216;District of Columbia&#8217;, &#8216;DC&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;10&#8242;, &#8216;Florida&#8217;, &#8216;FL&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;11&#8242;, &#8216;Georgia&#8217;, &#8216;GA&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;12&#8242;, &#8216;Hawaii&#8217;, &#8216;HI&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;13&#8242;, &#8216;Idaho&#8217;, &#8216;ID&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;14&#8242;, &#8216;Illinois&#8217;, &#8216;IL&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;15&#8242;, &#8216;Indiana&#8217;, &#8216;IN&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;16&#8242;, &#8216;Iowa&#8217;, &#8216;IA&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;17&#8242;, &#8216;Kansas&#8217;, &#8216;KS&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;18&#8242;, &#8216;Kentucky&#8217;, &#8216;KY&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;19&#8242;, &#8216;Louisiana&#8217;, &#8216;LA&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;20&#8242;, &#8216;Maine&#8217;, &#8216;ME&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;21&#8242;, &#8216;Maryland&#8217;, &#8216;MD&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;22&#8242;, &#8216;Massachusetts&#8217;, &#8216;MA&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;23&#8242;, &#8216;Michigan&#8217;, &#8216;MI&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;24&#8242;, &#8216;Minnesota&#8217;, &#8216;MN&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;25&#8242;, &#8216;Mississippi&#8217;, &#8216;MS&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;26&#8242;, &#8216;Missouri&#8217;, &#8216;MO&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;27&#8242;, &#8216;Montana&#8217;, &#8216;MT&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;28&#8242;, &#8216;Nebraska&#8217;, &#8216;NE&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;29&#8242;, &#8216;Nevada&#8217;, &#8216;NV&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;30&#8242;, &#8216;New Hampshire&#8217;, &#8216;NH&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;31&#8242;, &#8216;New Jersey&#8217;, &#8216;NJ&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;32&#8242;, &#8216;New Mexico&#8217;, &#8216;NM&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;33&#8242;, &#8216;New York&#8217;, &#8216;NY&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;34&#8242;, &#8216;North Carolina&#8217;, &#8216;NC&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;35&#8242;, &#8216;North Dakota&#8217;, &#8216;ND&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;36&#8242;, &#8216;Ohio&#8217;, &#8216;OH&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;37&#8242;, &#8216;Oklahoma&#8217;, &#8216;OK&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;38&#8242;, &#8216;Oregon&#8217;, &#8216;OR&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;39&#8242;, &#8216;Pennsylvania&#8217;, &#8216;PA&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;40&#8242;, &#8216;Rhode Island&#8217;, &#8216;RI&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;41&#8242;, &#8216;South Carolina&#8217;, &#8216;SC&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;42&#8242;, &#8216;South Dakota&#8217;, &#8216;SD&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;43&#8242;, &#8216;Tennessee&#8217;, &#8216;TN&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;44&#8242;, &#8216;Texas&#8217;, &#8216;TX&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;45&#8242;, &#8216;Utah&#8217;, &#8216;UT&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;46&#8242;, &#8216;Vermont&#8217;, &#8216;VT&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;47&#8242;, &#8216;Virginia&#8217;, &#8216;VA&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;48&#8242;, &#8216;Washington&#8217;, &#8216;WA&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;49&#8242;, &#8216;West Virginia&#8217;, &#8216;WV&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;50&#8242;, &#8216;Wisconsin&#8217;, &#8216;WI&#8217;);<br />
INSERT INTO `states` VALUES (&#8217;51&#8242;, &#8216;Wyoming&#8217;, &#8216;WY&#8217;);<br />
[/sql]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saynotoflash.com/archives/us-states-snippet-and-sql-dump/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Script benchmark / bottleneck debugging snippet</title>
		<link>http://www.saynotoflash.com/archives/php-script-benchmark-bottleneck-debugging-snippet/</link>
		<comments>http://www.saynotoflash.com/archives/php-script-benchmark-bottleneck-debugging-snippet/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 13:00:59 +0000</pubDate>
		<dc:creator>jestep</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.saynotoflash.com/?p=244</guid>
		<description><![CDATA[Here&#8217;s a really simple function that I use for finding bottlenecks in php scripts. You can add any number of steps to the the script using the microtime() function, and this function shows the execution time of each step. /** * Benchmark a php script * * @param array $time_sample * @return string HTML */ [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a really simple function that I use for finding bottlenecks in php scripts. You can add any number of steps to the the script using the microtime() function, and this function shows the execution time of each step.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Benchmark a php script
 *
 * @param array $time_sample
 * @return string HTML
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> quick_benchmark<span style="color: #009900;">&#40;</span><span style="color: #000088;">$time_sample</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;">$steps</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$steps</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</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;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$steps</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p&gt;Time '</span><span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</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;">number_format</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_sample</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: #cc66cc;">6</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</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: #0000ff;">' seconds.&lt;/p&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p&gt;Total time: '</span><span style="color: #339933;">.</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$steps</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</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: #0000ff;">' seconds.&lt;/p&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a simple example using sleep() to demonstrate the output.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//start</span>
<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//time 1</span>
<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//time 2</span>
<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//time 3</span>
<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//time 4</span>
&nbsp;
<span style="color: #b1b100;">echo</span> quick_benchmark<span style="color: #009900;">&#40;</span><span style="color: #000088;">$time_sample</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>The script outputs:</strong></p>
<p>Time 1: 1.001833 seconds.<br />
Time 2: 2.001427 seconds.<br />
Time 3: 3.001124 seconds.<br />
Time 4: 1.001720 seconds.<br />
Total time: 7.006104 seconds.</p>
<p>It&#8217;s a good idea to comment each time you record a microtime so that you know which section of script took that amount of time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saynotoflash.com/archives/php-script-benchmark-bottleneck-debugging-snippet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Random string generator snippet</title>
		<link>http://www.saynotoflash.com/archives/php-random-string-generator-snippet/</link>
		<comments>http://www.saynotoflash.com/archives/php-random-string-generator-snippet/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 15:42:11 +0000</pubDate>
		<dc:creator>jestep</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.saynotoflash.com/?p=231</guid>
		<description><![CDATA[This is a little function that I use all the time to generate random strings. There are 3 options for random strings with this: Alpha, Alpha-numeric, and Alpha-numeric with symbols. This is important because sometimes it&#8217;s a good idea not to allow special characters in a php string. However, the special characters are great if [...]]]></description>
			<content:encoded><![CDATA[<p>This is a little function that I use all the time to generate random strings. There are 3 options for random strings with this: Alpha, Alpha-numeric, and Alpha-numeric with symbols. This is important because sometimes it&#8217;s a good idea not to allow special characters in a php string. However, the special characters are great if you need to create a key or initialization vector for <a href="http://www.saynotoflash.com/archives/php-aes-encryption/">2 way encryption</a>.</p>
<p>This can be used to generate random passwords or keys or just about anything else that needs a random string. You can also throw this directly into a class and use it as a static method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Generate a random string
 *
 * @param int $length
 * @param int $mode 1 = Alpha, 2 = Alpha-numeric, 3 = Alpha-numeric with symbols
 * @param boolian $char_set Set true for Upper and Lower case letters
 * @return string 
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> random_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><span style="color: #000088;">$mode</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$char_set</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: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$possible</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$char_set</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$possible</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$possible</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
&nbsp;
			<span style="color: #000088;">$possible</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'`~!@#$%^&amp;*()_-+=|}]{[&quot;:;&lt;,&gt;.?/'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
&nbsp;
			<span style="color: #000088;">$possible</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'0123456789'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$length</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$char</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$possible</span><span style="color: #339933;">,</span> <span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$possible</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$char</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Examples:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> random_string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//WQTISVJVMWSEFXEIQISJPCBENFEHQAN</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> random_string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</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: #666666; font-style: italic;">//cZhVGHJb0PqJIk3</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> random_string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//=,:UT__GN[ST&gt;GH</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.saynotoflash.com/archives/php-random-string-generator-snippet/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Querying with Propel Criteria using &#8220;NOT IN&#8221; (criteria::not_in)</title>
		<link>http://www.saynotoflash.com/archives/querying-with-propel-criteria-using-not-in/</link>
		<comments>http://www.saynotoflash.com/archives/querying-with-propel-criteria-using-not-in/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 19:20:22 +0000</pubDate>
		<dc:creator>jestep</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.saynotoflash.com/?p=26</guid>
		<description><![CDATA[It&#8217;s fairly common to use &#8220;NOT IN&#8221; database queries in web development. Symfony when using Propel does not have native support for using NOT IN queries in reference to another tables. You can use NOT IN and an array, but cannot use NOT IN with reference to another table. Here&#8217;s how to use NOT IN [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s fairly common to use &#8220;NOT IN&#8221; database queries in web development.</p>
<p>Symfony when using Propel does not have native support for using NOT IN queries in reference to another tables. You can use NOT IN and an array, but cannot use NOT IN with reference to another table.</p>
<p>Here&#8217;s how to use NOT IN other_table with Propel and Symfony. Let&#8217;s pretend we need to run this query.</p>
<p><code>SELECT * FROM my_table_1 WHERE id NOT IN (<br />
SELECT id FROM my_table_2);</code></p>
<p>Using Propel and Criteria, there is no native way of running this query. But, with the Criteria::CUSTOM modifier, we can force this query through propel.</p>
<p>It&#8217;s actually very easy.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Criteria<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Example query
$not_in_query = 'my_table_1.id NOT IN (
SELECT id
FROM my_table_2)';
*/</span>
&nbsp;
<span style="color: #000088;">$not_in_query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'%s NOT IN (
SELECT %s
FROM %s)'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$not_in_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span>
<span style="color: #000088;">$not_in_query</span><span style="color: #339933;">,</span>
MyTable1Peer<span style="color: #339933;">::</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span>
MyTable2Peer<span style="color: #339933;">::</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span>
MyTable2Peer<span style="color: #339933;">::</span><span style="color: #004000;">TABLE_NAME</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span>MyTable1Peer<span style="color: #339933;">::</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #000088;">$not_in_query</span><span style="color: #339933;">,</span> Criteria<span style="color: #339933;">::</span><span style="color: #004000;">CUSTOM</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> MyTable1Peer<span style="color: #339933;">::</span><span style="color: #004000;">doSelect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can add whatever other Criteria to the query as well. So far this is the only reliable way to run these sort of queries that I&#8217;ve found.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saynotoflash.com/archives/querying-with-propel-criteria-using-not-in/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PHP Tutorial &#8211; Forking using wget or php cli in the background</title>
		<link>http://www.saynotoflash.com/archives/php-tutorial-forking-using-wget-in-the-background/</link>
		<comments>http://www.saynotoflash.com/archives/php-tutorial-forking-using-wget-in-the-background/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 16:31:57 +0000</pubDate>
		<dc:creator>jestep</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.saynotoflash.com/?p=13</guid>
		<description><![CDATA[I see a lot of people asking on how to use a fork to run a php script or web page in the background. Here&#8217;s a quick script I created using linux&#8217;s wget to run another process in the background. You could use this to automatically upload something to another server, or just about anything [...]]]></description>
			<content:encoded><![CDATA[<p>I see a lot of people asking on how to use a fork to run a php script or web page in the background.</p>
<p>Here&#8217;s a quick script I created using linux&#8217;s wget to run another process in the background. You could use this to automatically upload something to another server, or just about anything that needs to happen after a user action.</p>
<p><strong>Note:</strong> you must have <a href="http://us2.php.net/function.exec">exec</a> enabled for this to work. This is a security risk, so make sure you know the potential damage that can be done by enabling exec, and by the script that you are running. Make absolutely sure to sanitize any input being transferred to the executed script.</p>
<p>In my case, I needed to initiate a file upload to a remote server after a certain action was taken. Because of the nature of the upload, it would have taken about 30 seconds before displaying any response to my user, which is an unacceptable delay.</p>
<p><strong>First, make the script that needs to run in the background.</strong></p>
<p><span id="more-13"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//ALWAYS USE THIS FOR UNATTENDED SCRIPTS, YOU DON'T WANT THEM RUNNING FOREVER</span>
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
SCRIPT THAT DOES SOMETHING YOU WANT TO RUN IN THE BACKGROUND
THIS SCRIPT CAN DO ANYTHING. IT IS A NORMAL PHP SCRIPT
HOWEVER, DO NOT MAKE A SCRIPT THAT WILL OUTPUT TO THE BROWSER
AS NOBODY WILL EVER SEE IT
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//YOU CAN ALSO USE sleep($seconds) TO RUN THIS AFTER A SPECIFIED AMOUNT OF TIME</span>
<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SOME DATA THAT I GENERATED'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ftp_user_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'user'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ftp_user_pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'pass'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ftp_server</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ftp.someplace.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ftp_dir</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'/this_folder/'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$destination_file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// connect, login, and transfer the file</span>
<span style="color: #000088;">$conn_id</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ftp_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ftp_server</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$login_result</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ftp_login</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ftp_user_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ftp_user_pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #339933;">@</span><span style="color: #990000;">ftp_put</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$destination_file</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.csv'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Next, make the script that executes the background script.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//DO A BUNCH OF STUFF HERE...</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//LOCATION OF THE PAGE YOU WANT TO EXECUTE IN THE BACKGROUND</span>
<span style="color: #000088;">$page_to_execute</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.mysite.com/background_script.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
COMMAND TO EXECUTE THE PAGE USING WGET
YOU CAN SEND GET VARIABLES USING THIS
BE ABSOLUTELY SURE TO PROTECT AGAINST INJECTION
*/</span>
<span style="color: #000088;">$command</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'wget -q '</span><span style="color: #339933;">.</span><span style="color: #000088;">$page_to_execute</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//ALTERNATELY TO WGET YOU COULD USE SOMETHING LIKE</span>
<span style="color: #666666; font-style: italic;">//$command = &quot;nohup /usr/local/bin/php -f &quot;.$page_to_execute;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
NOW EXECUTE THE PAGE IN THE BACKGROUND
THIS WILL NOT RETURN ANY ERROR IF IT FAILS
YOU WILL NEED TO WATCH YOUR SERVER LOGS 
TO PROPERLY DEBUG
*/</span>
&nbsp;
<span style="color: #339933;">@</span><span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$command</span> &gt; /dev/null &amp;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arrOutput</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There you have it. This will run a script in the background (any web page that you specify). I do not use a local path to the file, because it will often break and can pose additional security risks. Using the above code, the server can only execute wget, and the actual page is parsed just like any other page on your website.</p>
<p>Again, make sure you understand the potential problems enabling exec can cause. There are also other ways of doing this such as running php from the exec command (path/to/php my_script). These can pose much greater security risks if they are not properly coded.</p>
<p>Lastly, this should be run on user or administrator initiated event, and not on events such as page views. This could spawn a huge number of processes if it is ever allowed to run based on every page view or something similar.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saynotoflash.com/archives/php-tutorial-forking-using-wget-in-the-background/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

