<?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>stuntSnippets &#187; PHP</title>
	<atom:link href="http://stuntsnippets.com/snippets/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://stuntsnippets.com</link>
	<description></description>
	<lastBuildDate>Fri, 09 Dec 2011 14:54:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Validate Select Field in Zend Form</title>
		<link>http://stuntsnippets.com/validate-select-field-in-zend-form/</link>
		<comments>http://stuntsnippets.com/validate-select-field-in-zend-form/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 14:54:16 +0000</pubDate>
		<dc:creator>Darjan Vukusic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Select]]></category>
		<category><![CDATA[Validate Select Field in Zend Form]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Form]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend Select]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1146</guid>
		<description><![CDATA[When setting your Zend Form with Select field you don&#8217;t always wish to have label on that filed but rather “Select one” as a first option. Setting the required flag on Zend Select, or adding notEmpty validator won&#8217;t always work especially if your box is being populated dynamically. Let&#8217;s say you have something like this: [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fvalidate-select-field-in-zend-form%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fvalidate-select-field-in-zend-form%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>When setting your Zend Form with Select field you don&#8217;t always wish to have label on that filed but rather “Select one” as a first option.</p>
<p>Setting the required flag on Zend Select, or adding notEmpty validator won&#8217;t always work especially if your box is being populated dynamically.</p>
<p>Let&#8217;s say you have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;select name=&quot;example&quot;&gt;
    &lt;option value=&quot;Select one&quot;&gt;Select one&lt;/option&gt;
    &lt;option value=&quot;value1&quot;&gt;value1&lt;/option&gt;
    &lt;option value=&quot;value2&quot;&gt;value2&lt;/option&gt;
    &lt;option value=&quot;value3&quot;&gt;value3&lt;/option&gt;
&lt;/select&gt;</pre></div></div>

<p>You could easily validate this by adding Zend_Validate_Regex and passing any pattern to it<br />
as follows in snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_Regex<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/[^Select\sone]/'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addErrorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You need to select one value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This one will check if the value is not &#8220;Select one&#8221; and let your user pass validation only in that case.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/validate-select-field-in-zend-form/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/validate-select-field-in-zend-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP fluent interface</title>
		<link>http://stuntsnippets.com/php-fluent-interface/</link>
		<comments>http://stuntsnippets.com/php-fluent-interface/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 10:01:02 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[fluent interface]]></category>
		<category><![CDATA[PHP fluent interface]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1134</guid>
		<description><![CDATA[PHP fluent interfaces have been around for quite some time. They are often used just for chaining methods of an object so that we get nice readable code. Good fluent interface takes a while to build, and will take some planning to make object methods work seamlessly. However, if you still haven&#8217;t experienced the PHP [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-fluent-interface%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-fluent-interface%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>PHP fluent interfaces have been around for quite some time. They are often used just for chaining methods of an object so that we get nice readable code.</p>
<p>Good fluent interface takes a while to build, and will take some planning to make object methods work seamlessly. However, if you still haven&#8217;t experienced the <strong>PHP fluent interface</strong>, there is a good way to start, just check out the following snippet.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Php_Fluent_Interface_Demo <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_first</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_second</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_third</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setFirst<span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstVal</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>_first <span style="color: #339933;">=</span> <span style="color: #000088;">$firstVal</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</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> setSecond<span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondVal</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>_second <span style="color: #339933;">=</span> <span style="color: #000088;">$secondVal</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</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> setThird<span style="color: #009900;">&#40;</span><span style="color: #000088;">$thirdVal</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>_third <span style="color: #339933;">=</span> <span style="color: #000088;">$thirdVal</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You have noticed that all setters have one line in common <strong>return $this;</strong> and this is how you set your methods to be able to chain. Example of usage follows in next snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$phpFluentInterface</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Php_Fluent_Interface_Demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$phpFluentInterface</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFirst</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSecond</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setThird</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I hope that this simple example is enough to get you started if you still haven&#8217;t tried PHP fluent interface. Good luck!</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/php-fluent-interface/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/php-fluent-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework CSRF protection</title>
		<link>http://stuntsnippets.com/zend-framework-csrf-protection/</link>
		<comments>http://stuntsnippets.com/zend-framework-csrf-protection/#comments</comments>
		<pubDate>Sat, 01 Oct 2011 17:23:20 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CSRF]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1126</guid>
		<description><![CDATA[CSRF or Cross-Site Request Forgery is basically a way of requesting an unauthorized commands from a website by using an authorised user. For example: Let&#8217;s assume that you are logged in on your blog and I know that. I could then send you an e-mail with following content: &#60;img src=&#8221;http://yoursite.com/?action=delete-article&#038;id=12&#8243; /&#62;. Although you wouldn&#8217;t see [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fzend-framework-csrf-protection%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fzend-framework-csrf-protection%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>CSRF or Cross-Site Request Forgery is basically a way of requesting an unauthorized commands from a website by using an authorised user.</p>
<p>For example: Let&#8217;s assume that you are logged in on your blog and I know that. I could then send you an e-mail with following content: &lt;img src=&#8221;http://yoursite.com/?action=delete-article&#038;id=12&#8243; /&gt;.</p>
<p>Although you wouldn&#8217;t see the image &#8211; request for deleting article with id 12 would be sent. This can be prevented by setting up a hidden input in your Zend Framework form with a value that will be posted with your request and validated when request is sent in order to prevent possible CSRF attack.</p>
<p>It is quite easy in Zend Framework to set CSRF protection and here is the snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hash'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'csrf_token'</span><span style="color: #339933;">,</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'salt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'stunt@c0d3rs~!'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In order to see a full example of <strong>Zend Framework CSRF protection</strong> I have prepared an example of simple form with no elements on it.</p>
<h3>Zend Framework CSRF protection</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Stunt_Form <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hash'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'csrf_token'</span><span style="color: #339933;">,</span>
                    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'salt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'stunt@c0d3rs~!'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/zend-framework-csrf-protection/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/zend-framework-csrf-protection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Analytics results on your website</title>
		<link>http://stuntsnippets.com/google-analytics-results-on-your-website/</link>
		<comments>http://stuntsnippets.com/google-analytics-results-on-your-website/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 15:29:58 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1089</guid>
		<description><![CDATA[Have you ever wondered if you could let user see his website visits on his own admin page without having to leave backend of his system? I have&#8230; Actually &#8211; the CMS I made for my clients (written in Zend Framework) has this option for quite some time, and users love it. So &#8211; in [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fgoogle-analytics-results-on-your-website%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fgoogle-analytics-results-on-your-website%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Have you ever wondered if you could let user see his website visits on his own admin page without having to leave backend of his system? I have&#8230;</p>
<p>Actually &#8211; the CMS I made for my clients (written in Zend Framework) has this option for quite some time, and users love it.</p>
<p>So &#8211; in order to implement this function on your own website (written in Zend Framework) I have shared following code. First &#8211; copy model:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Default_Model_Analytics
<span style="color: #009900;">&#123;</span> 
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> auth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;your_email@domain.com&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$passwd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;your_password&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		try <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> Zend_Gdata_ClientLogin<span style="color: #339933;">::</span><span style="color: #004000;">getHttpClient</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$passwd</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;analytics&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>catch <span style="color: #009900;">&#40;</span>Zend_Gdata_App_CaptchaRequiredException <span style="color: #000088;">$cre</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'URL of CAPTCHA image: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cre</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCaptchaUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Token ID: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cre</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCaptchaToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Gdata_App_AuthException <span style="color: #000088;">$ae</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Problem authenticating: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ae</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exception</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Gdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$client</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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$gdClient</span><span style="color: #339933;">,</span> <span style="color: #000088;">$startDate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2010-07-01'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$endDate</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ga:region&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ga:city&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$metrics</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ga:visits&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ga:pageviews&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gdClient</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$endDate</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$endDate</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;
		try <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$reportURL</span>	<span style="color: #339933;">=</span>	<span style="color: #0000ff;">&quot;https://www.google.com/analytics/feeds/data?ids=ga:&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$yourAccountNumber</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;dimensions=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">@</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;metrics=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">@</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$metrics</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;start-date=<span style="color: #006699; font-weight: bold;">{$startDate}</span>&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;end-date=<span style="color: #006699; font-weight: bold;">{$endDate}</span>&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;sort=-ga:visits&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$gdClient</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFeed</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$reportURL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$titleRow</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// To output a row of column labels</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;table&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$rep</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;">$titleRow</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;">$rep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extensionElements</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$elem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #000088;">$titles</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$elem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extensionAttributes</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&lt;td&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$titles</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/td&gt;&lt;/tr&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$titleRow</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000088;">$row</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: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extensionElements</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$elem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$elem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extensionAttributes</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;tr&gt;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&lt;td&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/td&gt;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;/tr&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/table&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Caught exception: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Message: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</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: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Then &#8211; run following code in your controller so user can see Analytics results:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$analyticsModel</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Default_Model_Analytics<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//get: date start and date end of Analytics results</span>
<span style="color: #000088;">$postParams</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParams</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</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;">$gdata</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$analyticsModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">auth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$analyticsModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gdata</span><span style="color: #339933;">,</span> <span style="color: #000088;">$postParams</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'year1'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$postParams</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'month1'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-01'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$postParams</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'year2'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$postParams</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'month2'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-31'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><em>Notice:</em> This is not the proper way to write code in Zend Framework, but is used as an example.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/google-analytics-results-on-your-website/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/google-analytics-results-on-your-website/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Zend Framework get domain name</title>
		<link>http://stuntsnippets.com/zend-framework-get-domain-name/</link>
		<comments>http://stuntsnippets.com/zend-framework-get-domain-name/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 15:30:09 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[domain]]></category>
		<category><![CDATA[get]]></category>
		<category><![CDATA[name]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1042</guid>
		<description><![CDATA[There is an easy way to get current domain name in Zend Framework. To get domain name in Zend Framework &#8211; use following snippet in your view file. echo $this-&#62;serverUrl&#40;&#41;;]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fzend-framework-get-domain-name%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fzend-framework-get-domain-name%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>There is an easy way to get current domain name in Zend Framework. To get domain name in Zend Framework &#8211; use following snippet in your view file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">serverUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/zend-framework-get-domain-name/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/zend-framework-get-domain-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get number of Twitter followers with PHP</title>
		<link>http://stuntsnippets.com/get-number-twitter-followers-php/</link>
		<comments>http://stuntsnippets.com/get-number-twitter-followers-php/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 14:07:33 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[get number of Twitter followers]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1020</guid>
		<description><![CDATA[Get number of Twitter followers using this PHP snippet is easy. Once you get all the data from Twitter, you just have to isolate followers_count. Besides followers count, you can extract following data as well: id name screen_name location description profile_image_url url friends_count created_at lang Now, finally, to get number of Twitter followers, use following [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fget-number-twitter-followers-php%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fget-number-twitter-followers-php%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Get number of Twitter followers</strong> using this PHP snippet is easy. Once you get all the data from Twitter, you just have to isolate <em>followers_count</em>. Besides followers count, you can extract following data as well:</p>
<ol>
<li>id</li>
<li>name</li>
<li>screen_name</li>
<li>location</li>
<li>description</li>
<li>profile_image_url</li>
<li>url</li>
<li>friends_count</li>
<li>created_at</li>
<li>lang</li>
</ol>
<p>Now, finally, to <strong>get number of Twitter followers</strong>, use following snippet:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$screenName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'stuntsnippets'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$getData</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'followers_count'</span><span style="color: #339933;">;</span>
&nbsp;
<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;">'http://twitter.com/users/show.xml?screen_name='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$screenName</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;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$getData</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&gt;(.*)&lt;/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice: Don&#8217;t forget to do some <a href="http://stuntsnippets.com/php-caching/">PHP caching</a> for your script. You don&#8217;t want to have too many requests toward Twitter.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/get-number-twitter-followers-php/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/get-number-twitter-followers-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Magento static block</title>
		<link>http://stuntsnippets.com/magento-static-block/</link>
		<comments>http://stuntsnippets.com/magento-static-block/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 15:30:51 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[Mage]]></category>
		<category><![CDATA[static block]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=991</guid>
		<description><![CDATA[Have you ever wondered how to display Magento static block inside your app/store? It is an easy task, so just use following snippet. First, you have to create your static block in Magento admin, and than use its name to load it in your web application. 1 2 $block = Mage::getModel&#40;'cms/block'&#41;-&#62;setStoreId&#40;Mage::app&#40;&#41;-&#62;getStore&#40;&#41;-&#62;getId&#40;&#41;&#41;-&#62;load&#40;&#34;Magento_static_block_name&#34;&#41;; echo $block;]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fmagento-static-block%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fmagento-static-block%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Have you ever wondered how to display Magento static block inside your app/store? It is an easy task, so just use following snippet. First, you have to create your static block in Magento admin, and than use its name to load it in your web application.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$block</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cms/block'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStoreId</span><span style="color: #009900;">&#40;</span>Mage<span style="color: #339933;">::</span><span style="color: #004000;">app</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Magento_static_block_name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$block</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/magento-static-block/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/magento-static-block/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP contact form</title>
		<link>http://stuntsnippets.com/php-contact-form/</link>
		<comments>http://stuntsnippets.com/php-contact-form/#comments</comments>
		<pubDate>Thu, 16 Dec 2010 12:05:11 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[contact]]></category>
		<category><![CDATA[contact form]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[PHP contact form]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=966</guid>
		<description><![CDATA[So, almost every website needs contact form - is it a real contact form or just a feedback from your users. We need to make a contact form as easy to deal with as possible.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-contact-form%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-contact-form%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>So, almost every website needs contact form &#8211; is it a real contact form or just a feedback from your users. We need to make a contact form as easy to deal with as possible. There is a simple way of dealing with all elements on your contact form by doing a simple trick. We are going to use a name convention to easily get all elements entered by user, to wrap them, and finally &#8211; to get them sent to the website owner.</p>
<p>First we shall start with a little bit of HTML as it is necessary for our contact form to live. Here &#8211; we are going to use our name convention to easily deal with details.</p>
<p>HTML contact form:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;contact.php&quot; method=&quot;post&quot;&gt;
&lt;dl class=&quot;php_contact_form&quot;&gt;
	&lt;dt&gt;
		&lt;label for=&quot;php_contact_form[email]&quot;&gt;E-mail:&lt;/label&gt;
	&lt;/dt&gt;
	&lt;dd&gt;
		&lt;input type=&quot;text&quot; name=&quot;php_contact_form[email]&quot; /&gt;
	&lt;/dd&gt;
	&lt;dt&gt;
		&lt;label for=&quot;php_contact_form[subject]&quot;&gt;Subject:&lt;/label&gt;
	&lt;/dt&gt;
	&lt;dd&gt;
		&lt;input type=&quot;text&quot; name=&quot;php_contact_form[subject]&quot; /&gt;
	&lt;/dd&gt;
	&lt;dt&gt;
		&lt;label for=&quot;php_contact_form[message]&quot;&gt;Message:&lt;/label&gt;
	&lt;/dt&gt;
	&lt;dd&gt;
		&lt;textarea type=&quot;text&quot; name=&quot;php_contact_form[message]&quot; &gt;&lt;/textarea&gt;
	&lt;/dd&gt;
	&lt;dt&gt;
&nbsp;
	&lt;/dt&gt;
	&lt;dd&gt;
		&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
	&lt;/dd&gt;
&lt;/dl&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Now &#8211; you might have noticed that all names are in &#8220;array&#8221; naming convention. They all belong to a single parameter that is going to be posted: <strong>php_contact_form</strong>.</p>
<p>As we are posting data from our PHP contact form to a contact.php, this is how contact.php should look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//if there is any data posted - we should process it</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//we shall be sending e-mail with data from our php contact form,</span>
  <span style="color: #666666; font-style: italic;">//so let just fill in a message</span>
  <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'php_contact_form'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$headers</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'MIME-Version: 1.0'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Content-type: text/html; charset=utf-8'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//now that we have a message from our php contact form</span>
  <span style="color: #666666; font-style: italic;">//we should send it to recipient</span>
  <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contact@domain.com'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'New message from our php contact form'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Thank you for contacting us!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Notice:</strong> Real contact form should have some form of validation on both client and server side. It should also be protected with (at least) <a href="http://stuntsnippets.com/simple-php-captcha/">simple php captcha</a> to prevent spam bots from sending a large amount of SPAM mail. However &#8211; this is just an example of how PHP contact form can be made for easy handling.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/php-contact-form/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/php-contact-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP download counter</title>
		<link>http://stuntsnippets.com/php-download-counter/</link>
		<comments>http://stuntsnippets.com/php-download-counter/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 16:00:40 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[download]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=899</guid>
		<description><![CDATA[This is an example how PHP download counter could be made. This is just a basic but still functional example. So, in order to make a download counter we shall need three scripts: download_file.php download_count.php show_download_number.php First part of our PHP download counter script will be replacing actual file that should be downloaded, and will [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-download-counter%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-download-counter%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This is an example how <strong>PHP download counter</strong> could be made. This is just a <strong>basic</strong> but still functional example. So, in order to make a download counter we shall need three scripts:</p>
<ul>
<li>download_file.php</li>
<li>download_count.php</li>
<li>show_download_number.php</li>
</ul>
<p>First part of our PHP download counter script will be replacing actual file that should be downloaded, and will count number of times someone has downloaded that file. Lets say your HTML link to a file will not be file.xls, but download_file.php instead. This is what a whole file should look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">$download_num = require 'download_count.php'; //get number of times file has been downloaded
$download_num++;
&nbsp;
$my_stat = &quot;download_count.php&quot;;
$handler = fopen($my_stat, 'w');
fwrite($handler, &quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot; . <span style="color: #006699; font-weight: bold;">$download_num</span> . &quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;); //rewrite a whole download_count.php file
fclose($handler);
&nbsp;
header('location: /file.xls'); //redirect user to his file</pre></td></tr></table></div>

<p>Second file (download_count.php) should be one line PHP file containing nothing but code from following snippet:</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> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This PHP file only returns value of download count, and since this is our setup, number of downloads should be zero.</p>
<p>Third file is used to see how many downloads has our counter recorded. So, include following snippet in your administration in order to present administrator with number of downloads of his file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$download_num</span> <span style="color: #339933;">=</span> <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'download_count.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'PHP download counter has recorded: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$download_num</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' downloads of your file.xls'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Notice:</strong> This is a basic level script, and script of this kind should be able to track downloads of all files &#8211; and not just one. With simple adjustments this could be done, but it was not the scope of this article.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/php-download-counter/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/php-download-counter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Date and time before extension</title>
		<link>http://stuntsnippets.com/date-time-before-extension/</link>
		<comments>http://stuntsnippets.com/date-time-before-extension/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 07:00:09 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=845</guid>
		<description><![CDATA[Script authors: Drasko Gomboc and Sankovic Marko. When running automated backup it is a good practice to save exact time when you made backup in your file name. This snippet shows you how to automatically add date and time before extension in your file name. 1 2 3 $filepath = '/backup/folder/drasko_gomboc-and-sankovic_marko.txt'; $new_filepath = preg_replace&#40;&#34;/(\.[^\.]*)$/&#34;, date&#40;'-Y-m-d-h-i-s'&#41; [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fdate-time-before-extension%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fdate-time-before-extension%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Script authors:</strong> Drasko Gomboc and Sankovic Marko.</p>
<p>When running automated backup it is a good practice to save exact time when you made backup in your file name. This snippet shows you how to automatically add date and time before extension in your file name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filepath</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/backup/folder/drasko_gomboc-and-sankovic_marko.txt'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$new_filepath</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/(\.[^\.]*)$/&quot;</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-h-i-s'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filepath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Add date and time before file extension</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$new_filepath</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//output should look like this -&gt; /backup/folder/drasko_gomboc-and-sankovic_marko-2010-10-27-09-00-00.txt</span></pre></td></tr></table></div>

<p>Script has been provided by generous guys and great developers: <strong>Drasko Gomboc</strong> and <strong>Sankovic Marko</strong>.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/date-time-before-extension/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/date-time-before-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

