<?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>Not A n00b &#187; Tutorials</title>
	<atom:link href="http://notan00b.com/category/tuts/feed/" rel="self" type="application/rss+xml" />
	<link>http://notan00b.com</link>
	<description>Tutorials, Scripts, and Rants</description>
	<lastBuildDate>Wed, 12 May 2010 08:08:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery Slideshow</title>
		<link>http://notan00b.com/2010/05/jquery-slideshow/</link>
		<comments>http://notan00b.com/2010/05/jquery-slideshow/#comments</comments>
		<pubDate>Wed, 12 May 2010 08:08:35 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=307</guid>
		<description><![CDATA[A simple jQuery function that will enable you to create nice looking slide shows.]]></description>
			<content:encoded><![CDATA[<p>I programmed this the other day and tried to make it as user friendly as possible.  Let me post the code and then I can explain it a little.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE HTML <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;jquery-1.4.2.min.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ready<span style="color: #009900;">&#40;</span><span style="color: #000000; 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: #000000; font-weight: bold;">var</span> speed<span style="color: #339933;">=</span><span style="color: #cc66cc;">2500</span><span style="color: #339933;">;</span>
   slideShow<span style="color: #009900;">&#40;</span>speed<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: #000000; font-weight: bold;">function</span> slideShow<span style="color: #009900;">&#40;</span>speed<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>   
   <span style="color: #000000; font-weight: bold;">var</span> width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.slideshow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>width<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">var</span> height<span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.slideshow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>height<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">var</span> images<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#slideshow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>html<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">var</span> overText<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#overText&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>html<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: #0000ff;">&quot;#slideshow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>css<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;width&quot;</span><span style="color: #339933;">:</span>width<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;px&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;height&quot;</span><span style="color: #339933;">:</span>height<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;px&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;position&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;relative&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#slideshow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>html<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;img src='#' id='imagea' height='&quot;</span><span style="color: #339933;">+</span>height<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;' width='&quot;</span><span style="color: #339933;">+</span>width<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;'&gt;&lt;img src='13.JPG' id='imageb'  height='&quot;</span><span style="color: #339933;">+</span>height<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;' width='&quot;</span><span style="color: #339933;">+</span>width<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;'&gt;&lt;div id='hiddenimages'&gt;&lt;/div&gt;&lt;div id='overtext'&gt;&quot;</span><span style="color: #339933;">+</span>overText<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#hiddenimages&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>hide<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>html<span style="color: #009900;">&#40;</span>images<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imagea #imageb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>hide<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: #0000ff;">&quot;#overtext&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>css<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;position&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;absolute&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;right&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;1px&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;bottom&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;1px&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;z-index&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;5&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imagea&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>css<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;position&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;absolute&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;top&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;1px&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;1px&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;z-index&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imageb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>css<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;position&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;absolute&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;top&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;1px&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;1px&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;z-index&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;4&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.slideshow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">var</span> old<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imageb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>attr<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000000; font-weight: bold;">var</span> newsrc<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span>this<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>attr<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       setTimeout<span style="color: #009900;">&#40;</span><span style="color: #000000; 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: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imagea&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>attr<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;src&quot;</span><span style="color: #339933;">,</span>$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imageb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>attr<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imagea&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>show<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: #0000ff;">&quot;#imageb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>hide<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: #0000ff;">&quot;#imageb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>attr<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;src&quot;</span><span style="color: #339933;">,</span>newsrc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imageb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>fadeIn<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#imagea&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>fadeOut<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> index<span style="color: #339933;">*</span>speed<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: #000000; font-weight: bold;">&lt;/script&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;3.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;4.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;5.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;6.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;7.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;12.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;13.JPG&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slideshow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;overText&quot;</span><span style="color: #339933;">&gt;&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;caption.png&quot;</span><span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p>So basically, copy and paste everything in the head portion.  You can set the speed to what ever you would like.<br />
In the body, create a div with the id of &#8220;slideshow&#8221;, then add your images inside the div.  Add the class &#8220;slideshow&#8221; to all the pictures you add.  You may want to keep them the same size as jQuery does not resize them for you.<br />
Inside the &#8220;slideshow&#8221; div, add a div with the id &#8220;overText&#8221;.  The text, or image will be displayed at the bottom right corner of the slideshow on top of them all.  Format the text or image however you would like.<br />
Final notes:<br />
You can add as many pictures as you want, but 100 pictures might take a while to load.<br />
It only loops once, and doesn&#8217;t loop through them again.<br />
You can leave the &#8220;overText&#8221; div empty and no caption will be displayed on the slideshow.<br />
You can change the speed of the slideshow by increasing or decreasing the speed.</p>
<p>Have fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2010/05/jquery-slideshow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a &#8220;frilly&#8221; border</title>
		<link>http://notan00b.com/2010/02/making-a-frilly-border/</link>
		<comments>http://notan00b.com/2010/02/making-a-frilly-border/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 15:59:28 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[blogger]]></category>
		<category><![CDATA[blogspot]]></category>
		<category><![CDATA[border]]></category>
		<category><![CDATA[caption]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[frilly]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[header]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=245</guid>
		<description><![CDATA[How to make a border on the top with "frills".  One example: http://gkconover.blogspot.com/]]></description>
			<content:encoded><![CDATA[<div id="attachment_246" class="wp-caption aligncenter" style="width: 310px"><a href="http://notan00b.com/wordpress/wp-content/uploads/2010/02/borderexample.png"><img class="size-medium wp-image-246" title="Example" src="http://notan00b.com/wordpress/wp-content/uploads/2010/02/borderexample-300x23.png" alt="Frilly border example" width="300" height="23" /></a><p class="wp-caption-text">Frilly border example - click to view</p></div>
<p>This is what we are working on today.  There are several ways to do this, some are easier than other.  The easiest way (which we will not be doing) is to make an entire header image with a border that looks like this.  The only problem I have found is that you have to match up the edges of the blog perfectly if you want to make the border work.  Also, you would need a decent graphics program to do anything that is decent looking (paint won&#8217;t cut it).  Since a lot of people don&#8217;t have the $400 Adobe Photoshop CS4, we have come up with a different hack to get it to display.</p>
<p>First off, we need the image of a single border.  I have gone ahead and created it and uploaded it here: http://www.notan00b.com/border.png</p>
<p>Second, we need to edit some serious HTML:</p>
<p>Copy and paste this above  &#8220;#outer-wrapper{&#8221; :</p>
<p>#topborder{<br />
background-image:url(&#8216;http://www.notan00b.com/border.png&#8217;);<br />
background-repeat:repeat-x;<br />
background-position:bottom  left;<br />
width: 960px;<br />
height: 20px;<br />
}<br />
#contentALL{<br />
background:  #fff;<br />
padding:10px;<br />
padding-top:30px;<br />
}</p>
<p>Change #outer-wrapper to look like this:<br />
#outer-wrapper {<br />
width: 960px;<br />
margin:60px  auto;<br />
text-align:left;<br />
font: normal normal 90% Georgia, Times,  serif;<br />
}</p>
<p>Go to the bottom,  and under &#8220;&lt;div id=&#8217;outer-wrapper&#8217;&gt;&#8221;:<br />
&lt;div id=&#8217;topborder&#8217;/&gt;<br />
&lt;div id=&#8217;contentALL&#8217;&gt;</p>
<p>At the bottom edit this  &#8220;&lt;/div&gt;&lt;/div&gt; &lt;!&#8211; end outer-wrapper &#8211;&gt;&#8221;:<br />
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;  &lt;!&#8211; end outer-wrapper &#8211;&gt;</p>
<p>That&#8217;s it!  Save it and you will have your border.  This is a specific case where the blog content was originally 950px wide.  I changed it to 960px to get the full frill instead of half of one (border.png is 20px wide).  Let me know if it doesn&#8217;t work or if you are experiencing problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2010/02/making-a-frilly-border/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is a BLIND SQL Injection?</title>
		<link>http://notan00b.com/2010/01/what-is-a-blind-sql-injection/</link>
		<comments>http://notan00b.com/2010/01/what-is-a-blind-sql-injection/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 07:34:20 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[1=1]]></category>
		<category><![CDATA[1=2]]></category>
		<category><![CDATA[blind sql]]></category>
		<category><![CDATA[blind sql injection]]></category>
		<category><![CDATA[bsi]]></category>
		<category><![CDATA[get variables]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=241</guid>
		<description><![CDATA[Its been awhile, and I have been pretty busy.  I decided to talk a little bit about a few things I have learned and been working on.  When it comes to website security, most programmers understand what SQL injections are.  The user types something into a form that manipulates the SQL statement [...]]]></description>
			<content:encoded><![CDATA[<p>Its been awhile, and I have been pretty busy.  I decided to talk a little bit about a few things I have learned and been working on.  When it comes to website security, most programmers understand what SQL injections are.  The user types something into a form that manipulates the SQL statement to give them access to certain areas or display the info they want.  Then there are Blind Sql Injections (BSI).  These are much more complicated and considered an advanced penetration technique.  The funny thing is, they are 100% preventable.</p>
<p>So lets start out with the basics, what is it?  It all has to do with the URL bar and <em>get</em> variables.  Let&#8217;s say that my site is &#8220;http://www.mysite.com/&#8221; and I created a script that grabs info out of my database based on it&#8217;s ID in the database.  For example:  &#8220;http://www.mysite.com/index.php?pageid=10&#8243;.  My SQL statement will look like this: &#8220;SELECT * FROM pages WHERE ID=$_GET[id]&#8220;.  Just like a regular SQL Injection, a hacker can submit malicious code using the <em>get</em> variable.</p>
<p>This is where we differ a little from a basic SQL Injection.  In a basic SQL Injection, you simply type <code>' or 'x'='x</code> into a form and it may or may not authenticate you.  Your goal is to get it to toss an error, so that you know your code is not being parsed correctly.</p>
<p>Because we are retrieving info about a page with my example website script, not users, it makes it difficult to authenticate yourself with one line of code like a basic SQL Injection.  There is one thing we can do, and that is to make the SELECT statement true or false.  If my site is vulnerable, all we would need to type in the browser is: &#8220;http://www.mysite.com/index.php?pageid=10 and 1=1&#8243;.  If it isn&#8217;t parsed correctly, my vulnerable code will now look like this: SELECT * FROM pages WHERE ID=10 and 1=1.  That is a true statement so our page will display normally.  Now we try a false statement: &#8220;http://www.mysite.com/index.php?pageid=10 and 1=2&#8243; and our SQL query reads as: SELECT * FROM pages WHERE ID=10 and 1=2.  This statement is false, so it will not grab ANY info from pages.  With no info being grabbed from the database, our page will look significantly different.  Maybe some pictures won&#8217;t be there, or text will be missing, or maybe the entire page will be blank.  If that is the case, then you are in business.</p>
<p>So why is it called BLIND Sql Injection?  Well rather than executing queries directly, like a regular SQL injection, and getting your info directly, you are now blind instead.  You are using true/false statements to guess what the table names are, or manually hash them out using ascii codes and substrings.  It can take hours to get access to a vulnerable site, but there are programs and scripts out there that will do all the work for you so you don&#8217;t even have to think about it.</p>
<p>If you want to know a little bit more about how this works, lets try it out on a vulnerable example: http://www.cblpi.org</p>
<p>Lets say I went to Ann Coulter&#8217;s bio page: http://www.cblpi.org/programs/bio.cfm?ID=15&#038;type=Speaker</p>
<p>We notice that ID=15 and may be vulnerable.  So we test it by typing: http://www.cblpi.org/programs/bio.cfm?ID=15 and 1=1&#038;type=Speaker</p>
<p>Nothing changes!  Let&#8217;s test it again by typing 1=2: http://www.cblpi.org/programs/bio.cfm?ID=15 and 1=2&#038;type=Speaker</p>
<p>Now a blank page appears, so we know that the ID variable isn&#8217;t being parsed correctly.</p>
<p>Lets figure out what version of MySQL it is using BSI using @@version: http://www.cblpi.org/programs/bio.cfm?ID=15 and substring(@@version,1,1)=5&#038;type=Speaker</p>
<p>If its version 5.xxxx it will show up as true!  Does it? No.  Then lets try 4: http://www.cblpi.org/programs/bio.cfm?ID=15 and substring(@@version,1,1)=4&#038;type=Speaker</p>
<p>And know we know they are using version 4.xxxx of MySQL.</p>
<p>Lets try guessing a table name! http://www.cblpi.org/programs/bio.cfm?ID=15 and (select 1 from USERS limit 0,1)=1&#038;type=Speaker</p>
<p>If table USERS exists, it will return 1 and the statement will be TRUE!  Does it?  No it doesn&#8217;t, but it does give us a nice error page saying it doesn&#8217;t exist, with the table name that we are currently selecting out of (PEOPLE).  </p>
<p>So lets validate the code real quick.  We know PEOPLE does exist, so http://www.cblpi.org/programs/bio.cfm?ID=15 and (select 1 from PEOPLE limit 0,1)=1&#038;type=Speaker should be true.</p>
<p>And it is! The page displays normally.  </p>
<p>You can bruteforce guess the table names, or there are many round about ways of detecting them letter by letter using substring and ascii functions.  And once you figure out where the good information is stored, then you can break into those tables and grab it.  </p>
<p>I&#8217;m not here to give you the exact methods on how to bruteforce it or use char codes, but just to give you the basics on what it is.  Know that you know what it is, protect against it!  How hard is it to check for a space? Add slashes? Check if it is an int?  Guys common, these practices are standard when dealing with forms, why not make them standard when dealing with <em>get</em> variables.  Recently (about 4-6 months ago) HyperVM was found to have a BSI vulnerability and within a couple of days, thousands and thousands of VPS servers were hacked and accounts deleted.  It all could have been prevented with just one line of code.</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2010/01/what-is-a-blind-sql-injection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP to create website thumbnails</title>
		<link>http://notan00b.com/2009/09/php-to-create-website-thumbnails/</link>
		<comments>http://notan00b.com/2009/09/php-to-create-website-thumbnails/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 00:46:20 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[explorer]]></category>
		<category><![CDATA[frontpage]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[internetexplorer]]></category>
		<category><![CDATA[internetexplorer.application]]></category>
		<category><![CDATA[nails]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[screenshot]]></category>
		<category><![CDATA[shot]]></category>
		<category><![CDATA[thumb]]></category>
		<category><![CDATA[thumbnails]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[webpage]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=230</guid>
		<description><![CDATA[Want to know how to create a thumbnail/screen shot of a webpage using PHP?  Well check it out here]]></description>
			<content:encoded><![CDATA[<p>Bing.com has an awesome feature that is popping up more and more all over the internet.  Most websites charge for this, as they should.  It isn&#8217;t something that can be done on a normal hosting plan, but can only be done (this way) on a Windows server, where Internet Explorer is installed and you have access to.  There are several ways to do this on Linux servers, but I am going to cover the Windows version.</p>
<p>In order to create an image for a thumbnail, we need to take a screen shot of webpage.  PHP GD class allows us to take screen shots of different windows.  PHP also allows us to control different windows, like Internet Explorer using the COM class.  So basically what we need to do is have PHP open up a window in Internet Explorer, navigate to a website, and then take a picture of that window, then save it/display it.  The tutorial shown on php.net shows exactly how to do this:</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: #000088;">$browser</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> COM<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;InternetExplorer.Application&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">HWND</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Visible</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Navigate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.libgd.org&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Still working? */</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Busy</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">com_message_pump</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagegrabwindow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Quit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;iesnap.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So this is SUPPOSED to load libgd.org and take a snap shot and save it to &#8220;iesnap.png&#8221;, and it DOES work, but only if your server allows it to work.  If it doesn&#8217;t you just get a black/blank picture.  </p>
<p>Windows rarely lets one program access another program via the desktop and control it.  By default, Apache isn&#8217;t allowed to open Internet Explorer on windows, so we have to add an exception.  </p>
<ol>
<li>On Vista, click the start button, and in the search box type &#8220;Services&#8221;.  </li>
<li>At the very top, there should be a link under Programs called Services.  Click that link. </li>
<li>It will open up all running services, and you will see Apache running (mine is Apache 2.2, and is the first entry).</li>
<li>Right click on it and select &#8220;Properties&#8221;</li>
<li>Navigate to the &#8220;Log On&#8221; tab</li>
<li>Click the box for &#8220;Allow service to interact with the desktop&#8221;</li>
<li>Save it and close the properties</li>
<li>Right click on Apache again</li>
<li>This time, click restart, and Apache will be restarted with the new features</li>
</ol>
<p>Now run your script again!  Vista pops up a little box alerting the user that a program is interact with the desktop.  Just ignore it and it will disappear in a few seconds.  After the program is complete, go to your web folder and you will see iesnap.png is now an image of a webpage!</p>
<p>Time to make some tweaks!  You will notice that it grabs the ENTIRE screen, meaning the tool bars and everything.  We will need to make a few adjustments to get rid of those.  The first one I made was to make the browser show as a full screen:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$browser</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> COM<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;InternetExplorer.Application&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">HWND</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Visible</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FullScreen</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

<p>That last line will make it display in the fullscreen mode, which takes care of the tool bars at the top, but I still see the footer and the side bar that I want to get rid of.  In order to do this, we need to crop a little, but GD doesn&#8217;t have a simple cropping method.  What I did was copied part of this picture to a new picture and resized it.  Keep in mind, my display settings are going to be different, and will require you to look at your picture and test it out:</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: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$w</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$h</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'h'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$w</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$h</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$h</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$url</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;www.notan00b.com&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1263</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$w</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1263</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$h</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">780</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$h</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1263</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> COM<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;InternetExplorer.Application&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">HWND</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Visible</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FullScreen</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Navigate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/* Still working?*/</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Busy</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">com_message_pump</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagegrabwindow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$browser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Quit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;iesnap.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dest</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</span><span style="color: #339933;">,</span><span style="color: #000088;">$h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Copy</span>
<span style="color: #990000;">imagecopyresized</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #339933;">,</span> <span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$h</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1263</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">780</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output and free from memory</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: image/gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagegif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>There we have it.  Even with the GET variables so you can reuse this script for AJAX or any HTML document.  My images were 1280&#215;800.  To remove the bars I scaled it down to 1263&#215;780, and they were removed almost perfectly, but it will be different for everyone.</p>
<p>I would post an example of it, but I am only hosted&#8230; I don&#8217;t actually pay for a VPS or private server just for one little blog.  Sorry folks.  If it was up to me, and I needed this service, I would probably pay a company to do it for me for a simple little website.  If you have a VPS with enough memory, this may work for you.  Good luck, let me know if you have any questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/09/php-to-create-website-thumbnails/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>jQuery, PHP, and Forms</title>
		<link>http://notan00b.com/2009/09/jquery-php-and-forms/</link>
		<comments>http://notan00b.com/2009/09/jquery-php-and-forms/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 01:56:57 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[box]]></category>
		<category><![CDATA[dialog]]></category>
		<category><![CDATA[edit]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[hidden]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=204</guid>
		<description><![CDATA[I decided to put up a quick post on how I have been using jQuery and PHP to process forms for me very easily.  To get started, let me give you a background on what I have been trying to do.  I have a page with a list of info, and I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to put up a quick post on how I have been using jQuery and PHP to process forms for me very easily.  To get started, let me give you a background on what I have been trying to do.  I have a page with a list of info, and I wanted to give the user the option of editing that info using jQuery&#8217;s built in dialog box because it is awesome looking.  There were a few ways to go about doing this, which is what I am going to describe.</p>
<h1>This is the goal!</h1>
<li>I click the link</li>
<li>A dialog box pops up</li>
<li>The text boxes have info filled in already</li>
<li>Something like this:<br />Name:<br />
<input type="text" value="Bob Jones"/></li>
<li>I can edit it, then submit it</li>
<li>I want the code to be small</li>
<p>The first way that I thought of was to create a form for every piece of info I pulled from the database.  So lets say for ID=1, there would be 1 form with all the info preloaded into it.  ID=2 would be a separate form with all different info.  This works, but I had to have about 20 dialog boxes per page, and it would cause the HTML output to be about 20 times larger than I needed.</p>
<p>In my head, I knew there had to be a way to do this with DHTML and jQuery, and I finally figured it out.  All I had to do was create a value and store it in the button that I was already using (well it was actually just a link).  So I turned <code>&lt;a href="#" class="editThis"&gt;Edit!&lt;/a&gt;</code> into <code>&lt;a href="#" class="editThis" id="1"&gt;Edit!&lt;/a&gt;</code>.  I used PHP to echo out the data already but now each link will have a different ID:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>#<span style="color: #000099; font-weight: bold;">\&quot;</span> class<span style="color: #000099; font-weight: bold;">\&quot;</span>editThis<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$row[UserId]</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Edit!&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now we have 20 links, each looking the same, but each with a unique ID.  This is where jQuery comes in handy.  We can look up the anchor by class, and then store the ID as a variable for later use!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.editThis&quot;</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;">&#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>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now we have the ID of the info we want to manipulate.  The next part was a little more difficult to figure out.  I want only 1 dialog box and 1 form, but want the info auto filled.  At first I was thinking we needed to do a lot of AJAX to pull out each and every variable that we want to edit, but then I figured out we could pass that info into HTML as a &#8220;HIDDEN&#8221; variable.  HTML is great because you can hide all sorts of stuff and the user will never see it.  I can set the style as &#8220;hidden&#8221;, or I can include it inside a tag somewhere, or better yet, and my favorite, I can create a hidden variable: <code>&lt;input type="hidden" id="editinfo1" value="myinfohere"&gt;</code>  Now we need to implement this into our PHP to write it to the HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>#<span style="color: #000099; font-weight: bold;">\&quot;</span> class<span style="color: #000099; font-weight: bold;">\&quot;</span>editThis<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$row[UserId]</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Edit!&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>hidden<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; name=<span style="color: #000099; font-weight: bold;">\&quot;</span>editinfo<span style="color: #006699; font-weight: bold;">{$row['UserId']}</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; id=<span style="color: #000099; font-weight: bold;">\&quot;</span>editinfo<span style="color: #006699; font-weight: bold;">{$row['UserId']}</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; value=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$row[Name]</span>::<span style="color: #006699; font-weight: bold;">$row[Address]</span>::<span style="color: #006699; font-weight: bold;">$row[Age]</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now our HTML will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;#&quot; class=&quot;editThis&quot; id=&quot;1&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;editinfo1&quot; id=&quot;editinfo1&quot; value=&quot;Bob Jones::123 N Street::26&quot;&gt;</pre></div></div>

<p>All our info is now conveniently stored for jQuery to access later!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.editThis&quot;</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;">&#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>
   <span style="color: #003366; font-weight: bold;">var</span> variables<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#editinfo&quot;</span><span style="color: #339933;">+</span>id<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> info<span style="color: #339933;">=</span>variables.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'::'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>info<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: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">+</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">+</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now for the fun part, using jQuery to dynamically change the values of our SINGLE form!<br />
Our form might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;dialog&quot; title=&quot;Edit User&quot;&gt;
    &lt;form method=&quot;post&quot; name=&quot;edituserform&quot; action=editUser.php&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;edituser&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;userid&quot; id=&quot;euid&quot; value=&quot;0&quot;&gt;
    &lt;center&gt;
        &lt;table width=&quot;75%&quot;&gt;
            &lt;tr&gt;
                &lt;td align=&quot;right&quot;&gt;User Name: &lt;/td&gt;
                &lt;td align=&quot;left&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;username&quot; id=&quot;eusername&quot;&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td align=&quot;right&quot;&gt;Address: &lt;/td&gt;
                &lt;td align=&quot;left&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;address&quot; id=&quot;eaddress&quot;&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td align=&quot;right&quot;&gt;Age: &lt;/td&gt;
                &lt;td align=&quot;left&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;age&quot; id=&quot;eage&quot;&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
    &lt;/center&gt;
    &lt;/form&gt;
&lt;/div&gt;</pre></div></div>

<p>And our jQuery would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.editThis&quot;</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;">&#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>
   <span style="color: #003366; font-weight: bold;">var</span> variables<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#editinfo&quot;</span><span style="color: #339933;">+</span>id<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> info<span style="color: #339933;">=</span>variables.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'::'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#eusername&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>info<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>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#eaddress&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#eage&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;euid&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;open&quot;</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;">&#40;</span><span style="color: #3366CC;">'#dialog'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        autoOpen<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
        width<span style="color: #339933;">:</span> <span style="color: #CC0000;">600</span><span style="color: #339933;">,</span>
        modal<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        position<span style="color: #339933;">:</span> <span style="color: #3366CC;">'top'</span><span style="color: #339933;">,</span>
        buttons<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;Update user&quot;</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>
                document.<span style="color: #660066;">edituserform</span>.<span style="color: #660066;">submit</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: #339933;">,</span> 
            <span style="color: #3366CC;">&quot;Cancel&quot;</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: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</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></div></div>

<p>That&#8217;s it!  Hopefully it was easy to understand.  Let me know if you need anything cleared up a little!  Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/09/jquery-php-and-forms/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>More with PHP Classes</title>
		<link>http://notan00b.com/2009/08/more-with-php-classes/</link>
		<comments>http://notan00b.com/2009/08/more-with-php-classes/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 20:13:19 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[employee]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=184</guid>
		<description><![CDATA[I got a comment on my PHP OOP tutorial to explain how to relate what I taught to real life scenarios.  His scenario was Human Resources Information.  The first thing we will need to decide is what info is important, so lets list a few items:

First name
Last Name
Social Security Number
Address
City
State
Zip
Date of birth
Salary
Position
Start Date

That&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I got a comment on my PHP OOP tutorial to explain how to relate what I taught to real life scenarios.  His scenario was Human Resources Information.  The first thing we will need to decide is what info is important, so lets list a few items:</p>
<ul>
<li>First name</li>
<li>Last Name</li>
<li>Social Security Number</li>
<li>Address</li>
<li>City</li>
<li>State</li>
<li>Zip</li>
<li>Date of birth</li>
<li>Salary</li>
<li>Position</li>
<li>Start Date</li>
</ul>
<p>That&#8217;s probably sufficient for our purposes, and then some.  So the next question we need to ask is, what are we planning on doing with this info?  I would assume a lot of it would be interacting with a database.  This would probably involve a lot of CRUD operations (Create, Read, Update, Delete).  So we need to make functions for each.  One to create a new employee in the database, one to get employee info, one to update employee info if it is changed, one to delete employees once they have been fired or never completed training or whatever.  The one other thing I can think of is maybe assigning an employee an ID so he can easily be tracked in the system, in case there are two Mr. John Smith employees.  Speaking of searching, maybe a search function would be nice as well.  So lets set out to create our class:</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: #000000; font-weight: bold;">class</span> Employee
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$Id</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$FirstName</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$LastName</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$DOB</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$Position</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$Salary</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$StartDate</span><span style="color: #339933;">=</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'m/d/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$EndDate</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">function</span> _construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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: #000088;">$id</span><span style="color: #339933;">!=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">LoadEmployee</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">function</span> LoadEmployee<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">/*** Connect to database  ***/</span>
        <span style="color: #666666; font-style: italic;">/*** Code Not Included ***/</span>
        <span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT FROM Employees WHERE Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//makes sure the user exists</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//Sets class variables</span>
            <span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</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;">Id</span><span style="color: #339933;">=</span><span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FirstName</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'FirstName'</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;">LastName</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'LastName'</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;">DOB</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOB'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Position</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Position'</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;">Salary</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Salary'</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;">StartDate</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'StartDate'</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;">EndDate</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'EndDate'</span><span style="color: #009900;">&#93;</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: #000000; font-weight: bold;">function</span> CreateEmployee<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CheckVariables</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">/*** Connect to database  ***/</span>
        <span style="color: #666666; font-style: italic;">/*** Code Not Included ***/</span>
        <span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;INSERT INTO Employees (FirstName,LastName,DOB,Position,Salary,StartDate) VALUES ('&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FirstName</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;','&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">LastName</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;','&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">DOB</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;','&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Salary</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;','&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">StartDate</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;')&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//Inserted, and now lets return his ID number</span>
        <span style="color: #000088;">$query2</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT Id FROM Employees WHERE FirstName='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FirstName</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' AND LastName='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">LastName</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' AND DOB='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">DOB</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' AND StartDate='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">StartDate</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query2</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: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>      
    <span style="color: #000000; font-weight: bold;">function</span> CheckVariables<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;">//makes sure we don't have any empty variables in the areas that matter</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FirstName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span>empty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">LastName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span>empty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">DOB</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span>empty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Position</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span>empty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Salary</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">else</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: #000000; font-weight: bold;">function</span> UpdateEmployee<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//makes sure an ID is chosen or specified.</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">/*** Connect to database  ***/</span>
        <span style="color: #666666; font-style: italic;">/*** Code Not Included ***/</span>
        <span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UPDATE Employees SET <span style="color: #006699; font-weight: bold;">$field</span>='<span style="color: #006699; font-weight: bold;">$value</span>' WHERE Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">function</span> ToArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Id'</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;">Id</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'FirstName'</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;">FirstName</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'LastName'</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;">LastName</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Position'</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;">Position</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Salary'</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;">Salary</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOB'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">DOB</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'StartDate'</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;">StartDate</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'EndDate'</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;">EndDate</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$array</span><span style="color: #339933;">;</span>        
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here is the finished class minus a few things that you can add if you want.  What my class does is creates employees, updates specific fields, checks information, turns the info into an array, etc.  I didn&#8217;t include all the variables we listed because I am lazy, as well as the delete function.  There are always improvements that can be made, but this is a good starting block for any programmer.  So lets see how we use it:</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;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;classEmployee.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//create a new employee</span>
<span style="color: #000088;">$emp</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$emp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FirstName</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Bob&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$emp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">LastName</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Smith&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$emp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">DOB</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;01/01/99&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$emp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Position</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Data Entry&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$emp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Salary</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$10</span>/hr&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$empid</span><span style="color: #339933;">=</span><span style="color: #000088;">$emp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CreateEmployee</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//And now he was created and entered into the system.</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$empid</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//His ID;</span>
<span style="color: #666666; font-style: italic;">//lets check to make sure he is still in the system.</span>
<span style="color: #000088;">$emp2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #000088;">$empid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$empArray</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$emp2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ToArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$empArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//ok, all looks good.  Lets update his name.</span>
<span style="color: #000088;">$emp2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">UpdateEmployee</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FirstName&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Bobby&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$empid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Well that&#8217;s my sample.  Its pure rough draft, and probably won&#8217;t even work properly, but I am conveying the IDEA of OOP and employees.  Think how much smoother your code would be if you created a few forms asking for employees IDs and it loads it, or allows the HR Department to enter a new employees info straight into the database on a very simple form.  Good luck with this!  Let me know if there are any other problems I can help with!</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/08/more-with-php-classes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP PDO and SQL injections</title>
		<link>http://notan00b.com/2009/08/php-pdo-and-sql-injections/</link>
		<comments>http://notan00b.com/2009/08/php-pdo-and-sql-injections/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 18:22:42 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[bypass]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[injections]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[MsSQL]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PDO]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=179</guid>
		<description><![CDATA[SQL Injections o_O???]]></description>
			<content:encoded><![CDATA[<p>MySQL and MsSQL are huge when it comes to internet sites.  They are probably the easiest databases to set up and the most universal when it comes to internet apps.  The only problem is that programmers are sometimes lazy and forget to think about things like security when it comes to programming the forms and site.  When people are lazy, they leave their sites open to SQL injections.  I don&#8217;t really want to make this an SQL injection tutorial, but just want to touch on it briefly.  Lets start with a standard SQL statement that a form uses to find an Administrator password:<br />
<code>mysql&gt; SELECT * FROM Users WHERE Name='Admin' AND Pass='MyP4ssw0rd';</code><br />
Now if we add a login form and use php, we might have code structured similar to this:</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: #000088;">$name</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pass</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: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT * FROM Users WHERE Name='<span style="color: #006699; font-weight: bold;">$name</span>' AND Pass='<span style="color: #006699; font-weight: bold;">$pass</span>'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This would work perfectly, and would allow people to log in.  It would also allow people to SQL inject it.  What if they typed this in:</p>
<table>
<tr>
<td>Name:</td>
<td>
<input type="text" value="admin"></td>
</tr>
<tr>
<td>Pass:</td>
<td>
<input type="text" value="admin' OR 'x'='x"></td>
</tr>
</table>
<p>
Well our SQL statement ends up looking like this:<br />
<code>SELECT * FROM Users WHERE Name='admin' AND Pass='admin' OR 'x'='x'</code><br />
&#8216;x&#8217;='x&#8217; is a TRUE statement 100% of the time, so as long as &#8216;x&#8217;='x&#8217;, it will pull up all the info you needed, and suddenly you are logged in.  This is probably the easiest SQL injection around, and does work on occasion.  There are hundreds of SQL injections, and a poorly coded site will always be susceptible.  Not only are people able to log into a site as an administrator, but they can display login info using UNION statements.<br />
One other way good programmers tend to get lazy is through their &#8220;GET&#8221; variables.  A lot of times programmers will pass variables in the URL bar: http://www.mysite.com/view.php?id=123.  There is nothing wrong with this, in fact, everyone does it and will continue to do it because it is efficient and easy to do.  The problem comes when people SQL inject that variable: view.php?id=123&#8242; and &#8216;x&#8217;='y.  This particular injection allows a hacker to test your site for susceptibility to injections in the URL bar.  If you didn&#8217;t protect your variables, your SQL statement would look like this if it wasn&#8217;t protected: <code>"SELECT * FROM Info WHERE Id='123' and 'x'='y'"</code>.  If it wasn&#8217;t protected, the hacker would see a page with NO info on it, or an error because &#8216;x&#8217; NEVER equals &#8216;y&#8217; and is a FALSE statement.  If they see a blank page or an error, they know they can continue their attack and eventually gain access to all of your information.</p>
<p>My goal is not to teach you how to hack, but rather how a simple PHP object exists and will help you avoid these types of attacks.  That object is PDO.  It comes standard with the latest releases of PHP and can be used to prevent SQL injections very simply.  PDO is a database object, that allows you to connect to a variety of different databases, send queries, and display the results.  It actually is a bit easier to code than actual mysql in PHP, but it is a complete 180 from what you have been taught using w3schools and tizag.com.  I don&#8217;t really want to get into a tutorial of PDO either because there are quite a few on the internet that explain it much better than I can right now.  I recommend checking out <a href="http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html">http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html</a> to get a grip on the basics of how PDO works.  The one thing that this tutorial does NOT explain is the &#8220;prepare&#8221; function.  He only goes into it slightly, but doesn&#8217;t describe what it does exactly.  Prepare() simply checks for all types of quotes and makes sure that no SQL injections can get through.  Here is a quick example:</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: #000088;">$mysql_host</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mysql_user</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mysql_pass</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mysql_database</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;epco&quot;</span><span style="color: #339933;">;</span>
try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mysql:host=<span style="color: #006699; font-weight: bold;">$mysql_host</span>;dbname=<span style="color: #006699; font-weight: bold;">$mysql_database</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mysql_user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mysql_pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/*** create the statement ***/</span>
    <span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM users WHERE user = :user AND pass = :pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/*** bind the paramaters ***/</span>
    <span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':user'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_STR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':pass'</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: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_STR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/*** execute the prepared statement ***/</span>
    <span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/*** close the database connection ***/</span>
    <span style="color: #000088;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span>catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>When we prepare our statement, we use a place-holder that will later assign a variable to.  When that statement is executed, all our quotes are removed or slashed.  <code>$_POST['user']="admin' OR 1=1";</code> typically becomes <code>$_POST['user']="admin\' OR 1=1";</code> and your database is protected from the attacks.</p>
<p>In conclusion, I am not advocating PDO and saying only to use that and nothing else.  I use a database class that is much smaller and does what I need it to do.  I have spoken with other programmers who have created their own class to add/strip slashes, rawurlencode/decode, and htmlspecialcharacters and connect to their databases.  PDO was created as a &#8220;universal&#8221; database object that can do whatever you want it to do with several different databases.  It is awesome and will go a long way to protect your site from SQL injections if used properly.  The prepare statement is simple, but doesn&#8217;t protect against EVERY type of attack.  Don&#8217;t be fooled into a false sense of security just because you are using a PHP object.  XSS, javascript injections, and bruteforce, attacks are all still possible even if you use this class.  Be careful in what you code, and fix any code when the holes are found by users or when you are hacked.</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/08/php-pdo-and-sql-injections/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Wordpress page views hack</title>
		<link>http://notan00b.com/2009/07/wordpress-page-views-hack/</link>
		<comments>http://notan00b.com/2009/07/wordpress-page-views-hack/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 19:54:22 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=177</guid>
		<description><![CDATA[I created this pretty quick, and it is by far, not the prettiest thing out there.  This uses an extra table in the MySQL table to keep track of pages that get viewed and how many times they are viewed.  Here is what I did:
Step 1 &#8211; Create a new table!
Go to your [...]]]></description>
			<content:encoded><![CDATA[<p>I created this pretty quick, and it is by far, not the prettiest thing out there.  This uses an extra table in the MySQL table to keep track of pages that get viewed and how many times they are viewed.  Here is what I did:</p>
<p><b>Step 1 &#8211; Create a new table!</b><br />
Go to your MySQL database and create a table called &#8220;wp_counter&#8221; with 2 fields: Post_Id and Count.  Both are Int with length 10.  Here is the SQL code you can copy and paste to make it a bit shorter:<br />
<code>CREATE TABLE `wp_counter` (`Post_Id` INT( 10 ) NOT NULL , `Count` INT( 10 ) NOT NULL) </code></p>
<p><b>Step 2 &#8211; Editing post-template.php</b><br />
This is the file that is used to display your posts (not the snippets on your front page, but full out posts).  It is located in the &#8220;wp-includes&#8221; directory.<br />
Open post-template.php for editing.<br />
Search for a function called &#8220;<code>the_content()</code>&#8220;.  It typically starts on line 165 should look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>165
166
167
168
169
170
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$more_link_text</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stripteaser</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$more_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> get_the_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$more_link_text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stripteaser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$more_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">']]&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">']]&amp;gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So what we want to do is make it so that every time it shows the content, it will update the count and display it to the user.  So we are going to change that function to look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>165
166
167
168
169
170
171
172
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$more_link_text</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stripteaser</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$more_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> get_the_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$more_link_text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stripteaser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$more_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">']]&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">']]&amp;gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
	update_count<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: #0000ff;">&quot;&lt;small&gt;&lt;i&gt;Views: &quot;</span><span style="color: #339933;">.</span>get_content_views<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/i&gt;&lt;/small&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And immediately following that, we are going to add a couple functions:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> update_count<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM wp_counter WHERE Post_Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;wp_counter&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Post_Id'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Count'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">1</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;">'%d'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'%d'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM wp_counter WHERE Post_Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>ARRAY_A<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$count</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Count'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_counter'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Count'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$count</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;">'Post_Id'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$id</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;">'%d'</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;">'%d'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> content_views<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM wp_counter WHERE Post_Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>ARRAY_A<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Count'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it!  So your finalized post-template.php will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$more_link_text</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stripteaser</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$more_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> get_the_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$more_link_text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stripteaser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$more_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">']]&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">']]&amp;gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
	update_count<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: #0000ff;">&quot;&lt;small&gt;&lt;i&gt;Views: &quot;</span><span style="color: #339933;">.</span>get_content_views<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/i&gt;&lt;/small&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> update_count<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM wp_counter WHERE Post_Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;wp_counter&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Post_Id'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Count'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">1</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;">'%d'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'%d'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM wp_counter WHERE Post_Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>ARRAY_A<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$count</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Count'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_counter'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Count'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$count</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;">'Post_Id'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$id</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;">'%d'</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;">'%d'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> content_views<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM wp_counter WHERE Post_Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>ARRAY_A<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Count'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> get_content_views<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM wp_counter WHERE Post_Id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>ARRAY_A<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Count'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now every time someone visits the page, they will see how many other people have viewed that page!</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/07/wordpress-page-views-hack/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Nifty PHP/AJAX tricks</title>
		<link>http://notan00b.com/2009/07/nifty-phpajax-tricks/</link>
		<comments>http://notan00b.com/2009/07/nifty-phpajax-tricks/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 21:03:52 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[eval]]></category>
		<category><![CDATA[evaluate]]></category>
		<category><![CDATA[exit]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=166</guid>
		<description><![CDATA[This brief tutorial covers usage of the exit function to help with AJAX scripts as well as how to return both a numeric and associative array via AJAX.]]></description>
			<content:encoded><![CDATA[<p>These are a couple awesome AJAX/PHP tricks I learned to make coding just slightly easier on myself.</p>
<h1>Exit</h1>
<p>Lets say we had an index page, and when the user click a button, it would display the time.  Typically, in the past I would have it go to another &#8220;remote procedure call&#8221; (RPC) page, like &#8220;time.php&#8221; where the main portion of the script would be &#8220;echo time();&#8221;.  This tends to clutter my folders with miscellaneous scripts if I have multiple AJAX events.  I may have a different RPC for every page, which could mean I have like 50 php files in one folder and I&#8217;m not sure which ones are the RPC and which ones are the main files.</p>
<p>I then started condensing them, making one large script for a few files, and using a $_POST['action'] to decide what to do.  So if I had an ajax.php page it might look something like this:</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: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</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: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'hello'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;hello!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So now it will execute the portion of the script that it is told.  So if I point my AJAX to ajax.php and pass a post variable called &#8220;action=time&#8221;, it will return the time.  We have now condensed it slightly.</p>
<p>We can condense it even further by using the php function &#8220;exit();&#8221;.  This function completely stops (exits) the script.  If my script looked like this:</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;">echo</span> <span style="color: #0000ff;">&quot;hello &quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</span><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: #0000ff;">&quot;world!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The output would look like this: <code>hello </code><br />
Exit will abort the script and not run anything after it.</p>
<p>Exit also has the capability to display (echo) a message as to why it is quitting, so you could essentially use exit() and let people know they aren&#8217;t logged in if you wanted.  Here is an example:</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: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$notlogged</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>login.php<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Please log in to view this page&lt;/a&gt;&quot;</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;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'loggedin'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$notlogged</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* Main Content Starts Here */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So if we had never logged into the site, all we would see is &#8220;Please log in to view this page&#8221; as a link that we could click.</p>
<p>Lets put this all together.  The exit function allows you to point the ajax script to the current page, as it will &#8220;exit&#8221; and echo the data you want without running the main content of the page over again.  Let me show you what I mean, using jQuery ajax, with a time.php.</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: #666666; font-style: italic;">//time.php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'do'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'do'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#41;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'g:i A'</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: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'do'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'do'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'hello'</span><span style="color: #009900;">&#41;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;AJAX Example&lt;/title&gt;
&lt;link type=&quot;text/css&quot; href=&quot;css/custom-theme/jquery-ui-1.7.2.custom.css&quot; rel=&quot;stylesheet&quot; /&gt;	
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-ui-1.7.2.custom.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.geturlparams.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function() {
	$('#checktime').click(function(){
		$.get('time.php',
			{do:'time'},
			function(data){
				$('#timeorhello').html(data);
			});								 
	});
	$('#sayhello').click(function(){
		$.get('time.php',
			{do:'hello'},
			function(data){
				$('#timeorhello').html(data);
			});								 
	});
});
&lt;/script&gt;
&lt;body&gt;
&lt;div id=&quot;timeorhello&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;input type=&quot;button&quot; id=&quot;checktime&quot; value=&quot;Get Time&quot; /&gt;
&lt;input type=&quot;button&quot; id=&quot;sayhello&quot; value=&quot;Say Hello&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Pretty simple stuff right?  You can reduce the number of extra files by simply putting the AJAX commands at the top of the file.  If you put &#8220;echo(time());&#8221;, it would continue to run the entire page and mess up your AJAX.  This has made my life easier and my code a little more succinct.</p>
<h1>Arrays</h1>
<p>Because AJAX only accepts strings to be echo, and returned to the AJAX function, so a lot of people struggle with returning an array to Javascript through AJAX.  There are 2 ways to do this that don&#8217;t require JSON.</p>
<p>The first is simply to break the array up and make it into a string using the &#8220;implode&#8221; function:</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: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</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: #0000ff;">&quot;world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$newarray</span><span style="color: #339933;">=</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;::&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newarray</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>On the Javascript end, just split it up into an array again!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray<span style="color: #339933;">=</span>data.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'::'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That is by far the easiest way to return arrays.</p>
<p>There is one problem.  What if in PHP, your arrays didn&#8217;t have numerical keys, but strings: <code>$array['name']='bob'</code>.  This is where we run into a problem.  There is a way around this fairly easy.  Most languages have an &#8220;evaluate&#8221; command, which will take a string, and execute it as though it were code.  In PHP and Javascript, it is eval().  Here is a PHP example: <code>eval("echo 'Hello World!';");</code>.  This would output &#8220;Hello World!&#8221;.  In Javascript, it is exactly the same thing.  So lets use eval() to return a Javascript array!</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: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'bob'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'96'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sex'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'male'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$return</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'var myArray={&quot;name&quot;:&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;, &quot;age&quot;:&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;, &quot;sex&quot;:&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sex'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;};
echo $return;
?&gt;</span></pre></div></div>

<p>And on the Javascript side, all you need to do is evaluate the string:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> handler<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;">eval</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myArray<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Its a bit lengthy to do it this way, but will definitely get the job done.  One thing to watch out for would be quotes, plus signs, slashes, especially if the user gets to define some of the stuff that is being returned.</p>
<p>I hope this has helped out a little with using PHP and AJAX.  I spent hours trying to figure this stuff out the first time, but when I did, it made my life a lot simpler. </p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/07/nifty-phpajax-tricks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery &#8211; Lesson 2</title>
		<link>http://notan00b.com/2009/07/jquery-lesson-2/</link>
		<comments>http://notan00b.com/2009/07/jquery-lesson-2/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 00:05:25 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[child]]></category>
		<category><![CDATA[click]]></category>
		<category><![CDATA[each]]></category>
		<category><![CDATA[hover]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[parent]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[sibling]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=125</guid>
		<description><![CDATA[I have been working pretty intensely with jQuery and have learned a lot.  It is a difficult language to work with because it is picky.  But once you understand it, it is actually quite simple to manipulate.
Today I want to cover parent/child/sibling relationships and how jQuery can be very effective here.  I [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working pretty intensely with jQuery and have learned a lot.  It is a difficult language to work with because it is picky.  But once you understand it, it is actually quite simple to manipulate.</p>
<p>Today I want to cover parent/child/sibling relationships and how jQuery can be very effective here.  I spent a while trying to figure this out in javascript, and yes, it is completely do-able in just a little more code, but jQuery always has the UI plugins that make coding super easy.  Let me show you some code I currently have:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</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>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#rows'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;">hover</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: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rows-add-class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <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><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rows-add-class'</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;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.section2, .section3, .section4, .section5'</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>
         window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;viewClient.php?id=&quot;</span><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;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.small'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#cb'</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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><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;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.section1'</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>$<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;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.small'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#cb'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checked'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.small'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#cb'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checked'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000066; font-weight: bold;">else</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;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.small'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#cb'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checked'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</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;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I know this looks complicated, but lets break it down line by line.</p>
<ol>
<li><code>$('div').each(function (){</code></li>
<p>This code goes through each &lt;div&gt; in my code, and with each &lt;div&gt; it finds, it executes a function, which is the remainder of the code.</p>
<li><code>if($(this).is('#rows')) {</code></li>
<p>So when it finds a
<div>, it checks to see if the &lt;div&gt; that was found &#8220;is&#8221; (or has the property of) &#8216;#rows&#8217;.  The # in the front means the &lt;div&gt; id.  If there is a &#8220;.&#8221; in front, it means the &lt;div&gt; class.  If there is nothing, then it means that it is a tag itself.  So when it finds the div tag with the id=&#8221;rows&#8221;, it will execute the following code.</p>
<li><code>$(this).hover(</code></li>
<p>Simply, it adds a hover class to the div with the id=&#8221;rows&#8221;.  When you add a hover, you need to have it do 2 things, mouseover and mouseout.  So the next parts of the code describe mouseover and mouseout respectively.</p>
<li><code>function() { $(this).addClass('rows-add-class'); },</code></li>
<p>So on mouseover, it will execute this function, which is, it will add a class called &#8216;rows-add-class&#8217; to the div we have selected.  Note the &#8220;,&#8221; at the end, which means that this is the next line is also part of hover().</p>
<li><code>function() { $(this).removeClass('rows-add-class'); }</code></li>
<p>On mouseout, it will remove the class.  We could have it add another class or give us an alert if we wanted, but typically when you hover over something with the mouse, you want to emphasize/change something, and when you move it out, you want it to return to normal.</p>
<li><code>);</code></li>
<p>So finally we start closing stuff up.  This closes up the hover statement.</p>
<li><code>$(this).children('.section2, .section3, .section4, .section5').click(function(){</code></li>
<p>Here we are getting into children.  If you have a div, and inside that div, you have multiple other divs, you may want to do something to those divs.  What I am doing here is adding an &#8220;onclick&#8221; function to the children divs with classes &#8220;section2&#8243;, &#8220;section3&#8243;, &#8220;section4&#8243;, &#8220;section5&#8243;.  Notice how here I used &#8216;.section1, etc&#8217;, the &#8220;.&#8221; signifies classes.  So this is statement, I am adding an &#8220;onclick&#8221; to the child tags under &#038;ltldiv id=&#8221;rows&#8221;&gt;.  The next line will tell me what exactly I am going to do when I click on one of those 4 child divs.</p>
<li><code>window.location.href= "viewClient.php?id="+$(this).siblings('.small').children('#cb').val();</code></li>
<p>Wow, thats a long line of code!  window.location.href= is a redirect, so basically, when you click the divs, it will act like a link and take you somewhere else.  It looks like it will take you to a viewClient.php page with the id= whatever $(this).siblings(&#8216;.small&#8217;).children(&#8216;#cb&#8217;).val(); equals.  It gets a little confusing here.  So &lt;div id=&#8221;rows&#8221;&gt; is set up with a few children tags, and those children have a few tags.  All of the children are siblings.  So if I was to click on one of the children, say one with the class &#8220;section2&#8243;, I want to find out info about his sibling, with the class &#8220;small&#8221;.  Now &#8220;small&#8221; has a child with the id=&#8221;cb&#8221;.  Cb stands for check box.  Basically, when you click on the div, it will find its sibling&#8217;s, child&#8217;s value, which is the ID # we are looking for.  At the bottom of the page you can see the layout of the div with children and grandchildren.  So to recap, this line redirects us to &#8220;viewClient.php?id=1&#8243;.<br />
Why did I do this?  Well I am pulling data out of a database, and I don&#8217;t want to make a link in every div so they have to click on the words to be redirected.  These couple lines of code make it much easier for the user to redirect to the page they want.</p>
<li><code>});</code></li>
<p>Just closing up the &#8220;onclick&#8221; that we added to those 4 divs.</p>
<li><code>$(this).children('.section1').click(function(){</code></li>
<p>Here is the div with class=&#8221;section1&#8243; and we are adding a different &#8220;onclick&#8221; function to it.  Let&#8217;s see what we want it to do.</p>
<li><code>if($(this).siblings('.small').children('#cb').attr('checked'))</code></li>
<p>This looks similar to line 8 where we locate the sibling with the class=&#8221;small&#8221; and the child it has with the id=&#8221;cb&#8221;.  So we are locating our checkbox.  If our checkbox is checked (.attr(&#8216;checked&#8217;) returns true if checked, false if not checked), we are going to do something.  That was pretty easy now that siblings and children make a little sense.</p>
<li><code>$(this).siblings('.small').children('#cb').attr('checked',false);</code></li>
<p>Ok, so if it was checked, all this will do is set &#8216;checked&#8217; to false, meaning it will un-check the check box.  </p>
<li><code>else</code></li>
<p>If it wasn&#8217;t checked, then what.</p>
<li><code>$(this).siblings('.small').children('#cb').attr('checked',true);</code></li>
<p>Well then, check it!  So why did I add this code?  Well just to add it really.  It makes clicking the check box slightly easier.  They can click on this div, and it will check/uncheck the checkbox for them.
</ol>
<p>The rest of the code simply closes up the function that I created.  This is something I would have it do on start up, rather than allowing the user to click a button to enable these features.</p>
<p>This is the HTML markup that was used in the code.  The classes are easily created and is something you can do on your own.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;rows&quot;&gt;
   &lt;div class=&quot;small&quot;&gt;&lt;input type=&quot;checkbox&quot; id=&quot;cb&quot; value=&quot;1&quot;&gt;&lt;/div&gt;
   &lt;div class=&quot;section1&quot;&gt;1 (Clients ID)&lt;/div&gt;
   &lt;div class=&quot;section2&quot;&gt;Bob (Clients Name)&lt;/div&gt;
   &lt;div class=&quot;section3&quot;&gt;123 Fast Lane (Clients Address)&lt;/div&gt;
   &lt;div class=&quot;section4&quot;&gt;12345 (Clients Zip)&lt;/div&gt;
   &lt;div class=&quot;section5&quot;&gt;(123)555-1212 (Clients Phone)&lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p>Pretty simple layout causes pretty intense code.  Hopefully my explanation shows you some of what jQuery can do for you and provides a plan of attack for you.  If you want any help, let me know and I will be willing to explain something, review your code, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/07/jquery-lesson-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
