<?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>designing4u.de &#187; OOP PHP</title>
	<atom:link href="http://www.designing4u.de/category/oop-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Thu, 13 May 2010 13:24:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Accessing Google Analytics API with Zend Framework</title>
		<link>http://www.designing4u.de/2009/09/accessing-google-analytics-api-with-zend-framework/</link>
		<comments>http://www.designing4u.de/2009/09/accessing-google-analytics-api-with-zend-framework/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 19:52:39 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend_Http_Client]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=34</guid>
		<description><![CDATA[After a long time Google finally released the API which allows the read access to the Google Analytics accounts. Since I needed it to perform some analytics and included it into one of my projects written with Zend Framework. Zend framework provides a massive library collection which you can use to access Google services, but [...]]]></description>
			<content:encoded><![CDATA[<p>After a long time Google finally released the API which allows the read access to the Google Analytics accounts. Since I needed it to perform some analytics and included it into one of my projects written with Zend Framework. Zend framework provides a massive library collection which you can use to access Google services, but unfortunately it does not provide the interface for accessing the analytics yet. Therefore I decided to share the code.<br />
<span id="more-34"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * 
 *  @author Wojciech Gancarczyk &lt;gancarczyk@gmail.com&gt;
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> App_Analytics_Helper
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">const</span> CLIENTLOGIN_URL <span style="color: #339933;">=</span> <span style="color: #0000ff;">'https://www.google.com/accounts/ClientLogin'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">const</span> ACCOUNT_FEED_URL <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.google.com/analytics/feeds/accounts/default'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">const</span> DATA_FEED_URL <span style="color: #339933;">=</span> <span style="color: #0000ff;">'https://www.google.com/analytics/feeds/data'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">const</span> SERVICE_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">'analytics'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string
     */</span>
    protected <span style="color: #000088;">$_auth</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string
     */</span>
    protected <span style="color: #000088;">$_applicationName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'MyApp-MyDesc-0.1'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string
     */</span>
    protected <span style="color: #000088;">$_username</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string
     */</span>
    protected <span style="color: #000088;">$_password</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var int
     */</span>
    protected <span style="color: #000088;">$_ga</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Class counstructor. It accepts only array as a parameter. It then
     * checks, if the array keys correspond to the setters of this class
     * and if they do, it sets them. After that it is trying to perform
     * authentication.
     *
     * @param array $params
     * @throws Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Constructor accepts only an array as a parameter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$methods</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_class_methods</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'set'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$methods</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$method</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Performs authentication. It has to be calles before any data
     * can be received, because auth is neccassary for each call.
     *
     * @param string $username
     * @param string $password
     * @throws Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> authenticate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$username</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUsername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Username is necessary to perform authentication'</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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPassword</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Password is necessary to perform authentication'</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;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUsername</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPassword</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Http_Client<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">CLIENTLOGIN_URL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span>Zend_Http_Client<span style="color: #339933;">::</span><span style="color: #004000;">POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'Email'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUsername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Passwd'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPassword</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'AccountType'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'GOOGLE'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'service'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">SERVICE_NAME</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'source'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getApplicationName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</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;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'200'</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Authentication failed. Status returned: '</span> <span style="color: #339933;">.</span>
                <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'='</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">setAuth</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$return</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Recieves the account feed.
     *
     * @return string XML stream
     * @throws Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAccountFeed<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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAuth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Account Feed cannot retrieve any results, '</span> <span style="color: #339933;">.</span>
                <span style="color: #0000ff;">'because the client was not authenticated'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Http_Client<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">ACCOUNT_FEED_URL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span>Zend_Http_Client<span style="color: #339933;">::</span><span style="color: #004000;">GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start-index'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'max-results'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prettyprint'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'true'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Authorization'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GoogleLogin auth='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAuth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET'</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;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'200'</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Authentication failed. Status returned: '</span> <span style="color: #339933;">.</span>
                <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Performs data feed.
     *
     * @return string XML stream
     * @throws Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDataFeed<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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAuth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Account Feed cannot retrieve any results, '</span> <span style="color: #339933;">.</span>
                <span style="color: #0000ff;">'because the client was not authenticated'</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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getGa</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Account ID was not provided'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Http_Client<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">DATA_FEED_URL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span>Zend_Http_Client<span style="color: #339933;">::</span><span style="color: #004000;">GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ids'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ga:'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getGa</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dimensions'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ga:source,ga:medium'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'metrics'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ga:visits,ga:bounces'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sort'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-ga:visits'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start-date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2009-08-01'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end-date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2009-08-31'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start-index'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'10'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'max-results'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'100'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Authorization'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GoogleLogin auth='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAuth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET'</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;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'200'</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Authentication failed. Status returned: '</span> <span style="color: #339933;">.</span>
                <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Setter for username.
     *
     * @param string $username
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUsername<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</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>_username <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Getter for username.
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUsername<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Setter for password.
     *
     * @param string $password
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setPassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</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>_password <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Getter for password.
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_password<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Setter for auth.
     *
     * @param string $auth
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setAuth<span style="color: #009900;">&#40;</span><span style="color: #000088;">$auth</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>_auth <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Getter for getAuth.
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAuth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_auth<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Setter for applicationName.
     *
     * @param string applicationName
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setApplicationName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</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>_applicationName <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Getter for applicationName.
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getApplicationName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_applicationName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Setter for ga.
     *
     * @param integer $ga
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setGa<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ga</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>_ga <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$ga</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Getter for ga.
     *
     * @return integer
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getGa<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_ga<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you can see this class is not perfect yet. It basically utilizes Zend_Http_Client and its methods to perform the request and retrieve the results. First of all it needs a better support for setting the parameters to retrieve data and account feeds. Right now they are just hard coded and you cannot influence their values. Besides that as a return value it returns the XML stream. Zend Framework comes with an awesome library Zend_Dom which can be used to read the values from the response. I will definitely put that in this class, but it is a good subject for a next post. You may also consider using it as a controller helper, but then you will need to rewrite it a little bit. It might be another interesting subject for a post.</p>
<p>One more thing left mentioning is the usage of this class. Well, there are two ways of using it. You can either create an array of parameters and pass it while you instantiating this class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'yourUserName'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'yourPassword'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'ga'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'accountId'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$analytics</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> App_Analytics_Helper<span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dataFeed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$analytics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDataFeed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$accountFeed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$analytics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAccountFeed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can also use the setters and perform authentication "manually":</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$analytics</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> App_Analytics_Helper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$analytics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUsername</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'yourUserName'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$analytics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'yourPassword'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$analytics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setGa</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'accountId'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$analytics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dataFeed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$analytics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDataFeed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$accountFeed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$analytics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAccountFeed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I'm waiting for your comments about this class. I hope it will save you some time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2009/09/accessing-google-analytics-api-with-zend-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SMPT authentication with Zend_Mail</title>
		<link>http://www.designing4u.de/2009/09/smpt-authentication-with-zend_mail/</link>
		<comments>http://www.designing4u.de/2009/09/smpt-authentication-with-zend_mail/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 16:30:02 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[SMTP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend_Mail]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=33</guid>
		<description><![CDATA[It's been a long time since I wrote my last post. Well, I was busy working and getting familiar with Zend Framework. Since the Zend Framework documentation is not really helpful when it comes to send emails using SMTP, I decided I show you how I accomplished that.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * @author Wojciech Gancarczyk
 */
class MyApp_Mail_Example
&#123;
  [...]]]></description>
			<content:encoded><![CDATA[<p>It's been a long time since I wrote my last post. Well, I was busy working and getting familiar with Zend Framework. Since the Zend Framework documentation is not really helpful when it comes to send emails using SMTP, I decided I show you how I accomplished that.<br />
<span id="more-33"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @author Wojciech Gancarczyk
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> MyApp_Mail_Example
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * @var Zend_Mail_Protocol_Smtp_Auth_Login
     */</span>
    protected <span style="color: #000088;">$_connection</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var Zend_Mail_Transport_Smtp
     */</span>
    protected <span style="color: #000088;">$_transport</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string
     */</span>
    protected <span style="color: #000088;">$_host</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> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo.bar.baz'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$port</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'password'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection <span style="color: #339933;">=</span>
            <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail_Protocol_Smtp_Auth_Login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</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>_connection<span style="color: #339933;">-&gt;</span><span style="color: #004000;">helo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_transport <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail_Transport_Smtp<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>_transport<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setConnection</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get the connection.
     *
     * @return Zend_Mail_Protocol_Smtp_Auth_Login
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connection<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get the transport.
     *
     * @return Zend_Mail_Transport_Smtp
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTransport<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_transport<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get the host.
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getHost<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Send the emial
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo@bar.baz, '</span>Foo Bar<span style="color: #0000ff;">');
        $mail-&gt;setFrom('</span>foo<span style="color: #339933;">@</span>bar<span style="color: #339933;">.</span>baz<span style="color: #0000ff;">', '</span>Foo Bar<span style="color: #0000ff;">');
        $mail-&gt;setSubject(
            '</span>Demonstration <span style="color: #339933;">-</span> Sending Multiple Mails per SMTP Connection<span style="color: #0000ff;">'
        );
        $mail-&gt;setBodyText('</span><span style="color: #339933;">...</span>Your message here<span style="color: #339933;">...</span><span style="color: #0000ff;">');
        $mail-&gt;send($this-&gt;getTransport());
    }
}</span></pre></td></tr></table></div>

<p>That's it. Of course, you can break it into smaller units of code, resolve the dependencies etc. I just wanted to demonstrate the basics of operation. Class constructor demonstrates mostly, what you need to do to get it work. Basically you need to initialize Zend_Mail_Protocol_Smtp_Auth_Login by passing the host name, port and credentials. After that you connect to your mail server and initiate helo/ehlo sequence which marks the session as valid. After that you need to initialize Zend_Mail_Transport_Smtp and set the connection. The parameter you pass to setConnection method has to be an instance of Zend_Mail_Protocol_Abstract. After performing this initialization you are ready to send the emails. I put an example method send() to demonstrate how to do that. Don't forget to change the credentials, host name and all the emails in the send method. I'm waiting for your feedback, how to improve that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2009/09/smpt-authentication-with-zend_mail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zip code search using jQuery and Google maps &#8211; displaying results in a defined radius</title>
		<link>http://www.designing4u.de/2009/05/zip-code-search-using-jquery-and-google-maps-displaying-results-in-a-defined-radius/</link>
		<comments>http://www.designing4u.de/2009/05/zip-code-search-using-jquery-and-google-maps-displaying-results-in-a-defined-radius/#comments</comments>
		<pubDate>Sun, 10 May 2009 15:18:36 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[distance from zip code]]></category>
		<category><![CDATA[GMap2]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zip code]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=32</guid>
		<description><![CDATA[Last time I wrote about handling a Google map event, which is triggered, when a user moves around the map. I only mentioned that I had to use this solution for one of my projects, which goal was to display companies branches in a certain distance radius. I decided that it probably will be really [...]]]></description>
			<content:encoded><![CDATA[<p>Last time I wrote about handling a Google map event, which is triggered, when a user moves around the map. I only mentioned that I had to use this solution for one of my projects, which goal was to display companies branches in a certain distance radius. I decided that it probably will be really interesting for all of you, how to display all company branches according to user input, therefore this time I will show you, how to retrieve the addresses of the places from a MySQL database, based on the zip code and radius provided by a user. I will also split this example in two parts. Todays part will only handle retrieving the results, displaying a simple navigation and markers on the map. In the second part we will push our project a little bit farther and we will allow our user to display the directions from the zip code he or she provided to the branch of his or hers choice.<br />
<span id="more-32"></span><br />
To see a working example of this tutorial you can visit <a href="http://www.designing4u.de/examples/calculating-distance/index.html" target="_blank">this</a> site.</p>
<p>As I already mentioned in my previous post, to measure the distance between two points on a sphere one can use Haversine formula. For a detailed description I suggest visiting the wikipedia site. In our example we will use a simplified version of Haversine formula, which is caused by a really simple reason. Executing a web query with the actual Haversine formula might lead to a really slow response from the database and our simplified version will retrieve results which do not differentiate that much with the original version.</p>
<p>For retrieving the company branches in a certain radius we will first build a php class, which will be responsible for translating the zip code into geographical lengths and then use this values to retrieve the results and return them as json_encoded() data. Because our class will be relatively long, I will split it into parts and explain it line by line.</p>
<p>Since we are using an AJAX request to retrieve our data and the location of our php file is easy to guess just by looking at our jQuery code (will come after php class), we add a simple check of the apache environmental variable to check, if it actually is an XMLHttpRequest. A lot of you will probably say that this security measure is not enough, but for this example, it will at least stop all the robots trying access this file and since looking up addresses might consume a lot of resources of your server, this will save you some speed lost on servers with high traffic.</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: #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;">&quot;HTTP_X_REQUESTED_WITH&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'XMLHttpRequest'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'why are you doing this??'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In line 3 we create our class. Since the subject of this example is finding company branches, lets call it Branch. In lines 5 to 13, we define some variables, which we will use in our class. I think they are pretty much self explanatory, and you will understand, what is their usage just by looking at the rest of the class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Branches <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR GOOGLE MAPS KEY'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_db_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_db_pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'pass'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'db'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$zip</span><span style="color: #339933;">,</span> <span style="color: #000088;">$radius</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data_set</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In our constructor we connect to the database and set the radius and zip properties. You could notice that I convert the zip property to integer as well and I additionally add leading zero for strings, which length equals 4. Well, this class was written for German zip codes. You might want to change this parts so it suits your needs. After all it will be important in the part where we will convert the zip code to geographical length, so Google can find this zip code in their database.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</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>
  <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</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;">$_host</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_db_user</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_db_pass</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_db_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">radius</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'radius'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'zip'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// add leading zeros in case of 0... zip codes</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%05s</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In line 26 we define a public method translateZipToLatLng() and in line 39 a private method getGeolocation(), which both will be responsible for translating the zip code into the geographical length. To get a detailed description, what this two methods actually do, refer to my other post:  <a href="http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/">Geolocation class - retrieving longitude and latitude using google maps</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> translateZipToLatLng<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://maps.google.com/maps/geo?q='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">', Germany'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&amp;output=csv&amp;key='</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_key</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getGeolocation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</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: #0000ff;">'200'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$geo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$geo</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$geo</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGeolocation<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$init</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #339933;">,</span> CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_USER_AGENT&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<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;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In line 51 we define a private method getResults, which, as you can see, will be responsible for retrieving all the information from the “branches" table and additionally measure the approximate distance from the zip code provided by the user to our actual company branches. In line 56 we define private method populate results, which will be responsible for creating an array of the results retrieved from the database. As you can see, if our query returns zero rows we define an error, if our query returns at least one row, we create an array with all the information about the company. The last public method in line 75, findBranches() will be the one, which we will call after instantiating our class and translating zip code into geographical length.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getResults<span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT *, (POW((69.1*(longitude-<span style="color: #006699; font-weight: bold;">{$latlng[1]}</span>)*COS(<span style="color: #006699; font-weight: bold;">{$latlng[0]}</span>/57.3)), '2') + POW((69.1*(latitude-<span style="color: #006699; font-weight: bold;">{$latlng[0]}</span>)), '2')) AS distance FROM branches HAVING distance &lt; <span style="color: #006699; font-weight: bold;">{$this-&gt;radius}</span> ORDER BY distance ASC;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> populateResults<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: #339933;">!</span><span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_set</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;">results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'none'</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: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_set</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;">results</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'name'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'zip'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'zip'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'city'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'city'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'latitude'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'longitude'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'distance'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'distance'</span><span style="color: #009900;">&#93;</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> findBranches<span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</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;">data_set</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResults</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">populateResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Our class for retrieving results is done. For the simplicity I did not assume the case, when the database query returns zero rows, in which radius should be automatically extended. You can easily do this, by writing one more method, which will be responsible for extending the radius and adding a simple if...else check in our findBranches() method, to make it loop until your query returns any results. As I said before though, in a database with many entries, executing a web query with a formula complicated like the one we are using, retrieving results might consume a lot of time, and creating a loop, in which you will repeat the same query might lead to a really long time a user will have to wait for a response. In my case, the database was a little bit over 1000 entries and extending the radius and repeating the query did not result in any remarkable performance lost though.</p>
<p>The last part is the actual usage of the class we just wrote. Basically in line 84 we instantiate our class, in line 85 we translate the zip code to geographical length and according to the value returned by this method we use json_encode() function to return the results as a json representation of the array we just created or an error.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>84
85
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$branches</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Branches<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$branches</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">translateZipToLatLng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$branches</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findBranches</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'zip_invalid'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Since our class for retrieving the results in a certain radius is ready, we should move to our client side code.</p>
<p>To make this simple, I will just show you the whole jQuery code and discuss it line by line.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #003366; font-weight: bold;">var</span> mapContainer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#map'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLargeMapControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519015140666</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.419671058654785</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#submit'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> zip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#zip'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> radius <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#radius'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>zip <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You need to provide a zip code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">var</span> rand <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'zip.php?rand='</span> <span style="color: #339933;">+</span> rand<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> zip<span style="color: #339933;">:</span> zip<span style="color: #339933;">,</span> radius<span style="color: #339933;">:</span> radius <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> markers <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> descriptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> bounds <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLngBounds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          map.<span style="color: #660066;">clearOverlays</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#error'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#locations'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#closest_location'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
          $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">error</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'No results found or ZIP incorrect'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#error'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'No results found or ZIP incorrect'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span>
              <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #003366; font-weight: bold;">var</span> description <span style="color: #339933;">=</span>
              <span style="color: #3366CC;">'&lt;div class=&quot;branch&quot; style=&quot;float: left;&quot;&gt;'</span> <span style="color: #339933;">+</span>
              <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' (~ '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">distance</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' km)&lt;br /&gt;'</span> <span style="color: #339933;">+</span>
              <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">zip</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">city</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;br /&gt;'</span> <span style="color: #339933;">+</span>
              <span style="color: #3366CC;">'&lt;span class=&quot;branch_show&quot; style=&quot;cursor: pointer; color: blue; text-decoration: underline;&quot; id=&quot;'</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;Show&lt;/span&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> lat <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> lng <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>lat<span style="color: #339933;">,</span> lng<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            markers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> marker<span style="color: #339933;">;</span>
            descriptions<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#locations'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>description<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            bounds.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              marker.<span style="color: #660066;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>description<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#closest_location'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>descriptions<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          map.<span style="color: #660066;">setZoom</span><span style="color: #009900;">&#40;</span>map.<span style="color: #660066;">getBoundsZoomLevel</span><span style="color: #009900;">&#40;</span>bounds<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #006600; font-style: italic;">// just in case we did not find anything</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>flag<span style="color: #009900;">&#41;</span>
            map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">51.08282186160976</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10.26123046875</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">else</span>
            map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>bounds.<span style="color: #660066;">getCenter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.branch_show'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            markers<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>descriptions<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As usually we want to execute the code on onLoad of our document and we do that in line 1. In line 2 we define our container for the map and in line 3 and 4 we check if browser is compatible and if container was found respectively.</p>
<p>In line 5 we instantiate the Gmap2 inside of our map container, we add some controls in line 6 and set the center in line 7. In line 9 we add a event listener which triggers after a user clicks submit button.</p>
<p>In lines 10 to 15 we add a simple check, if the field with the zip code is not empty and it if is we alert an according message and we break the execution of the code. Additionally we define a zip variable in line 10 and radius variable in line 11, which will hold according information to use in our json request.</p>
<p>In line 16 we define rand variable which we pass during our json request to prevent internet explorer to cache the resource. If we would not do that, and changes you would do to zip.php file would not be visible, because IE would use the cached version of this file.</p>
<p>In line 17 we make a request to the zip.php file, which we wrote earlier and we pass the zip code and the radius to our php file. After the calculation is done and you do not have any errors in your php file  we retrieve the information, which we can finally process.</p>
<p>In line 18 and 19 we define two variables as arrays, which will be responsible for holding the information about markers and their corresponding descriptions and in line 20 we instantiate GLatLngBounds object, which we will later use to display the information on our map with matching zoom level.</p>
<p>In line 22 to 26 we remove all markers and we empty all containers holding the information about the places from a previous request. We do that to prevent displaying of the same markers after a user requests the zip.php file again. This way, if a user changes his or hers query, we will be sure that we are displaying only the information relevant to the current request and we wont have any trash from the previous requests. In line 26 we define a flag as boolean to use it later to display error information and redisplay the center of the map.</p>
<p>In line 28 we loop through the information we received in response. If an error was found, we break our loop and we display and alert according information to the user. We also set the flag to false to redisplay center of the map later in the code. This would be lines 29 to 34.</p>
<p>In lines 35 to 39 we put together the HTML code which we will use to display the information about the branches we are going to display on the map.</p>
<p>In lines 40 to 43 we define two variables lat and lng and use them to instantiate GLatLng object which we define as point variable. In line 44 we define marker variable as an instance of GMarker object and in lines 45 and 46 we add it to the arrays we defined earlier. In line 47 we append the description to the container we need to define in our HTML and in line 47 we add markers to the map. In line 48 we extend the bounds of our map by using a method extend() of GLatLngBounds object we defined earlier. This amazing functionality of google maps will let us display a square with all our markers and google map will take care of setting the correct zoom level.</p>
<p>In lines 49 to 51 we add a listener which will be triggered on a click event and will display the description in an info cloud. In line 52 we close the loop and in line 54 we append the first description to the closest_location container. To assure that the first description is really the closest place to the zip code provided by the user, you cannot forget about putting the “ORDER BY distance ASC” in the php code of zip.php file.</p>
<p>In line 56 we set the zoom of our map using our bound object I was mentioning before by utilizing getBoundsZoomLevel method of GMap. As you can see in the description in line 57, if our php code did not return any results, we override the zoom level we defined in line 56 be setting it again to the default value (line 59). On the other hand, if our php code returned any results we set the center of the map using our bounds object again (line 61).</p>
<p>The last part of this code is just a listener which is triggered when a user clicks on the pseudo link show. We retrieve the id of this link and display the according info window on the map.</p>
<p>That would be it. The code is done. I know it is not perfect and accurate either. You can still use it in many different ways. It does not necessarily need to be a zip code search. Using it you can display distances between users and any other stuff connected with distances. In the second part I will show you how you can make this code a little bit more accurate using google maps build in api to show the directions, how to get from the zip code provided by the user to one of the company branches.</p>
<p>If you do not want to copy all the code, here is a <a href="/examples/calculating-distance/code.zip">zip package</a> for you with working files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2009/05/zip-code-search-using-jquery-and-google-maps-displaying-results-in-a-defined-radius/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Maps and jQuery &#8211; calculating distance between two points</title>
		<link>http://www.designing4u.de/2008/11/google-maps-and-jquery-calculating-distance-between-two-points/</link>
		<comments>http://www.designing4u.de/2008/11/google-maps-and-jquery-calculating-distance-between-two-points/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 10:39:43 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[KML]]></category>
		<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[drop down]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=28</guid>
		<description><![CDATA[It has been a long time since I wrote my last example, but I've been really busy. Lately I got a request to write an example about calculating the distance between two points using KML file. What I will show you in this example is how to provide an user two drop down fields, which [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a long time since I wrote my last example, but I've been really busy. Lately I got a request to write an example about calculating the distance between two points using KML file. What I will show you in this example is how to provide an user two drop down fields, which will be build according to provided KML file. Choosing each field will show a marker on the map. Changing the drop down field will cause removal of an according marker and placement of it in a new location. Also the markers will have two different colors, so an user can differentiate between starting and ending point. As the last functionality we will add a button. After an user clicks on the button, our function will calculate the rout and display the distance in kilometers. Let's start with a simple class which we will use to display drop down field and retrieve the coordination of requested place.<br />
<span id="more-28"></span></p>
<p>To see, how our final solution will look like, go <a href="/examples/geo-directions/index.php" title="Google Maps and jQuery - calculating distance between two points" target="_blank">here</a></p>
<h3>1. Geo Class - Geo.class.php</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Geo
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">;</span>
&nbsp;
    protected <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>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'geo.xml'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'SimpleXMLElement'</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> getDropDown<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$option</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;select name=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; id=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$option</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'include_custom'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$select</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;option value=&quot;&quot; selected=&quot;selected&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$option</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'include_custom'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/option&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$child</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$select</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;option value=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/option&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$select</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/select&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$select</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> getLocation<span style="color: #009900;">&#40;</span><span style="color: #000088;">$identifier</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>_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$coordinates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$child</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$identifier</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Point</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">coordinates</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$coordinates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                    <span style="color: #0000ff;">'lat'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">'lng'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$coordinates</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It wasn't really necessary to put this functions into a class, but it is just a habit <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  We define a protected function _init(), which we call every time we want to use our KML file to retrieve information out of it. The function simplexml_load_file initializes an SimpleXMLElement object for us, which we can use later. The function getDropDown with the first mandatory argument and the second optional will be responsible for displaying the drop down field for us. I think I don't need to explain it in detail. The last function getLocation will retrieve the latitude and longitude from KML file. So let's move to index.php file, which will be the core of this example.</p>
<h3>2. Core file - index.php</h3>

<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: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Geo.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$geo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Geo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Before we send any headers we include our Geo.class.php file and initialize our Geo class. After that goes the usual <html> tag <meta> tags and the google maps and jQuery scripts. In the <head> section of our document goes the actual jQuery part.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #003366; font-weight: bold;">var</span> mapContainer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#map'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> markerStart<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> markerEnd<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>GBrowserIsCompatible<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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLargeMapControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519015140666</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.419671058654785</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#start'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> location <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>location <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'helper.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> location<span style="color: #339933;">:</span> location <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>markerStart<span style="color: #009900;">&#41;</span>
            map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>markerStart<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          pointA <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">lat</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          markerStart <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>markerStart<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#end'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> location <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>location <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'helper.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> location<span style="color: #339933;">:</span> location <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>markerEnd<span style="color: #009900;">&#41;</span>
            map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>markerEnd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #003366; font-weight: bold;">var</span> blueIcon <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GIcon<span style="color: #009900;">&#40;</span>G_DEFAULT_ICON<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          blueIcon.<span style="color: #660066;">image</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png&quot;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> markerEndOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> icon<span style="color: #339933;">:</span> blueIcon <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
          pointB <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">lat</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          markerEnd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointB<span style="color: #339933;">,</span> markerEndOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>markerEnd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#submit'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">pointA</span> <span style="color: #339933;">===</span> undefined <span style="color: #339933;">||</span> window.<span style="color: #660066;">pointB</span> <span style="color: #339933;">===</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You need to choose start and end&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> directions <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GDirections<span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        route <span style="color: #339933;">=</span> Array<span style="color: #009900;">&#40;</span>pointA<span style="color: #339933;">,</span> pointB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>directions<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> dist <span style="color: #339933;">=</span> directions.<span style="color: #660066;">getDistance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#distance'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>dist<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        directions.<span style="color: #660066;">loadFromWaypoints</span><span style="color: #009900;">&#40;</span>route<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Maybe it is long but pretty straight forward. As you can read in my other examples, we first initialize jQuery, so it starts after our document is loaded. After that we define our map container and our markers. After we check if our browser is compatible and our map container exists, we initialize the map with couple options (large controls, zoom level = 14 and the center of the map). </p>
<p>We add two event listeners, which are triggered on onchange event of our drop downs (with id start and end respectively, the html code will follow). If you use the $option parameter in getDropDown function for displaying custom blank option, we check if the user chose this option and return false if he did. If he chose a valid option, we use an AJAX request and retrieve the geolocation from our helper file. </p>
<p>After our call returns some data, we check, if we have already any markers saved in our map and if we do, we first remove them and then place a new marker according to the option chosen in the drop down. </p>
<p>We repeat the same for the drop down with id 'end'. Actually we could have put this two events into a separate function, but to make this example clear I purposely didn't do that.</p>
<p>The last event listener is triggered, when a user clicks on a 'calculate route' button. First we check, if both points were placed by the user, and if not we display an alert, that he or she has to provide valid starting and ending point. This check might not work in IE, therefore you might need to do some changes, so it also works in IE.</p>
<p>We initialize GDirections Class and define our route with starting and ending point. I also use the getDistance method to display the amount of kilometers between this two points. As the last thing we need to do is to display the directions on the map and voila, we are done <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>What we still need it the rest of index.php file and our helper, and KML file.</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;">&lt;div id=&quot;navigation&quot;&gt;
        Start <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$geo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDropDown</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'include_custom'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'please choose...'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        Stop <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$geo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDropDown</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'include_custom'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'please choose...'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;input type=&quot;button&quot; name=&quot;submit&quot; id=&quot;submit&quot; value=&quot;calculate route&quot; /&gt;
&lt;/div&gt;
&lt;div id=&quot;distance&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;map&quot; style=&quot;width: 1000px; height: 600px;&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>Pretty straight forward. We display our navigation with an empty option 'please choose...', the 'calculate route' button and containers for distance and map.</p>
<h3>3. Our helper - helper.php</h3>

<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: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Geo.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$geo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Geo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$geo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLocation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'location'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Pretty simple, isn't it <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Just initialize the class and call the method to retrieve location with the location argument used in the _GET request.</p>
<h3>4. KML file - geo.xml</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;kml</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://earth.google.com/kml/2.2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Alexanderplatz<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Center of Berlin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.52238370531909,13.409671783447266<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Frankfurter Tor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>A nice place to live<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.51595970749194,13.4527587890625<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Warschauerstr.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Place to go and spend some time with friends<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.508568336719854,13.451085090637207<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Revelerstr.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Cassiopeia Club<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.50721006978328,13.457050323486328<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Frankfurter Allee<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Shopping Center<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.513818166144006,13.474087715148926<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/kml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>It is a file I prepared by myself. As far as I know, you can create them using the Google Earth application. Probably they can be way more complex than this one. But I guess it will be a nice subject for next example <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  Good luck with this one and as usually, in case of any question, you know how to find me. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/11/google-maps-and-jquery-calculating-distance-between-two-points/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Symfony &#8211; no error message on empty values in not required fields</title>
		<link>http://www.designing4u.de/2008/07/symfony-no-error-message-on-empty-values-in-not-required-fields/</link>
		<comments>http://www.designing4u.de/2008/07/symfony-no-error-message-on-empty-values-in-not-required-fields/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 14:48:08 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[emtpy values]]></category>
		<category><![CDATA[no error message]]></category>
		<category><![CDATA[not required]]></category>
		<category><![CDATA[not required field]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=25</guid>
		<description><![CDATA[It's been a long time since I wrote my last post, but I kept myself busy with work. I'm just working with another web 2.0 project (yes, there will be another web 2.0 project  , which was written using symfony framework. I need to admit that my previous experience with different frameworks was pretty [...]]]></description>
			<content:encoded><![CDATA[<p>It's been a long time since I wrote my last post, but I kept myself busy with work. I'm just working with another web 2.0 project (yes, there will be another web 2.0 project <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , which was written using symfony framework. I need to admit that my previous experience with different frameworks was pretty good, but symfony is different and I'm really excited about it. In a way it is similar to CakePHP using MVC patter but it has features, which I couldn't find in Cake. I mean symfony has its pros and cons but I find it really easy to build database models and modules using CLI and ORM. Propel is probably not the best abstraction layer, but I was reading on Doctrine site (which I personally find better than Propel) that it will be included in the new 1.2 release. I don't want to write a lot about symfony here, but rather solve a validation problem I encountered during development.<br />
<span id="more-25"></span><br />
Symfony has a really powerful configuration system, in which you can also define your validation rules. My problem was as follows, I have 3 different access control levels and for one of them an input field is required and for the rest not. Let's describe it with an example of a company.</p>
<p>You have managers, executives and simple workers. To validate a manager we generated codes which we later distributed between them. Executives and simple workers didn't have to use a validation code. Therefore a simple input field of type text was provided only if a person registering on our site chose the position manager. Therefore I couldn't make this field as required because it was only required for a certain group of people registering on our site. </p>
<p>The validation rule was simple. I compared the code provided during registration with the one saved in the database and returned true or false according to the case. The problem I encountered was in the case that if a person chose manager and did not provide any input (an empty field), the error message was not displayed at all. I mean you could probably solve this problem using JavaScript but server side validation is always important. Researching the core code of symfony I found a place where this case is not considered an error. I could possibly change the core, but I didn't want to do that only for one case. I mean, if it would be a problem with many fields I would probably consider it necessary, but I've found a solution for that problem without changing the core code. Here is how I accomplished it.</p>
<p>I assume that you are familiar with symfony framework, therefore I wont explain the whole process from the beginning. In my /apps/myproject/lib folder I created a class extending sfValidation class, where I placed the database check. Unfortunately, defining additional string validation rules ($string == null etc.) in that class did not solve my problem that an error did not appear when the field was empty.</p>
<p>I did some more reading and I've found a really interesting solution. In your action.class.php file besides executeXXX() methods you can define additional validateXXX() methods, which are executed before executeXXX() methods. I placed a simple string validation rule in this method (with min value of 1) and it works like a charm right now. Below you can see my code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> validateIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//manager code validation</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'position'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'manager'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$managerCode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'manager_code'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfStringValidator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$val</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'min'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$val</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$managerCode</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;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setError</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'managerCode'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getI18N</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You have to fill out the code'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As I said before this approach might get pretty annoying if you are dealing with multiple fields but for me it was just this one. I hope this solution will help you during your development. As usually I'm waiting for your feedback. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/07/symfony-no-error-message-on-empty-values-in-not-required-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class for retrieving region, city and geolocation</title>
		<link>http://www.designing4u.de/2008/05/class-for-retrieving-region-city-and-geolocation/</link>
		<comments>http://www.designing4u.de/2008/05/class-for-retrieving-region-city-and-geolocation/#comments</comments>
		<pubDate>Mon, 19 May 2008 10:24:29 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[city]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[Geolocation class]]></category>
		<category><![CDATA[location]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[region]]></category>
		<category><![CDATA[retiriving geographical location]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=16</guid>
		<description><![CDATA[For one of the social platforms I was creating I had to provide profile display according to the location from which the request was made. There are plenty services in the internet, which provide you an accurate database, which will translate IP address into region, city and geographical lengths. I didn't want to spend any [...]]]></description>
			<content:encoded><![CDATA[<p>For one of the social platforms I was creating I had to provide profile display according to the location from which the request was made. There are plenty services in the internet, which provide you an accurate database, which will translate IP address into region, city and geographical lengths. I didn't want to spend any money for this kind of service, so I decided to find a free solution for this task. However, none of the services existing in the internet provides you 100% accuracy, the one that I've found locates my IP correctly. It would be nice, if you could give me some feedback, if this class is working for you as good as it works for me. This example requires URL file-access to be enabled on your server, because we will request the XML data through URL call. In this example we will also use SimpleXMLElement class, which should be enable by default in PHP5, to translate our XML file into an object. We will then recurse received object into a suitable array.<br />
<span id="more-16"></span><br />
Unfortunately my provider doesn't enable URL file-access, therefore I wont be able to show you, how this example works. I tested it on different server though and you can trust me, it works. </p>
<p>The first thing you need to do is to go to <a href="http://www.getpos.de/anmeldung.aspx" target="_blank" title="go to getpos.de">getpos.de</a> and register a new account. Unfortunately this service is only in  German, but I don't think you should have any problem with registering a new account. Let's take a look at our class:<br />
[source:php]<br />
class Geolocation {</p>
<p>	private $ip;<br />
	private $xml_data;<br />
	private $xml;</p>
<p>	public $geo;<br />
	public $error;</p>
<p>	function __construct($ip) {<br />
		$this->ip = $ip;<br />
		$this->xml_data = simplexml_load_file("http://services.getpos.de/ip2loc.asmx/GetIPPosition?UserName=YOUR_USERNAME&#038;Password=YOUR_PASSWORD&#038;IPAddress=".$this->ip);<br />
	}</p>
<p>	public function findGeo() {<br />
		if(!$this->xml_data) {<br />
			$this->error = "service unavailable";<br />
			return false;<br />
		}<br />
		$this->xml = new SimpleXMLElement($this->xml_data);<br />
		$this->RecurseXML($this->xml,$this->geo);<br />
	}</p>
<p>	private function RecurseXML($xml,$vals,$parent=""){<br />
		$child_count = 0;<br />
		foreach($xml as $key=>$value) {<br />
			$child_count++;<br />
			$k = ($parent == "") ? (string)$key : $parent.".".(string)$key;</p>
<p>			if($this->RecurseXML($value,$this->geo,$k) == 0)<br />
				$this->geo[$k] = (string)$value;<br />
		}<br />
		return $child_count;<br />
	}<br />
}<br />
[/source]<br />
We define IP address, XML data and an instance of SimpleXMLObject as private properties and our error handler and actual geo data as public properties. In our constructor we pass IP address and we use simplexml_load_file function to interpret the XML file into an object. Obviously you need to replace YOUR_USERNAME and YOUR_PASSWORD with the information you will use to register at getpos.de. In findGeo() method we first check, if the service is available, and if not we show error message. If our service is available though, we instantiate SimpleXMLObject with the XML data. We than use RecurseXML() method to translate our object into a suitable array. The credit for RecurseXML() goes to transglobe at gmx dot de. I found this function in the comments section of <a href="http://de3.php.net/manual/en/function.simplexml-element-children.php" target="_blank" title="SimpleXML element children">PHP manual</a> and changed it a little bit to suit our needs.</p>
<p>Let's take a closer look, how to use our class:<br />
[source:php]<br />
include("geolocation.class.php");<br />
$geo = new Geolocation($_SERVER['REMOTE_ADDR']);</p>
<p>$geo->findGeo();<br />
echo (isset($geo->error)) ? $geo->error : '';</p>
<p>echo "geo['returncode']:".$geo->geo['returncode']."<br />";<br />
echo "geo['country.code']:".$geo->geo['country.code']."<br />";<br />
echo "geo['country.name']:".$geo->geo['country.name']."<br />";<br />
echo "geo['region.code']:".$geo->geo['region.code']."<br />";<br />
echo "geo['region.name']:".$geo->geo['region.name']."<br />";<br />
echo "geo['city']:".$geo->geo['city']."<br />";<br />
echo "geo['position.longitude']:".$geo->geo['position.longitude']."<br />";<br />
echo "geo['position.latitude']:".$geo->geo['position.latitude']."<br />";<br />
echo "geo['returncode']:".$geo->geo['returncode']."<br />";<br />
[/source]<br />
We include our class and instantiate it with the remote address of a visitor. We than call findGeo() method, which will translate the IP address for us. We can than echo our results or just use them to display relevant information for our users. Obviously, if a user is connected through a proxy or some AOL users in Europe, who are connected through American servers, this class wont retrieve correct information. You need to take into consideration though, that about 65% of your visitors will get correct content. I hope it is clear enough. If you encounter any problems with this class please let me know. As mentioned before, it would be also nice if you could give me any feedback about the accuracy of this service.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/class-for-retrieving-region-city-and-geolocation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>User registration class &#8211; simple authorization of user</title>
		<link>http://www.designing4u.de/2008/05/registration-class-simple-authorization-of-user/</link>
		<comments>http://www.designing4u.de/2008/05/registration-class-simple-authorization-of-user/#comments</comments>
		<pubDate>Fri, 16 May 2008 13:23:32 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[authorization]]></category>
		<category><![CDATA[authorization class]]></category>
		<category><![CDATA[object oriented programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[registration]]></category>
		<category><![CDATA[registration class]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=14</guid>
		<description><![CDATA[This days almost any site is providing the users a simple registration to provide certain services only for registered users. This way you can collect their e-mails and keep in touch with them. If you planing on developing a social platform or just web 2.0 project you need to authorize your users and ask them [...]]]></description>
			<content:encoded><![CDATA[<p>This days almost any site is providing the users a simple registration to provide certain services only for registered users. This way you can collect their e-mails and keep in touch with them. If you planing on developing a social platform or just web 2.0 project you need to authorize your users and ask them to provide a valid e-mail address. In this simple example I will show you, how you can do that using this simple class.<br />
<span id="more-14"></span><br />
This example doesn't provide out of box solution for your site and you need to have at least a little bit knowledge in object oriented programming to adjust it to your needs. In this example I wont use a configuration file and the configuration will be done in constructor method, however it is highly recommended that you create this file to keep maintaining your project fast and easy.</p>
<p>Let's start with defining our class, some variables and the constructor.<br />
[source:php]<br />
class Register {</p>
<p>	//@string name of table, where the information will be stored<br />
	private $table_name;<br />
	//@array holds user input<br />
	private $input;<br />
	//@string subject of confirmation email<br />
	private $mail_subject;<br />
	//@string body of confirmation email<br />
	private $mail_body;<br />
	//@bool html mail true/false<br />
	private $mail_html;<br />
	//@array displays error messages<br />
	public $error;</p>
<p>	function __construct() {<br />
		$con = mysql_connect('localhost','root','pass') or die(mysql_error());<br />
		if($con) {<br />
			mysql_select_db('test',$con);<br />
		}<br />
		$this->table_name = "test_user";<br />
		$this->mail_subject = "confirmation mail";<br />
		$this->mail_body = "Here comes the text of confirmation e-mail. Click on this link %s to confirm your email.";<br />
		$this->mail_html = true;<br />
	}<br />
}<br />
[/source]<br />
$this->table_name is a string property, which holds information about MySQL table, in which we will save our new registered user. $this->input is an array, in which we will save the user input. As you will see later, we do that, to make the user input safe for MySQL INSERT function. $this->mail_subject and $this->mail_body are two properties, which will be responsible for holding the information we will later send to our new user after successful registration. $this->mail_html is a boolean property, which tells our script to either send HTML or plain text e-mail after successful registration. $this->error property is an array, which we will use to display all errors to the user, which our script will generate. In our constructor method we initialize data base connection and all the properties we will later use in our class. Let's move farther to our registration method.<br />
[source:php]<br />
public function registerUser() {<br />
	foreach($_POST as $k=>$v) {<br />
		$this->input[$k] = mysql_real_escape_string($v);<br />
	}<br />
	//Check users input<br />
	if($this->checkInput()) {<br />
		//Check, if username exists already<br />
		if($this->checkUser('username')) {<br />
			//Check if email exists already<br />
			if($this->checkUser('email')) {<br />
				//save user in database<br />
				if($this->insertUser()) {<br />
					//send mail with confirmation link<br />
					if($this->sendMail()) {<br />
						$this->success = "Thanks for registration. Check your e-mail for further details.";<br />
						return true;<br />
					} else {<br />
						//revert changes in database<br />
						$this->deleteUser();<br />
						$this->error[] = "something went wrong, please try again later send mail";<br />
						return false;<br />
					}<br />
				} else {<br />
					$this->error[] = "something went wrong, please try again later insert user";<br />
					return false;<br />
				}<br />
			} else {<br />
				$this->error[] = "email exists";<br />
				return false;<br />
			}<br />
		} else {<br />
			$this->error[] = "username exists";<br />
			return false;<br />
		}<br />
	} else {<br />
		return false;<br />
	}<br />
}<br />
[/source]<br />
We define this method as public because we will call it after our user clicks the submit button. We than loop through the $_POST variable and make the input safe against MySQL injection. First we check, if all the input is valid and meets our expectations according to password length, valid email etc. After that we provide a user check against the information saved in our database to determine, if an user with provided username or e-mail exists in our database. If it does we display corresponding error message. If our script passes this check we save new user in our database and send an e-mail with activation link. If our script return true after sending an activation e-mail we display a success message for our user with prompt to check his/hers e-mail and activate account. Simple logic, you can probably find in each user authorization. Let's take a closer look at corresponding methods, which I just mentioned:<br />
[source:php]<br />
private function checkInput() {<br />
	if(strlen($this->input['username']) < 5 || $this->input['username'] == '') {<br />
		$this->error[] = "username too short";<br />
	}<br />
	if(strlen($this->input['username']) > 16) {<br />
		$this->error[] = "username too long";<br />
	}<br />
	if($this->input['password'] == '' || $this->input['password1'] == '') {<br />
		$this->error[] = "you need to provide a password";<br />
	}<br />
	if($this->input['password'] != $this->input['password1']) {<br />
		$this->error[] = "password mismatch";<br />
	} else {<br />
		$this->input['password'] = md5($this->input['password']);<br />
	}<br />
	return (is_array($this->error) &#038;& !empty($this->error)) ? false : true;<br />
}<br />
[/source]<br />
This method validates user input. It is just a simple example and it doesn't validate the e-mail address. You should feel free to add any other validations you can imagine to this method. In the last step we check, if provided passwords match and if they do, we encode the password with md5() function. We do that to ensure the user, that his or hers data wont be abused later, when we use it for example to save it in session variables and check against database information to authenticate the user. If $this->error is an array and it's not empty, it means user input is not valid and we return false, otherwise true.<br />
[source:php]<br />
private function checkUser($case='') {<br />
	switch($case) {<br />
		case 'username':<br />
			$sql = mysql_query("SELECT COUNT(*) AS exist FROM ".$this->table_name." WHERE username='".$this->input['username']."';");<br />
			break;<br />
		case 'email':<br />
			$sql = mysql_query("SELECT COUNT(*) AS exist FROM ".$this->table_name." WHERE email='".$this->input['email']."';");<br />
			break;<br />
		default:<br />
			break;<br />
	}<br />
	return (mysql_result($sql,0,'exist') > 0) ? false : true;<br />
}<br />
[/source]<br />
In this method we check, if username or password exists in database. If it does we return false, otherwise true. At this point I should probably mention that AJAX gives you possibility to perform this validation on client side. Server side validation is just another check to ensure that provided data is really that, what we expect to save in our database. If you want to use this class in your AJAX request to perform checks on user input, you would have to change most of the methods to public and probably write another method which will set user input you will later use to perform those checks.<br />
[source:php]<br />
private function insertUser() {<br />
	$sql = mysql_query("INSERT INTO ".$this->table_name." (id, username, password, email, confirmed) VALUES (NULL,'".$this->input['username']."','".$this->input['password']."','".$this->input['email']."','0');") or die(mysql_error());<br />
	return ($sql) ? true : false;<br />
}</p>
<p>private function deleteUser() {<br />
	$sql = mysql_query("DELETE FROM ".$this->table_name." WHERE username='".$this->input['username']."' AND email='".$this->input['email']."';");<br />
}<br />
[/source]<br />
InsertUser and deleteUser are just simple methods to either save of delete a user from the database.<br />
[source:php]<br />
private function sendMail() {<br />
	if($this->email_html) {<br />
		$header = 'MIME-Version: 1.0' . "\r\n";<br />
		$header.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";<br />
		$header.= 'From: "yourmail@domain.com"<yourmail@domain.com>' . "\r\n";<br />
	} else {<br />
		$header =  'From:yourmail@domain.com';<br />
	}</p>
<p>	$body = sprintf($this->mail_body,"www.yourdomain.com?activation=youractivationcode");</p>
<p>	return (@mail($this->input['email'], $this->mail_subject, $body, $header)) ? true : false;<br />
}<br />
[/source]<br />
The last method replaces %s with the confirmation link, sends a confirmation mail and returns true on success and false in case of any errors. That's all. As I already mentioned this class doesn't provide a ready solution for your internet site and it should be adjusted to your needs. It's just a schema you can always start with, when you writing applications, which provide user registration. At the end lets take a closer look, how you should use this class.<br />
[source:php]<br />
if(isset($_POST['submit']) &#038;& $_POST['submit'] == 'register') {<br />
	include('register.class.php');<br />
	$register = new Register;<br />
	if($register->registerUser()) {<br />
		echo (isset($register->success)) ? $register->success : '';<br />
	} else {<br />
		foreach($register->error as $error) {<br />
			echo $error."<br />";<br />
		}<br />
		include('form.php');<br />
	}<br />
} else {<br />
	include('form.php');<br />
}<br />
[/source]<br />
We include and instantiate our class only in case, when our clicks a submit button. We then call the registerUser method and display success or error message depending on the case. You might also provide a redirection after successful registration to prevent the users from reloading the page. However our class would retrieve an error message saying that username already exists, it is just a nice way to do the things. And here the code of form.php in case you need it:<br />
[source:html]</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post"><br />
	<label for="username">Username</label></p>
<input type="text" name="username" id="username" value="<? echo (isset($_POST['username'])) ? $_POST['username'] : '';?>" /><br />
	<label for="email">E-Mail</label></p>
<input type="text" name="email" id="email" value="<? echo (isset($_POST['email'])) ? $_POST['email'] : '';?>" /><br />
	<label for="password">Password</label></p>
<input type="password" name="password" id="password" />
	<label for="password1">Repeat password</label></p>
<input type="password" name="password1" id="password1" />
	<label>&nbsp;</label></p>
<input type="submit" name="submit" value="register" />
</form>
<p>[/source]<br />
And the table structure:<br />
[source:sql]<br />
CREATE TABLE IF NOT EXISTS `test_user` (<br />
  `id` int(11) NOT NULL auto_increment,<br />
  `username` varchar(50) collate latin1_general_ci NOT NULL,<br />
  `password` varchar(32) collate latin1_general_ci NOT NULL,<br />
  `email` varchar(255) collate latin1_general_ci NOT NULL,<br />
  `confirmed` enum('0','1') collate latin1_general_ci NOT NULL,<br />
  PRIMARY KEY  (`id`),<br />
  UNIQUE KEY `username` (`username`)<br />
)<br />
[/source]<br />
I'm waiting for your comments about this one. Cheers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/registration-class-simple-authorization-of-user/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class for picture upload with fix width and height</title>
		<link>http://www.designing4u.de/2008/05/class-for-picture-upload-with-fix-width-and-height/</link>
		<comments>http://www.designing4u.de/2008/05/class-for-picture-upload-with-fix-width-and-height/#comments</comments>
		<pubDate>Wed, 14 May 2008 12:06:52 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[image upload]]></category>
		<category><![CDATA[object oriented programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=9</guid>
		<description><![CDATA[For one of my web 2.0 projects I had to develop a solution for users to upload their images with fixed width and height. Basically my customer wanted to display profile images on the start page, but the requirement was to display each of them in a div container with fixed width and height. Because [...]]]></description>
			<content:encoded><![CDATA[<p>For one of my web 2.0 projects I had to develop a solution for users to upload their images with fixed width and height. Basically my customer wanted to display profile images on the start page, but the requirement was to display each of them in a div container with fixed width and height. Because I'm not a CSS guru and I didn't want to set negative background position to display only parts of the images or because of the problems, which CSS has with different browsers, I came up with a solution, which does that pretty good. My class has a simple configuration and according to the width or height of an image uploaded by a user, resizes it to predefined width or height and places it according to the case vertically or horizontally in the center. This description might be confusing right now, but if you read this post to the end I hope you will know, what I meant.<br />
<span id="more-9"></span></p>
<p>(You can find a downloadable archive at the end of this example)</p>
<p>Let's start with defining the class and some configuration variables.<br />
[source:php]<br />
class Upload {<br />
	private $image = array();<br />
	private $image_location;<br />
	private $image_type;<br />
	private $image_height;<br />
	private $image_width;<br />
	private $image_maxheight;<br />
	private $image_maxwidth;<br />
	private $image_box;</p>
<p>	public $error;<br />
	public $success;<br />
}<br />
[/source]<br />
We set all of important variables as private, because we will use them only in the scope of our class. Two public variables will be responsible for displaying the error and success messages. Let's go to our constructor.<br />
[source:php]<br />
function __construct() {<br />
	$this->image_location = 'images/';<br />
	$this->image_height = 60;<br />
	$this->image_width = 60;<br />
	$this->image_maxheight = 1500;<br />
	$this->image_maxwidth = 1500;<br />
	$this->image_box = 60;<br />
}<br />
[/source]<br />
In our constructor method, which will be executed, after we initialize our class, we will define some basic configuration. $this->image_location property will tell our script, where to save the images. Before you start using this class, don't forget to create this directory and give it necessary rights, because otherwise you will get an error message. $this->image_width and $this->image_height tell our script, to which width or height should the image be resized. Both properties should have the same value, which shouldn't be bigger than the value of $this->image_box. Our script will automatically detect, if the picture is horizontal or vertical and will use the right value according to the case. $this->image_box tells our script, which resolution has the div box displayed on the start page. $this->image_maxheight and $this->image_maxwidth is the maximum resolution of the picture, which cannot be exceeded. Let's move to the picture upload.<br />
[source:php]<br />
public function uploadPicture() {<br />
	foreach($_FILES as $file) {<br />
		$this->image['tmp_name'] = $file['tmp_name'];<br />
		$this->image['name'] = $this->image_location.$file['name'];<br />
		$this->image['type'] = $file['type'];<br />
	}<br />
	return ($this->createPicture()) ? true : false;<br />
}<br />
[/source]<br />
In our first public method, which will be called, after a user clicks an upload button, we set some important properties, which we will use later to handle our image upload. $this->image['tmp_name'] will hold for us the temporary location of the file. $this->image['name'] will hold the location, where the uploaded image should be copied, and $this->image['type'] will hold MIME type of the uploaded picture. After we set those properties, we call createPicture method to actually create the pictures for us. Let's take a closer look at two last methods.<br />
[source:php]<br />
private function createPicture() {<br />
	if($this->checkType()) {</p>
<p>		move_uploaded_file($this->image['tmp_name'],$this->image['name']);<br />
		list($width, $height, $type, $attr) = getimagesize($this->image['name']);</p>
<p>		if ($width > $this->image_maxwidth || $height > $this->image_maxheight) {</p>
<p>			$this->error = 'Your picture is too big';<br />
			unlink($this->image['name']);<br />
			return false;</p>
<p>		} else {</p>
<p>			if($height > $width) {<br />
				$this->image_width = round((($this->image_height * $width) / $height));<br />
				$x = round(($this->image_height - $this->image_width) / 2);<br />
				$y = 0;<br />
			} else {<br />
				$this->image_height = round((($this->image_width * $height) / $width));<br />
				$x = 0;<br />
				$y = round(($this->image_width - $this->image_height) / 2);<br />
			}</p>
<p>			if($this->image_type == 'jpg') {<br />
				$galery_image = imagecreatefromjpeg($this->image['name']);<br />
			} elseif($this->image_type == 'gif') {<br />
				$galery_image = imagecreatefromgif($this->image['name']);<br />
			} else {<br />
				$galery_image = imagecreatefrompng($this->image['name']);<br />
			} </p>
<p>			$image = imagecreatetruecolor($this->image_box, $this->image_box);<br />
			$background = imagecolorallocate($image, 255, 0, 0);<br />
			imagefill($image, 0, 0, $background);</p>
<p>			imagecopyresampled($image, $galery_image, $x, $y, 0, 0, $this->image_width, $this->image_height, $width, $height);</p>
<p>			if($this->image_type == 'jpg') {<br />
				imagejpeg($image, $this->image['name']);<br />
			} elseif($this->image_type == 'gif') {<br />
				imagegif($image, $this->image['name']);<br />
			} else {<br />
				imagepng($image, $this->image['name']);<br />
			}</p>
<p>			chmod($this->image['name'], 0644);<br />
			imagedestroy($image);</p>
<p>			$this->success = 'Image uploaded successfully';<br />
			return true;<br />
		}<br />
	} else {<br />
		return false;<br />
	}<br />
}</p>
<p>private function checkType() {<br />
	switch ($this->image['type']) {<br />
		case 'image/jpeg':<br />
		case 'image/jpg':<br />
		case 'image/pjpeg':<br />
			$this->image_type = "jpg";<br />
			return true;<br />
			break;<br />
		case 'image/gif':<br />
			$this->image_type = "gif";<br />
			return true;<br />
			break;<br />
		case 'image/png':<br />
		case 'image/x-png':<br />
			$this->image_type = "png";<br />
			return true;<br />
			break;<br />
		default:<br />
			$this->error = "Wrong file type";<br />
			return false;<br />
			break;<br />
	}<br />
}<br />
[/source]<br />
Because this two method are pretty long I will split them into smaller pieces and explain step by step. We set both of the methods as private, because we will use them only in the scope of our class. It the first method, createPicture, we first check, if the MIME type of the file uploaded by a user is supported by our script. If it is we set $this->image_type and return true, if not we display error message and return false.<br />
[source:php]<br />
move_uploaded_file($this->image['tmp_name'],$this->image['name']);<br />
list($width, $height, $type, $attr) = getimagesize($this->image['name']);</p>
<p>if ($width > $this->image_maxwidth || $height > $this->image_maxheight) {</p>
<p>	$this->error = 'Your picture is too big';<br />
	unlink($this->image['name']);<br />
	return false;</p>
<p>}<br />
[/source]<br />
In this part we actually move the uploaded file from the temporary location to the location we defined earlier in our script. After that we list the width and height from our new uploaded image and check, if they are not bigger, then the values we defined in our constructor. If they are, we display an error message, if not we proceed.<br />
[source:php]<br />
if($height > $width) {<br />
	$this->image_width = round((($this->image_height * $width) / $height));<br />
	$x = round(($this->image_height - $this->image_width) / 2);<br />
	$y = 0;<br />
} else {<br />
	$this->image_height = round((($this->image_width * $height) / $width));<br />
	$x = 0;<br />
	$y = round(($this->image_width - $this->image_height) / 2);<br />
}<br />
[/source]<br />
If the resolution of the image is correct, we check, if the image is vertical or horizontal. According to the case, we calculate new width or new height and the offset, which we will later use to place the image in the middle of our box.<br />
[source:php]<br />
if($this->image_type == 'jpg') {<br />
	$galery_image = imagecreatefromjpeg($this->image['name']);<br />
} elseif($this->image_type == 'gif') {<br />
	$galery_image = imagecreatefromgif($this->image['name']);<br />
} else {<br />
	$galery_image = imagecreatefrompng($this->image['name']);<br />
} </p>
<p>$image = imagecreatetruecolor($this->image_box, $this->image_box);<br />
$background = imagecolorallocate($image, 255, 0, 0);<br />
imagefill($image, 0, 0, $background);</p>
<p>imagecopyresampled($image, $galery_image, $x, $y, 0, 0, $this->image_width, $this->image_height, $width, $height);<br />
[/source]<br />
In this step we return an image identifier according to the MIME type of the picture, which has been uploaded and create an empty image, in which we will place the uploaded picture. You can change the $background and use your own color for the background using the RGB values. Imagecopyresampled uses our identifier and the "picture box" we creted with predefined values and creates a ready image.<br />
[source:php]<br />
if($this->image_type == 'jpg') {<br />
	imagejpeg($image, $this->image['name']);<br />
} elseif($this->image_type == 'gif') {<br />
	imagegif($image, $this->image['name']);<br />
} else {<br />
	imagepng($image, $this->image['name']);<br />
}</p>
<p>chmod($this->image['name'], 0644);<br />
imagedestroy($image);</p>
<p>$this->success = 'Image uploaded successfully';<br />
return true;<br />
[/source]<br />
In the last part, according to MIME type, we output our new created picture to the file, change the permissions and destroy the original image. </p>
<p>Let's take a closer look, how you should use this class.<br />
[source:php]<br />
<?php<br />
if(isset($_POST['submit']) &#038;& $_POST['submit'] == 'upload') {<br />
	include('upload.class.php');<br />
	$upload = new Upload;<br />
	if($upload->uploadPicture()) {<br />
		echo (isset($upload->success)) ? $upload->success : '';<br />
		echo '<br /><a href="upload.php">go back</a>';<br />
	} else {<br />
		echo (isset($upload->error)) ? $upload->error : '';<br />
		echo '<br /><a href="upload.php">go back</a>';<br />
	}<br />
} else {<br />
?></p>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="picture" />
<input type="submit" name="submit" value="upload" />
</form>
<p><?php<br />
}<br />
?><br />
[/source]<br />
I think, this code doesn't need a detailed explanation. One hint only: don't forget to set the enctype of the form to multipart/form-data, because otherwise the $_FILES variable wont be initialized. If user clicks on upload button, we initialize our class and use the uploadPicture method to create our new picture. If this method returns true we display success message, if not the error message.</p>
<p>That's all. I hope it was clear enough for you, what I was trying to accomplish in this example. You can change it in probably hundreds different ways and adjust it to your own needs. Feel free to do that. </p>
<p>You can download all of the files <a href="http://www.designing4u.de/examples/image-upload-class/image-upload-class.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/class-for-picture-upload-with-fix-width-and-height/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Geolocation class &#8211; retrieving longitude and latitude using google maps</title>
		<link>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/</link>
		<comments>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/#comments</comments>
		<pubDate>Tue, 06 May 2008 10:58:43 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=7</guid>
		<description><![CDATA[A while ago I had to prepare a map for a customer. He wanted to be able to use text files with addresses and display them on the map. To be able to do that I had to find a service, which will convert a regular address into geographical longitude and latitude. What would be [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I had to prepare a map for a customer. He wanted to be able to use text files with addresses and display them on the map. To be able to do that I had to find a service, which will convert a regular address into geographical longitude and latitude. What would be better than google maps to do that really simple task? I researched a little bit on google and found a really nice way, how to accomplish that.<br />
<span id="more-7"></span><br />
On Tim Shower's homepage I have found a really nice <a href="http://www.timshowers.com/2008/08/php-geocoding-tutorial-with-the-google-maps-api-part-one/" target="_blank">php geocoding tutorial</a>, which I used to build my solution. For our purposes we will use client for URL (cURL) to retrieve the longitude and latitude of our places. Let's define our class first.<br />
[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>}<br />
[/source]<br />
We define private array url, in which we will store URLs prepared for cURL request. Let's build a contructor.<br />
[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>	function __construct($file) {<br />
		$gmapkey = "YOURKEY";<br />
		$address = file($file);<br />
		foreach($address as $addres) {<br />
			$this->url[] = "http://maps.google.com/maps/geo?q=".urlencode($addres)."&#038;output=csv&#038;key=".$gmapkey;<br />
		}<br />
	}<br />
}<br />
[/source]<br />
You need to replace $gmapkey with the key you can sign for at google. Our __constructor reads the text file and saves each line in an array. We then loop through this array and initialize URLs.</p>
<p>In the next method we will loop through the addresses to retrieve the geolocation.</p>
<p>[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>	function __construct($file) {<br />
		$gmapkey = "YOURKEY";<br />
		$address = file($file);<br />
		foreach($address as $addres) {<br />
			$this->url[] = "http://maps.google.com/maps/geo?q=".urlencode($addres)."&#038;output=csv&#038;key=".$gmapkey;<br />
		}<br />
	}<br />
	public function getLatLng() {<br />
		$i=1;<br />
		foreach($this->url as $urls) {<br />
			$cinit = curl_init();<br />
			curl_setopt($cinit, CURLOPT_URL, $urls);<br />
			curl_setopt($cinit, CURLOPT_HEADER,0);<br />
			curl_setopt($cinit, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);<br />
			curl_setopt($cinit, CURLOPT_FOLLOWLOCATION, 1);<br />
			curl_setopt($cinit, CURLOPT_RETURNTRANSFER, 1);<br />
			$response = curl_exec($cinit);<br />
			curl_close($cinit);</p>
<p>			if(strstr($data,"200")) {<br />
				$data = explode(",",$data);<br />
				echo $i.": ";<br />
				$zoom = $data[1];<br />
				echo $lat = $data[2];<br />
				echo $lon = $data[3]."<br />";<br />
			}</p>
<p>			/*<br />
			$sql = "INSERT INTO..."<br />
			$res = mysql_query($sql);<br />
			echo ($res) ? $i.": lat:".$latitude." lon: ".$longitdue." -- OK<br />" : $i.": ERROR<br />";<br />
			*/<br />
			$i++;<br />
		}<br />
	}<br />
}<br />
[/source]<br />
You can either echo the results or just save them in your MySQL database. You can also make multidimensional array and save some extra information about the certain place. </p>
<p>Let's take a look at how you should use this class.<br />
[source:php]<br />
include('geolocation.class.php');<br />
$geo = new Geolocation('address.txt');<br />
$geo->getLatLng();<br />
[/source]<br />
Basically you need to include the class, initialize it with the name of the file you want to use and getLatLng will do the magic for you. This class is pretty old and you may encounter one problem using it. After 10th request I started to receive the same geolocation for different places. What you can do is to put the foreach loop to sleep for 2 seconds and it should be fine. That's why I actually use iterator in this function. You basically need to add this line right after the loop starts.<br />
[source:php]<br />
if($i%10 == 0) sleep(2);<br />
[/source]</p>
<p>Here is the content of the text file:<br />
[source:html]<br />
Berlin, Dieffenbachstr. 38<br />
Berlin, Cuvrystr. 32<br />
Berlin, Lasdehner Str. 30<br />
[/source]<br />
Basically one address per line. That would be all, have fun with playing and extending this class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Login and authentication class</title>
		<link>http://www.designing4u.de/2008/05/authentication-class/</link>
		<comments>http://www.designing4u.de/2008/05/authentication-class/#comments</comments>
		<pubDate>Sat, 03 May 2008 09:50:01 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[Authentication Class]]></category>
		<category><![CDATA[object oriented programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=5</guid>
		<description><![CDATA[I lately got maybe not the best but really helpful book about object oriented programming in php5 and I decided to write my own login and authentication class. In this post I will give you a short description, how I manage the authentication of users for restricted pages. This class will use information stored in [...]]]></description>
			<content:encoded><![CDATA[<p>I lately got maybe not the best but really helpful book about object oriented programming in php5 and I decided to write my own login and authentication class. In this post I will give you a short description, how I manage the authentication of users for restricted pages. This class will use information stored in MySQL table but I will comment it out for demo purposes. I assume that you know how to use MySQL database and you will be able to create the tables by yourself. If not please let me know and I will post also the table structure.<br />
<span id="more-5"></span></p>
<p><a title="demo" href="/examples/login-authentication-class/login.php" target="_blank">Here</a> you can find a working demo of the class I will introduce in this example</p>
<p>Let's start with defining the class and the variables we will use in the scope of this class. We will give it a Login name because it has to handle a user, who wants to login to view certain content, which is available only for registered users.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Login <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$user_name</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$user_pass</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$user_id</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$user_access</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$login_error</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$userpass</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_pass</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$userpass</span> <span style="color: #339933;">:</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userpass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We declared the properties of this class as private, because we will use them only in scope of it and we will access them only internally from this class. The only public property will be responsible for error handling. We use __construct function to assign values to the properties we just declared, when the class will be initialized. We assume that password stored in database was encrypted using md5() function. As the first step we will declare a method, which will handle user login.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loginUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location:index.php?page=overview&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error_login</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'wrong username or password'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We set the loginUser function to public so we can access it after we initialize our class. We check, if the user exists in our database. If the information in database matches the user input, we use setUser function to store the information about the user, which we can use later to authenticate or to get user content availably only for that certain user.</p>
<p>As second step we will declare a private method checkUser, which will check, if a the information provided by user exists in database. This will be a simple MySQL query and the method will return true in case user exists and false in case of any mismatch.</p>
<p>(In this place you might comment out MySQL check and just add hard coded check for certain string. I add this part as a commentary.)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/*
    return ($this-&gt;user_name == 'demo' &amp;&amp; $this-&gt;user_pass == 'demo') ? true : false;
    */</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(*) AS exist FROM usertable WHERE username='<span style="color: #009933; font-weight: bold;">%s</span>' AND pass='<span style="color: #009933; font-weight: bold;">%s</span>' AND confirmed='1';&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>user_name<span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>user_pass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'exist'</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: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the next step we will define a method, which will set our user as a session array, where we will store all the important information about our user.</p>
<p>(Again, if you don't want to use database, you can define default user id.)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/*
    $this-&gt;user_ip = 1;
    */</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM miss_user WHERE username='<span style="color: #009933; font-weight: bold;">%s</span>' AND pass='<span style="color: #009933; font-weight: bold;">%s</span>';&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_name</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">user_id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_name'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_pass'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_pass</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_ip'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userip'</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can notice we saved user id, user name, user password and user remote address in an array, which we will later use to authenticate user. We declared this method as private again, because we will access it only in the scope of our class. Additionally we save a cookie with the IP address of the user to perform a check against session stealing. Even if someone would try to steal the session by just randomly typing it into the URL address, the authentication will be failed because it wont match the IP in the cookie.</p>
<p>In this step we will actually perform the authentication of a user.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> authenticateUser<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</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;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkUser</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: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userip'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_ip'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>First we check, if the session array is an array at all and if it's not empty. After that we perform checkUser again to compare the session array with the database information. At the end we do the last check, if the IP address saved in the cookie matches the address saved in session array. In my case the method authenticateUser returns true on success and false in case of failure but you can do whatever you want after a user passes authentication. We save id of a user because it might be useful later in getting the information relevant only for this certain user or from two MySQL tables, which are related to each other by an user ip.</p>
<p>The only thing, which we are missing right now is the log out method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> logOut<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userip'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location:login.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I think this is self explanatory. We unset the sessionn, destroy the cookie and redirect the user to the login page. That would be all. Now i will present a simple usage of this class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.login.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loginUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'logout'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We start a session and include our class before we send any headers. We use error_reporting only for develpment reasons and you can erase this line or decrees the level of error reporting if you want. We perform a check, if user submit the login form. If yes we initialize the Login class with the information provided by user. If the input validates we log in user and redirect him to welcome page (as stated in the class). If the user didn't submit the login information we perform another check, if the session array is set and if it's not empty we initialize our class with the information saved in our session array. If the user clicks on the log out button, we use logOut method to destroy all the information about the user. After that we can send some headers.</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;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticateUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'logged'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;a href=&quot;login.php?page=logout&quot;&gt;Logout&lt;/a&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error_login</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;form action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'PHP_SELF'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; method=&quot;post&quot;&gt;
username:&lt;input name=&quot;username&quot; type=&quot;text&quot; /&gt;
password:&lt;input name=&quot;pass&quot; type=&quot;text&quot; /&gt; &lt;input name=&quot;submit&quot; type=&quot;submit&quot; value=&quot;login&quot; /&gt;
&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>In this part we perform actual authentication of the user and display information for authenticated users and if there is any error we display the error message</p>
<p>Wow I did it. It's my first tutorial kind of example thing:) If you want to download the files with source code for this example you can do it <a href="/examples/login-authentication-class/login-authentication-class.zip">here</a>. I'm waiting for your feedback right now. If you went that far you can write at least couple words:) Going back to work now...</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/authentication-class/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
