<?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>bhaviksBlog &#187; PHP</title>
	<atom:link href="http://bhaviksblog.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://bhaviksblog.com</link>
	<description></description>
	<lastBuildDate>Fri, 06 Nov 2009 20:25:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>PHP Login System Tutorial &#8211; Part 3</title>
		<link>http://bhaviksblog.com/02/php-login-system-tutorial-part-3/</link>
		<comments>http://bhaviksblog.com/02/php-login-system-tutorial-part-3/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 17:00:19 +0000</pubDate>
		<dc:creator>Bhavik</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[add pictures]]></category>
		<category><![CDATA[align form elements]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[delete from webserver]]></category>
		<category><![CDATA[login system]]></category>
		<category><![CDATA[thumbnail]]></category>
		<category><![CDATA[update profile]]></category>
		<category><![CDATA[web server directories]]></category>

		<guid isPermaLink="false">http://bhaviksblog.com/?p=151</guid>
		<description><![CDATA[Part 3 of my login system tutorial using PHP. In this part I go over how to let a user update their profile, add pictures and delete pictures. We create directories for the user on the web server and store references of the picture in our database. We also create thumbnails of the pictures using PHP. I add css to all of the pages to help align form elements and pretty up the site a bit. Finally I show you how to use mod_rewrite in your .htaccess file to clean up urls. Enjoy!]]></description>
			<content:encoded><![CDATA[<p>This will be the last part of the tutorial. Below are links to the previous 2 parts. Sorry for the lack of whitespace use, I&#8217;m trying to save you from scrolling horizontally <img src='http://bhaviksblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<a href="http://bhaviksblog.com/01/php-login-system-tutorial-part-1/">Link to part 1</a><br />
<a href="http://bhaviksblog.com/01/php-login-system-tutorial-part-2/">Link to part 2</a></p>
<p>So far, we can register and log in. Now we want to be able to update our profile and add pictures. We&#8217;ll need to add another table in our database to store references of the users pictures. </p>
<p>If we were to store the actual picture into the database it could become very big, especially if you decide to allow your users to upload videos too. A better option would be to upload the picture into a directory on the web server and keep a reference to the picture in our database.</p>
<p>So first lets add our table. I added size and type to explain how to retrieve those values but I won&#8217;t be using them in my version of the site.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`db`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`user_photos`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`profile_id`</span> <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`title`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">128</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`size`</span> <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`type`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">128</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`reference`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">255</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> 
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> MYISAM</pre></div></div>

<p>The first 2 parts of the login system set everything up but the output looks terrible. We&#8217;ll add some css to make the site somewhat presentable. Give the sections a background and align the form elements with css.</p>
<p><strong>style.css</strong></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.formElm</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.formElm</span> label
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">120px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
div<span style="color: #6666ff;">.divider</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">450px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#C1f0f6</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">4px</span> <span style="color: #cc00cc;">#0ebfe9</span> <span style="color: #993333;">double</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Now we&#8217;re ready to make our profile control panel. At the top of our profilecp.php we need to start our session to see if the user is authenticated. So we start our session and include our database info.<br />
<span id="more-151"></span><br />
<strong>profilecp.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<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: #b1b100;">include</span> <span style="color: #0000ff;">&quot;db_connect.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Next we&#8217;ll link to the stylesheet we just created and echo one of the session variables we created when the user first logged in (on index.php).<br />
<strong>profilecp.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&nbsp;
  &lt;head&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;  
  &lt;title&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>'s Homepage&lt;/title&gt;
  &lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;div class=&quot;divider&quot;&gt;</pre></div></div>

<p>So now the title of the page will be that user&#8217;s name. This is how we&#8217;ll be referencing our user and their id for interaction with our database.</p>
<p>Now we&#8217;ll need to pull this users info. The SQL to do this using our session variable would be:<br />
<strong>profilecp.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//session id is established</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>home.php<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Home&lt;/a&gt;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//ugly navigation</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>logout.php<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Logout&lt;/a&gt;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//grab record with the correct id</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT * from `users` WHERE `id`='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SESSION</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: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$res</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//pull entire row of info and store in to  a variable</span></pre></div></div>

<p>We have all of this users info. We need to let them update it. We&#8217;ll do the usual if !POST method. If the form hasn&#8217;t posted, output the html, otherwise update the database.<br />
<strong>profilecp.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">if(!$_POST['update'])
{
?&gt;
&lt;/div&gt;
&lt;div class=&quot;divider&quot;&gt;
&lt;form method=&quot;post&quot; action=&quot;profilecp.php&quot;&gt;
&lt;br/&gt;&lt;strong&gt;Profile Control Panel&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;
&nbsp;
&lt;div class=&quot;formElm&quot;&gt;
&lt;label for=&quot;first&quot;&gt;First Name&lt;/label&gt;
&lt;input id=&quot;first&quot; type=&quot;text&quot; name=&quot;first&quot; maxlength=&quot;32&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
&lt;/div&gt;
&nbsp;
&lt;div class=&quot;formElm&quot;&gt;
&lt;label for=&quot;last&quot;&gt;Last Name&lt;/label&gt;
&lt;input id=&quot;last&quot; type=&quot;text&quot; name=&quot;last&quot; maxlength=&quot;32&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
&lt;/div&gt;
&nbsp;
&lt;div class=&quot;formElm&quot;&gt;
&lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
&lt;input id=&quot;email&quot; type=&quot;text&quot; name=&quot;email&quot; maxlength=&quot;255&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
&lt;/div&gt;
&nbsp;
&lt;div class=&quot;formElm&quot;&gt;
&lt;label for=&quot;about&quot;&gt;About&lt;/label&gt;
&lt;textarea id=&quot;about&quot; cols=&quot;40&quot; rows=&quot;6&quot; name=&quot;about&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'about'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/textarea&gt;
&lt;/div&gt;
&nbsp;
&lt;input type=&quot;submit&quot; name=&quot;update&quot; value=&quot;Update&quot;&gt;
&lt;/form&gt;
&lt;/div&gt;</pre></div></div>

<p>That will be the form to update the text portion of the profile. We also want to allow them to add pictures. The form will submit to addpics.php where we&#8217;ll create directories and interact with our database. Heres the html for that. We&#8217;ll make addpics.php later.<br />
<strong>profilecp.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;divider&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>form enctype<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;multipart/form-data&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;addpics.php&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Upload Pictures<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formElm&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;title&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;title&quot;</span> maxlength<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;32&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formElm&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #339933;">&gt;</span>File<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;file&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;file&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pics&quot;</span> maxlength<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;255&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Add&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Since they can add pictures, we&#8217;ll need to let them delete them too. This form will submit to deletepics.php which will handle removing entries from our database and deleting the file from the web server.<br />
<strong>profilecp.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;divider&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$sql2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM user_photos WHERE profile_id =&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SESSION</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;">$res2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql2</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #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;">$res2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//user has pictures</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;strong&gt;Delete Pictures&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;form name=<span style="color: #000099; font-weight: bold;">\&quot;</span>deletefile<span style="color: #000099; font-weight: bold;">\&quot;</span> method=<span style="color: #000099; font-weight: bold;">\&quot;</span>post<span style="color: #000099; font-weight: bold;">\&quot;</span> action=<span style="color: #000099; font-weight: bold;">\&quot;</span>deletefile.php<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//output a checkbox next to each pic so they can delete multiple pics with one click</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;input name=<span style="color: #000099; font-weight: bold;">\&quot;</span>files[]<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>checkbox<span style="color: #000099; font-weight: bold;">\&quot;</span> value=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reference'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/pics/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reference'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>/&gt;
&lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/pics/thumbs/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reference'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>/&gt;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>submit<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>delfile<span style="color: #000099; font-weight: bold;">\&quot;</span> value=<span style="color: #000099; font-weight: bold;">\&quot;</span>Delete Files<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/form&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">//they havent uploaded any pics yet! </span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Please upload some pictures!&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This next part handles updating the database for the text portion only. The files addpics.php and deletepics.php take care of the rest.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$first_name</span><span style="color: #339933;">=</span>protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$last_name</span><span style="color: #339933;">=</span>protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$about</span><span style="color: #339933;">=</span>protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'about'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$email</span><span style="color: #339933;">=</span>protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql3</span> <span style="color: #339933;">=</span>	<span style="color: #0000ff;">&quot;UPDATE `users` SET `first`='<span style="color: #006699; font-weight: bold;">$first_name</span>',`last`='<span style="color: #006699; font-weight: bold;">$last_name</span>',
`email`='<span style="color: #006699; font-weight: bold;">$email</span>',`about`='<span style="color: #006699; font-weight: bold;">$about</span>' WHERE `id`='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SESSION</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: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$res3</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql3</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Your profile has been successfully updated!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>		
&nbsp;
<span style="color: #666666; font-style: italic;">//else no session id was created, user is not authenticated...redirect to index</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>Javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;document.location.href='index.php'&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<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>Now we&#8217;re ready to make addpics.php which handles the pictures. This file will create a directory for the user, add the picture to that directory and also create a thumbnail of the picture. First we&#8217;ll add our thumbnail function to our db_connect.php so we can call it in the rest of our files. This function is not my work, I found it googling when I was teaching myself php. Credit goes to <a href="http://icant.co.uk/articles/phpthumbnails/" target="_blank">http://icant.co.uk/articles/phpthumbnails/</a></p>
<p>add the function to <strong>db_connect.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> createthumb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$new_h</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$system</span><span style="color: #339933;">=</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/jpg|jpeg/&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$system</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$src_img</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefromjpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/png/&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$system</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$src_img</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefrompng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$old_x</span><span style="color: #339933;">=</span><span style="color: #990000;">imageSX</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$old_y</span><span style="color: #339933;">=</span><span style="color: #990000;">imageSY</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_img</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;">$old_x</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$old_y</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$thumb_w</span><span style="color: #339933;">=</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb_h</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_y</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_h</span><span style="color: #339933;">/</span><span style="color: #000088;">$old_x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_x</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$old_y</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$thumb_w</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_x</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">/</span><span style="color: #000088;">$old_y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb_h</span><span style="color: #339933;">=</span><span style="color: #000088;">$new_h</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_x</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$old_y</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$thumb_w</span><span style="color: #339933;">=</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb_h</span><span style="color: #339933;">=</span><span style="color: #000088;">$new_h</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$dst_img</span><span style="color: #339933;">=</span><span style="color: #990000;">ImageCreateTrueColor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$thumb_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #339933;">,</span><span style="color: #000088;">$src_img</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;">$thumb_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_h</span><span style="color: #339933;">,</span><span style="color: #000088;">$old_x</span><span style="color: #339933;">,</span><span style="color: #000088;">$old_y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/png/&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$system</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</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;">$src_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now that the functions ready we&#8217;ll go on to making addpics.php. We need to start our session again and include our db_connect.php file. Then we&#8217;ll use the mkdir command to create our directories. We have to make sure they dont exist first also. The entire addpics.php code is below with comments.</p>
<p><strong>addpics.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<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: #b1b100;">include</span> <span style="color: #0000ff;">&quot;db_connect.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//make sure theres a session id</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//pull user info for this id</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT username FROM `users` WHERE `id`='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SESSION</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: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//record doesnt exist...redirect to index.php</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;">$res</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: #990000;">session_destroy</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;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>Javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
document.location.href='index.php'&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//pull entire row of info</span>
&nbsp;
<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//post value of title from the profilecp form</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//they must include a title</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>Javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
		alert(<span style="color: #000099; font-weight: bold;">\&quot;</span>You must choose a title for your picture!<span style="color: #000099; font-weight: bold;">\&quot;</span>)
		document.location.href='profilecp.php'&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>	
<span style="color: #666666; font-style: italic;">// the @ in front is to supress warning messages</span>
<span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//name of directory will be the username</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">@</span><span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//if directory does not exist, create it</span>
<span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/pics'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//store pictures in 'pics' subdirectory</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//check if pics subdirectory has been created, if not create it</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">@</span><span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">//Heres where we use the FILES variable</span>
<span style="color: #666666; font-style: italic;">//a double array with the name and then parameter ($_FILES['name']['param'])</span>
<span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pics'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pics'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'size'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//if you want to limit size</span>
<span style="color: #000088;">$pic</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pics'</span><span style="color: #009900;">&#93;</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;">$type</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pics'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//if you want to limit type (jpg,gif,png)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//insert the reference of file into the database				</span>
<span style="color: #000088;">$sql2</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO `user_photos` (`profile_id`,`title`,`size`,`type`,`reference`)
VALUES ('&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SESSION</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: #0000ff;">&quot;','<span style="color: #006699; font-weight: bold;">$title</span>','<span style="color: #006699; font-weight: bold;">$size</span>','<span style="color: #006699; font-weight: bold;">$type</span>','<span style="color: #006699; font-weight: bold;">$pic</span>'); &quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$res2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql2</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//move the file into the correct directory</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pics'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>Javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
	alert(<span style="color: #000099; font-weight: bold;">\&quot;</span>Your picture has been uploaded<span style="color: #000099; font-weight: bold;">\&quot;</span>)
	document.location.href='profilecp.php'&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">//if something goes wrong display and error message</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>Javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
	alert(<span style="color: #000099; font-weight: bold;">\&quot;</span>There was an error, try again<span style="color: #000099; font-weight: bold;">\&quot;</span>)
	document.location.href='profilecp.php'&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//create thumbnail and move it into the thumbnail directory</span>
<span style="color: #000088;">$target2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$target2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/pics'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$target2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/thumbs'</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: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">@</span><span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$target2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target2</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pics'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
createthumb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #339933;">,</span><span style="color: #000088;">$target2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">150</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">150</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>Javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
document.location.href='index.php'&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Addpics.php is done, now on to deletepics.php. We use the unlink function to delete the file from our web server.<br />
The entire <strong>deletepics.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location:profilecp.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;db_connect.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$profile_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$profile_id</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//no session id...redirect to index.php</span>
<span style="color: #009900;">&#123;</span>	
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>Javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
	alert(<span style="color: #000099; font-weight: bold;">\&quot;</span>You are not logged in!<span style="color: #000099; font-weight: bold;">\&quot;</span>);document.location.href='index.php';
	&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>            
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'files'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//for each picture that was checked</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//delete reference in database</span>
<span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM user_photos WHERE profile_id='<span style="color: #006699; font-weight: bold;">$profile_id</span>' AND reference='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/pics/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//delete pic in directory</span>
<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/pics/thumbs/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//delete thumbnail</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>Alright&#8230;almost there. Everything looks good for the user that logs in. But we need something that&#8217;ll show of our users profile to anyone that visits, whether they are registered or not. So we&#8217;ll make a profile.php. All it does is grab the username from the url, find that users info and output it all to the page. We&#8217;ll be using the GET method to pull from the url.<br />
<strong>profile.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<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: #b1b100;">include</span> <span style="color: #0000ff;">&quot;db_connect.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT * from `users` WHERE `username`='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$res</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<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;">$res</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: #666666; font-style: italic;">//no user with that username exists</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>Javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
	alert(<span style="color: #000099; font-weight: bold;">\&quot;</span>This user does not exist<span style="color: #000099; font-weight: bold;">\&quot;</span>)
	document.location.href='index.php'&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//pull the users info</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;html&gt;
	&lt;head&gt;&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;&lt;/head&gt;
&nbsp;
        //use php to echo whats stored in our database
	&lt;div class=&quot;divider&quot;&gt;
	&lt;strong&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>'s Profile&lt;/strong&gt;&lt;br/&gt;
	Name: <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first'</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;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;br/&gt;
	Email: <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;br/&gt;
	About: <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'about'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;br/&gt;
    &lt;/div&gt;
&nbsp;
	&lt;div class=&quot;divider&quot;&gt;
	&lt;strong&gt;Pictures&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//find this users pictures</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT reference FROM user_photos WHERE`profile_id`='&quot;</span><span style="color: #339933;">.</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: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//show the pictures thumbnail as a link to the full picture</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> MYSQL_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/pics/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reference'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
&lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/pics/thumbs/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row2</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reference'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;/a&gt;&lt;br/&gt;&quot;</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>So to test this out say we had a username of &#8216;bananas&#8217;. If we went to localhost/yoursite/profile.php?username=bananas the page that comes up should be bananas profile. To clean this up we can use mod_rewrite in our .htaccess file. With the code below you only need to go to localhost/yoursite/bananas and you should see that users profile.<br />
<strong>.htaccess</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">RewriteEngine on
RewriteRule ^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>_<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span>$ profile<span style="color: #339933;">.</span>php?username<span style="color: #339933;">=</span>$<span style="color:#800080;">1</span>
RewriteRule ^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>_<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>$ profile<span style="color: #339933;">.</span>php?username<span style="color: #339933;">=</span>$<span style="color:#800080;">1</span></pre></div></div>

<p>We&#8217;re finally done! Thats everything! You should have a fully working login system (hopefully). I&#8217;ve attached all of the source code to the post. It can be found below. If there are any bugs or errors please let me know. I hope this helps! Tell me how it works for you!<br />
<strong>Zip of all source code</strong><br />
<a href='http://bhaviksblog.com/wp-content/uploads/2009/02/php_login_system.zip'>php_login_system.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bhaviksblog.com/02/php-login-system-tutorial-part-3/feed/</wfw:commentRss>
		<slash:comments>87</slash:comments>
		</item>
		<item>
		<title>PHP Login System Tutorial &#8211; Part 2</title>
		<link>http://bhaviksblog.com/01/php-login-system-tutorial-part-2/</link>
		<comments>http://bhaviksblog.com/01/php-login-system-tutorial-part-2/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 03:59:12 +0000</pubDate>
		<dc:creator>Bhavik</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[error checking]]></category>
		<category><![CDATA[html form submit]]></category>
		<category><![CDATA[login system]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php validation]]></category>

		<guid isPermaLink="false">http://bhaviksblog.com/?p=17</guid>
		<description><![CDATA[Part two of my PHP Login System Tutorial. We'll go through sanitizing input to get rid SQL injection attempts, Setup a registration form where users can create an account, and a login form. We'll use PHP sessions to accomplish this.]]></description>
			<content:encoded><![CDATA[<p><a href="http://bhaviksblog.com/01/php-login-system-tutorial-part-1/">Link to part 1</a></p>
<p>So far we&#8217;ve made our database, connected to it, and wrote a function to get rid of<br />
SQL Injection attempts. Now we can make the registration page. </p>
<p>We&#8217;ll have a form with a couple fields and a submit button that sends everything to our database.</p>
<p>The first thing we&#8217;re going to do is include the db_connect.php page. This will connect us to our database and give us access to the protect function we created earlier.</p>
<p>This form will then submit to itself and insert the data into our db.</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;">//Create registration form (register.php)</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;db_connect.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// 'submit' hasn't been clicked so output html.</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So what the first IF statement does is checks to see if the form has been submitted. If it hasn&#8217;t it outputs the html form.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;form method=&quot;post&quot; action=&quot;register.php&quot;&gt;
First Name: &lt;input type=&quot;text&quot; name=&quot;first&quot;&gt;
Last Name: &lt;input type=&quot;text&quot; name=&quot;last&quot;&gt;
Desired Username: &lt;input type=&quot;text&quot; name=&quot;username&quot;&gt;
Password: &lt;input type=&quot;password&quot; name=&quot;password&quot;&gt;
Confirm Password: &lt;input type=&quot;password&quot; name=&quot;pass_conf&quot;&gt;
Email: &lt;input type=&quot;text&quot; name=&quot;email&quot;&gt;
About: &lt;textarea name=&quot;about&quot;&gt;Tell us about yourself&lt;/textarea&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Register&quot;&gt;
&lt;/form&gt;
or &lt;a href=&quot;index.php&quot;&gt;Login&lt;/a&gt;
&lt;/html&gt;</pre></div></div>

<p>Notice the forms action is register.php which is the page itself. When &#8216;submit&#8217; is posted we go to the else block here. This is where we store the posted values into variables. We call our protect function on these values to cleanse them of any injection attempts. </p>
<p><span id="more-17"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$last</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pass_conf</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass_conf'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$about</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'about'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now that we have the values the user entered, we can do some error checking. We&#8217;ll create an errors array and add an error message for every error that occurs to the array. The first one makes sure the email is in a valid name@domain format using regular expressions.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$regex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+.[a-z]{2,}$/i&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: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regex</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;E-mail is not in name@domain format!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The next block makes sure all of the fields have been filled out.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$first</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$last</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$username</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$password</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$pass_conf</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$email</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$about</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;You did not fill out the required fields&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This makes sure the passwords match</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$pass_conf</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Your confirmed password does not match you initial password&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we need to make sure the username this user wants isn&#8217;t already taken. To do this we&#8217;ll take the username they submitted and look for it in our db. If we find it, that username is taken so we output an error message.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM `users` WHERE `username`='<span style="color: #006699; font-weight: bold;">{$username}</span>'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<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;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Username already taken, please try another&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Heres where we loop through our errors array and output everything that went wrong with the registration.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The following errors occured with your registration&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;font color=&quot;</span>red<span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$error</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/font&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>javascript:history.go(-1)<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Try again&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//we use javascript to go back rather than reloading the page </span>
  <span style="color: #666666; font-style: italic;">// so the user doesn't have to type in all that info again.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If nothing went wrong we insert the new information into our database using SQL queries.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT into `users`(`first`,`last`,`username`,`password`,`email`,`about`)
  VALUES ('<span style="color: #006699; font-weight: bold;">$first</span>','<span style="color: #006699; font-weight: bold;">$last</span>','<span style="color: #006699; font-weight: bold;">$username</span>','&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;','<span style="color: #006699; font-weight: bold;">$email</span>','<span style="color: #006699; font-weight: bold;">$about</span>');&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Thank You for registering <span style="color: #006699; font-weight: bold;">{$first_name}</span>! Your username is <span style="color: #006699; font-weight: bold;">{$username}</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=&quot;</span>index<span style="color: #339933;">.</span>php<span style="color: #0000ff;">&quot;&gt; Click here &lt;/a&gt; to Login&quot;</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>So now that the user is registered, they need a place to login.<br />
We&#8217;re going to use the same if(!$_POST['submit']) idea. If submit hasn&#8217;t posted, output the html. If it has, query the database. We use the session_start() function to create a session. Once a login is succesful we can create session variables to reference this specific user for their session. This will come in handy in the next tutorial when we need to get this users pictures from the database. You can find more info on php sessions <a href="http://us3.php.net/manual/en/function.session-start.php" target="_new">here</a>.</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>
&nbsp;
<span style="color: #666666; font-style: italic;">//Login form (index.php)</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;db_connect.php&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;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;html&gt;
&lt;b&gt;&lt;Login&lt;/b&gt;
&lt;form method=&quot;post&quot; action=&quot;index.php&quot;&gt;
Username&lt;input type=&quot;text&quot; name=&quot;username&quot; maxlength=&quot;16&quot;&gt;
Password&lt;input type=&quot;password&quot; name=&quot;password&quot; maxlength=&quot;16&quot;&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Login&quot;&gt;
&lt;/form&gt;
&lt;a href=&quot;register.php&quot;&gt;Register Here&lt;/a&gt;
&lt;/html&gt;</pre></div></div>

<p>Ok here we&#8217;re going to make sure both the username and password were entered.  Then we&#8217;ll query our db with the posted values to see if theres a match. If there is, we create our session variables (to be used later) and take the user to their homepage. If the database couldn&#8217;t find anything we output an incorrect login message.</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: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//compare the encrypted password</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT id,username FROM `users` WHERE `username`='<span style="color: #006699; font-weight: bold;">$user</span>' AND `password`='<span style="color: #006699; font-weight: bold;">$pass</span>'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <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;">$query</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: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// mysql_fetch_assoc gets the value for each field in the row</span>
      <span style="color: #000088;">$_SESSION</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;">$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: #666666; font-style: italic;">//creates the first session var</span>
      <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// second session var</span>
&nbsp;
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script type=&quot;</span>text<span style="color: #339933;">/</span>javascript<span style="color: #0000ff;">&quot;&gt;window.location=&quot;</span>home<span style="color: #339933;">.</span>php<span style="color: #0000ff;">&quot;&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
   <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script type=&quot;</span>text<span style="color: #339933;">/</span>javascript<span style="color: #0000ff;">&quot;&gt;
    alert(&quot;</span>Username and password combination is incorrect<span style="color: #339933;">!</span><span style="color: #0000ff;">&quot;);
    window.location=&quot;</span>index<span style="color: #339933;">.</span>php<span style="color: #0000ff;">&quot;&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>			
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script type=&quot;</span>text<span style="color: #339933;">/</span>javascript<span style="color: #0000ff;">&quot;&gt;
    alert(&quot;</span>You need to gimme a username AND password<span style="color: #339933;">!!</span><span style="color: #0000ff;">&quot;);
    window.location=&quot;</span>index<span style="color: #339933;">.</span>php<span style="color: #0000ff;">&quot;&lt;/script&gt;&quot;</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>Now we need a place for our user to go to when they login successfully. Make another php file and call it &#8220;home.php&#8221;</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: #666666; font-style: italic;">//home.php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Welcome &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; &lt;a href=&quot;</span>logout<span style="color: #339933;">.</span>php<span style="color: #0000ff;">&quot;&gt;Logout&lt;/a&gt;&quot;</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;You don't belong here!&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>Finally we create a logout page which destroys our session.</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: #666666; font-style: italic;">//logout (logout.php)</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;db_connect.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #990000;">session_destroy</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;&lt;script type=&quot;</span>text<span style="color: #339933;">/</span>javascript<span style="color: #0000ff;">&quot;&gt;
    alert(&quot;</span>You have logged out<span style="color: #0000ff;">&quot;);
    window.location=&quot;</span>index<span style="color: #339933;">.</span>php<span style="color: #0000ff;">&quot;&lt;/script&gt;&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>That&#8217;s it for part 2. Check out <a href="http://bhaviksblog.com/02/php-login-system-tutorial-part-3/">Part 3</a> to learn how to allow the user to update their profile and add pictures. </p>
]]></content:encoded>
			<wfw:commentRss>http://bhaviksblog.com/01/php-login-system-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>43</slash:comments>
		</item>
		<item>
		<title>PHP Login System Tutorial &#8211; Part 1</title>
		<link>http://bhaviksblog.com/01/php-login-system-tutorial-part-1/</link>
		<comments>http://bhaviksblog.com/01/php-login-system-tutorial-part-1/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 03:52:46 +0000</pubDate>
		<dc:creator>Bhavik</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[login system]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://bhaviksblog.com/?p=13</guid>
		<description><![CDATA[This is Part 1 of the PHP Login System tutorial. In this part we will create our database and setup our tables. Then I'll show you how to actually connect to your mySQL database.]]></description>
			<content:encoded><![CDATA[<p>This is Part 1 of my PHP Login System tutorial. By the end of this series, you&#8217;ll have your own custom social networking site. Users will be able to register, login, edit their profile and add pictures. We&#8217;re aiming for a <em>very</em> stripped down version of Facebook and Myspace.</p>
<p>For this tutorial you&#8217;ll need a webserver running PHP and mySQL. Click <a href="http://bhaviksblog.com/01/xampp-setup/">here</a> for instructions on how to set one up on your local machine with XAMPP.</p>
<p>Lets start by making our database. Our users table will have 7 fields.<br />
 id, first, last, username, password, email and about.</p>
<p>Heres the SQL code to make this happen. You can paste it into your phpmyadmin SQL tab. Make sure you change the `db` in the first line to match the name of your database.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`db`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`first`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">32</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`last`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">32</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`username`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`password`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`email`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`about`</span> TEXT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> MYISAM;</pre></div></div>

<p>We make the id auto_increment so that every user that is added to the db has their own id. Since this field will be unique, we&#8217;ll make it our primary key. It will be used to reference each user later.</p>
<p>Our database is setup. We just need to connect to it using php. We&#8217;ll create our first php file, &#8220;db_connect.php&#8221;.</p>
<p><span id="more-13"></span><br />
PHP has built in functions to interact with mysql databases, <strong>mysql_connect</strong> and <strong>mysql_select_db</strong>.</p>
<p>The <strong>mysql_connect</strong> function takes 3 arguments, the host, username, and password. We&#8217;ll store the result of this function into a variable. If the connect fails you tell it to die and show the error output on the screen using <strong>mysql_error()</strong>.</p>
<p>If your connection is successful, you just have to select the database you want to<br />
interact with. The <strong>mysql_select_db</strong>(&#8220;name_of_your_db&#8221;,&#8221;the_connection_resource&#8221;) function will select your database from the successful connection we established previously.</p>
<p>Heres the php code</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;">$con</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span>localhost<span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;db&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$con</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>We&#8217;ve connected to the mysql server and selected our db.<br />
Im going to add a function into this page that we&#8217;ll be using later</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This function will clean what the users submit to the database so that we won&#8217;t need to worry about SQL Injection. I made it a function because typing &#8220;mysql_real_escape_string&#8221; every time you need to use it is a pain.</p>
<p> The entire db_connect.php code</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;">//db_connect.php</span>
<span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span>localhost<span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;db&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</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>Next will be <a href="http://bhaviksblog.com/01/php-login-system-tutorial-part-2/">Part 2</a> where we create the Login and Registration Page.</p>
]]></content:encoded>
			<wfw:commentRss>http://bhaviksblog.com/01/php-login-system-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>PHP Basics</title>
		<link>http://bhaviksblog.com/01/php-basics/</link>
		<comments>http://bhaviksblog.com/01/php-basics/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 03:46:10 +0000</pubDate>
		<dc:creator>Bhavik</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php basics]]></category>

		<guid isPermaLink="false">http://bhaviksblog.com/?p=10</guid>
		<description><![CDATA[This is a beginner tutorial on PHP. I'll show you how to declare basic variables, comments, printing to the screen, loops and mixing php and html together.]]></description>
			<content:encoded><![CDATA[<p>Heres a quick intro to PHP. A lot will be left out, I&#8217;m assuming you know basic programming (variables and loops). <a href="http://w3schools.com/php/default.asp" target="_new">W3 Schools</a> is a great place to learn PHP more in depth.</p>
<p>To try this code make sure you have XAMPP installed, check out <a href="http://bhaviksblog.com/01/xampp-setup/">this tutorial</a> if you haven&#8217;t set it up yet.</p>
<p>Every script should begin and end with the php open/close tags.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #666666; font-style: italic;">//Comments</span>
<span style="color: #666666; font-style: italic;">//rest of code here</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here are some examples</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// two backslashes are used for comments</span>
<span style="color: #666666; font-style: italic;">// A variable is created using the dollar sign before the name of the var</span>
<span style="color: #000088;">$v1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;bananas&quot;</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//semicolon after each statement</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//echo is used to print to the screen</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$v1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//You can print multiple items using a comma</span>
<span style="color: #000088;">$v2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;apples&quot;</span> <span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$v1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//If statements are similar to those in C++ and Java.</span>
<span style="color: #000088;">$uno</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$dos</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uno</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$dos</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;WOO!&quot;</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;WTF!?&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><span id="more-10"></span><br />
The open/close tags make it easier to mix html in a php page.<br />
If you were to echo your html out you would have to escape quotes like this (quite a pain)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;html&gt;n&quot;</span> <span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=&quot;</span>http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//www.site.com&quot;&gt;my site&lt;/a&gt;&quot; ;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;input type=&quot;</span>text<span style="color: #0000ff;">&quot; value=&quot;</span>hello<span style="color: #0000ff;">&quot; maxlength=&quot;</span><span style="color: #cc66cc;">6</span><span style="color: #0000ff;">&quot;&gt;&quot;</span> <span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The easier way to do it is to just close the php tag, type your html and open the php tag again. An example would be a php if statement.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<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: #339933;">!</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//if the form hasn't been submitted, output the html</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span> //close tag here and work in html
&lt;html&gt;
&lt;form name=&quot;myform&quot; method=&quot;post&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;box&quot;&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot;&gt;
&lt;/form&gt;
&lt;/html&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">//open the tag back up since we're back in php</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//do whatever in php here</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now lets go over loops</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//the usual for loop</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ha ha ha &quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//heres a foreach loop, used to loop through an array</span>
<span style="color: #000088;">$myarray</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;green&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$myarray</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$colors</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//you can use a &quot;.&quot; to splice vars and strings together</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Color: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$colors</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s it for the basics. Let&#8217;s start making websites! Click <a href="http://bhaviksblog.com/01/php-login-system-tutorial-part-1/">here</a> for Part 1 of my Login System Tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://bhaviksblog.com/01/php-basics/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

