<?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>PHP Tutorials By Kloplop321 &#187; sessions</title>
	<atom:link href="http://kloplop321.com/php-tutorials/index.php/tag/sessions/feed/" rel="self" type="application/rss+xml" />
	<link>http://kloplop321.com/php-tutorials</link>
	<description>PHP video tutorials, for everyone.</description>
	<lastBuildDate>Sun, 20 Mar 2011 19:03:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>PHP &amp; MySQL Tutorial 08: Mysql Login system concept</title>
		<link>http://kloplop321.com/php-tutorials/index.php/2010/03/12/php-mysql-tutorial-08-mysql-login-system-concept/</link>
		<comments>http://kloplop321.com/php-tutorials/index.php/2010/03/12/php-mysql-tutorial-08-mysql-login-system-concept/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 04:12:20 +0000</pubDate>
		<dc:creator>Kloplop321</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHPMyAdmin]]></category>
		<category><![CDATA[Select]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[sessions]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://kloplop321.com/php-tutorials/?p=190</guid>
		<description><![CDATA[This tutorial goes over the concept of a log in system with MySQL with sessions. (The sources are provided after the jump) One of my viewers sent me a message on youtube requesting this great example Dear kloplop321, I really want to make a php, mysql based game. I have an idea about how I [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial goes over the concept of a log in system with <a href="http://www.mysql.com/">MySQL</a> with <a href="http://en.wikipedia.org/wiki/Session_%28computer_science%29">sessions</a>. (The sources are provided after the jump)<br />
One of my viewers sent me a message on youtube requesting this great example</p>
<blockquote><p>Dear kloplop321,<br />
I really want to make a php, mysql based game. I have an idea about how I am going to do it. I would just really love some help with the SESSION login. There are tutorials on the internet but I find that they don&#8217;t show the full code or stuff like that. So if you would please create a tutorial on it I would apprecitate it. Thanks. </p></blockquote>
<p>This tutorial naturally has the <strong>sources provided</strong>. </p>
<p>The following videos(2) go over the concept of a login system in <a href="http://www.php.net/">PHP</a> while using sessions(to maintain the login status) and MySQL(a database that holds the information). I first go over how to make</p>
<ul>
<li>the login page</li>
<li>the table in the database</li>
<li>a user through <a href="http://www.phpmyadmin.net/home_page/index.php">PHPMyAdmin</a> <sub>[the next tutorial will likely go over registering users]</sub></li>
<li>code how to validate that user and if they are validated</li>
<li>if so, set the session information</li>
<li>if acceptable, they can go to a &#8220;members only&#8221; page <sub>[which only allows identified people in]</sub></li>
<li>a way to log out</li>
</ul>
<p>Again: this tutorial is pretty much a walk-through in concept of a mysql login system.<br />
<object type="application/x-shockwave-flash" style="width: 800px; height: 480px;" data="http://www.youtube.com/v/epo2qPGxZiE&amp;hl=en_US&amp;fs=1&amp;color1=0x006699&amp;color2=0x54abd6"><param name="movie" value="http://www.youtube.com/v/epo2qPGxZiE&amp;hl=en_US&amp;fs=1&amp;color1=0x006699&amp;color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param></object></p>
<p>Part 2 (the most important) and the sources after the jump.<br />
<span id="more-190"></span></p>
<p><object type="application/x-shockwave-flash" style="width:800px; height:480px;" data="http://www.youtube.com/v/ir12sxw8lMs&amp;hl=en_US&amp;fs=1&amp;color1=0x006699&amp;color2=0x54abd6"><param name="movie" value="http://www.youtube.com/v/ir12sxw8lMs&amp;hl=en_US&amp;fs=1&amp;color1=0x006699&amp;color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param></object></p>
<p>And now the sources.<br />
<strong>include.php</strong></p>
<p><code><span style="color: #000000"><br />
<span style="color: #0000BB">&lt;?php<br /><a href="http://php.net/manual/en/function.session-start.php">session_start</a></span><span style="color: #007700">();</span><span style="color: #FF8000">//start&nbsp;the&nbsp;session<br />//now&nbsp;to&nbsp;do&nbsp;the&nbsp;MySQL&nbsp;connection.<br /></span><span style="color: #0000BB">$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB"><a href="http://php.net/manual/en/function.mysql-connect.php">mysql_connect</a></span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'phpuser'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'phppass'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$link</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Could&nbsp;not&nbsp;connect:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB"><a href="http://php.net/manual/en/function.mysql-error.php">mysql_error</a></span><span style="color: #007700">());<br />}<br /></span><span style="color: #FF8000">//WE&nbsp;NEED&nbsp;to&nbsp;select&nbsp;the&nbsp;database!<br /></span><span style="color: #0000BB"><a href="http://www.php.net/manual/en/function.maxdb-select-db.php">mysql_select_db</a></span><span style="color: #007700">(</span><span style="color: #DD0000">"tutorials_login"</span><span style="color: #007700">);</p>
<p></span><span style="color: #0000BB">?&gt;</span></p>
<p></span><br />
</code></p>
<p><strong>index.php</strong></p>
<p><code><span style="color: #000000"><br />
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700"><a href="http://www.php.net/manual/en/function.include.php">include</a>(</span><span style="color: #DD0000">"include.php"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//see&nbsp;if&nbsp;the&nbsp;person&nbsp;is&nbsp;trying&nbsp;to&nbsp;log&nbsp;in&nbsp;now..<br /></span><span style="color: #007700">if(<a href="http://www.php.net/manual/en/function.isset.php">isset</a>(</span><span style="color: #0000BB">$_REQUEST</span><span style="color: #007700">[</span><span style="color: #DD0000">'sub'</span><span style="color: #007700">])){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB"><a href="http://www.php.net/manual/en/function.trim.php">trim</a></span><span style="color: #007700">(</span><span style="color: #0000BB">$_REQUEST</span><span style="color: #007700">[</span><span style="color: #DD0000">'username'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$_REQUEST</span><span style="color: #007700">[</span><span style="color: #DD0000">'password'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//make&nbsp;it&nbsp;safe&nbsp;to&nbsp;see&nbsp;if&nbsp;in&nbsp;the&nbsp;table<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB"><a href="http://www.php.net/manual/en/function.mysql-real-escape-string.php">mysql_real_escape_string</a></span><span style="color: #007700">(</span><span style="color: #0000BB">$username</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB"><a href="http://www.php.net/manual/en/function.md5.php">md5</a></span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//time&nbsp;to&nbsp;query<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;*&nbsp;FROM&nbsp;`users`&nbsp;WHERE&nbsp;username&nbsp;=&nbsp;'</span><span style="color: #0000BB">$username</span><span style="color: #DD0000">'&nbsp;AND&nbsp;password&nbsp;=&nbsp;'</span><span style="color: #0000BB">$password</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB"><a href="http://www.php.net/manual/en/function.mysql-query.php">mysql_query</a></span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$exists&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$userid&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;while(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB"><a href="http://www.php.net/manual/en/function.mysql-fetch-array.php">mysql_fetch_array</a></span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$exists&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$userid&nbsp;</span><span style="color: #007700">=&nbsp;(int)</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;</span><span style="color: #FF8000">//get&nbsp;out&nbsp;of&nbsp;the&nbsp;while&nbsp;loop<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$exists</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'isin'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'userid'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$userid</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"You&nbsp;got&nbsp;something&nbsp;wrong,&nbsp;try&nbsp;again&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//echo&nbsp;$sql;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />}</span><span style="color: #FF8000">//chopsuey</p>
<p>//detect&nbsp;if&nbsp;logged&nbsp;in&nbsp;via&nbsp;variable&nbsp;in&nbsp;the&nbsp;session<br /></span><span style="color: #007700">if(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'isin'</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//we&nbsp;are&nbsp;logged&nbsp;in.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Hey&nbsp;there,&nbsp;you&nbsp;seem&nbsp;to&nbsp;be&nbsp;logged&nbsp;in,<br />&nbsp;&nbsp;&nbsp;&nbsp;Would&nbsp;you&nbsp;like&nbsp;to&nbsp;go&nbsp;to&nbsp;the&nbsp;&lt;a&nbsp;href=\"membersonly.php\"&gt;Members&nbsp;Only&lt;/a&gt;&nbsp;area?&lt;br&nbsp;/&gt;&nbsp;Or&nbsp;would&nbsp;you&nbsp;want&nbsp;to<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;a&nbsp;href=\"logout.php\"&gt;log&nbsp;out&lt;/a&gt;?"</span><span style="color: #007700">;<br />}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//show&nbsp;the&nbsp;login&nbsp;prompt<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;form&nbsp;action="?"&nbsp;method="post"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Username&nbsp;&lt;input&nbsp;type="text"&nbsp;name="username"&nbsp;/&gt;&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Password&nbsp;&lt;input&nbsp;type="password"&nbsp;name="password"&nbsp;/&gt;&nbsp;&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="submit"&nbsp;value="Log&nbsp;in"&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="hidden"&nbsp;name="sub"&nbsp;value="1"&nbsp;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span></p>
<p></span><br />
</code>logout.php</p>
<p><code><span style="color: #000000"><br />
<span style="color: #0000BB">&lt;?php<br />session_start</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">session_destroy</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"location:&nbsp;index.php"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span><br />
</span><br />
</code></p>
<p><strong>membersonly.php</strong></p>
<p><code><span style="color: #000000"><br />
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include(</span><span style="color: #DD0000">"include.php"</span><span style="color: #007700">);<br />if(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'isin'</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Hey&nbsp;there&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;*&nbsp;FROM&nbsp;users&nbsp;WHERE&nbsp;id&nbsp;=&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'userid'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;while(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'username'</span><span style="color: #007700">];</span><span style="color: #FF8000">//hey&nbsp;there&nbsp;pickles<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"&nbsp;Would&nbsp;you&nbsp;like&nbsp;to&nbsp;&lt;a&nbsp;href=\"index.php\"&gt;go&nbsp;back&lt;/a&gt;?&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//you&nbsp;don't&nbsp;belong&nbsp;here,&nbsp;kick&nbsp;back&nbsp;to&nbsp;the&nbsp;index<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"location:&nbsp;index.php"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></p>
<p></span><br />
</code></p>
<p>Also a tid bit of information: I did this in <a href="http://www.linux.org/">Linux</a>, and I recorded it in 1080p HD!</p>
]]></content:encoded>
			<wfw:commentRss>http://kloplop321.com/php-tutorials/index.php/2010/03/12/php-mysql-tutorial-08-mysql-login-system-concept/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

