<?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>Tayyab Bin Tariq &#187; PHP Login</title>
	<atom:link href="http://tayyab.xenoglaux-solutions.com/tag/php-login/feed/" rel="self" type="application/rss+xml" />
	<link>http://tayyab.xenoglaux-solutions.com</link>
	<description>I like sharing what i know</description>
	<lastBuildDate>Fri, 21 May 2010 20:59:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating Simple Member Login Area Using PHP</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/03/31/creating-simple-member-login-area-using-php/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/03/31/creating-simple-member-login-area-using-php/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 15:54:59 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[PHP & MySQL]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Cookie Login]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[PHP Authentication]]></category>
		<category><![CDATA[PHP Login]]></category>
		<category><![CDATA[PHP Members Area]]></category>

		<guid isPermaLink="false">http://tayyabtariq.wordpress.com/?p=11</guid>
		<description><![CDATA[This tutorial is aimed at creating a simple login/members area using PHP MySQL.
I will waste no time and get straight down to business. The tutorial is based on 6 easy steps.
Step 1:
Creating a table in the database.
I have created a very simple table that has only two columns; username and password.
Here is the SQL:
CREATE TABLE [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial is aimed at creating a simple login/members area using PHP MySQL.<span id="more-11"></span></p>
<p>I will waste no time and get straight down to business. The tutorial is based on 6 easy steps.</p>
<p><strong>Step 1:</strong></p>
<p>Creating a table in the database.</p>
<p>I have created a very simple table that has only two columns; username and password.</p>
<p>Here is the SQL:</p>
<blockquote><p>CREATE TABLE `userstable` (<br />
`userName` varchar(20) NOT NULL default &#8221;,<br />
`password` varchar(20) NOT NULL default &#8221;,<br />
PRIMARY KEY  (`userName`)<br />
) ENGINE=MyISAM DEFAULT CHARSET=latin1;</p></blockquote>
<p><strong>Step 2:</strong></p>
<p>Create a connection to the database. Although the file below is meant only for a database connection I have taken the liberty of using it to save some general purpose settings as well.</p>
<blockquote><p>//dbConfig.php</p>
<p>&lt;?php<br />
/*</p>
<ul>
<li>The config file for the database connection and variables</li>
<li>change the database name and username and password</li>
<li>/</li>
</ul>
<p>//variables for the databse connection<br />
$serverName = &#8220;localhost&#8221;;<br />
$userName = &#8220;manager&#8221;;<br />
$password = &#8220;&#8221;;<br />
$dbName = &#8220;testdatabase&#8221;;</p>
<p>$conn = mysql_pconnect($serverName, $userName, $password);<br />
if (!$conn)<br />
{<br />
//print error message, the echo command support html encodeing.<br />
echo(&#8217;The connection to the database could not be established!&#8217;);<br />
die(&#8217;The connection to the database could not be established!&#8217;);<br />
}<br />
else<br />
{<br />
// select the database which you wish to opereate<br />
mysql_select_db($dbName);</p>
<p>}</p>
<p>// other variable<br />
$loginSuccess = &#8220;phpMembersArea.php&#8221;;<br />
$RegisterSuccess = &#8220;phpRegister.php?op=thanks&#8221;;<br />
$loginRequired = &#8220;phpLogin.php?op=loginFirst&#8221;;<br />
$timeout = 3600;<br />
$authenticatioMethod = &#8220;cookie&#8221;;</p>
<p>function CheckLogin() {</p>
<p>// fucntion that checks if some is logged in or not<br />
global $authenticatioMethod;<br />
if (strcmp($authenticatioMethod ,&#8221;cookie&#8221;) == 0)//we have choosen to use cookies<br />
{</p>
<p>if (!isset($_COOKIE["login"]))<br />
{<br />
return null;<br />
}<br />
else<br />
{<br />
return $_COOKIE["login"];</p>
<p>}<br />
}<br />
else// we are using session based authentication<br />
{<br />
if (!$_Session["userID"] || $_Session["valid_expire_time"] &lt; time())<br />
{<br />
$_Session["userID"]  = null;<br />
$_Session["valid_expire_time"] = time()-1;<br />
session_destroy();<br />
return null;<br />
}<br />
else<br />
{<br />
return $_Session["userID"];<br />
}<br />
}<br />
return null;<br />
}<br />
function RedirectTo($url)<br />
{<br />
//I have to use this function because the php function header can only be called<br />
// if no output has been sent<br />
// this solution uses java scripts so beware<br />
echo(&#8217;&lt;script type=&#8221;text/javascript&#8221;&gt; document.location = &#8221;.$url.&#8221;; &lt;/script&gt;&#8217;);<br />
}<br />
?&gt;</p></blockquote>
<p><strong>Step 3:</strong></p>
<p>Create a Registeration Page.</p>
<p>I provide a simple registeration page, where you can enter two fields, username and password.</p>
<blockquote><p>//phpRegister.php<br />
&lt;!DOCTYPE HTML PUBLIC &#8220;-//W3C//DTD HTML 4.01 Transitional//EN&#8221;&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=UTF-8&#8243;&gt;<br />
&lt;title&gt;&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;form action=&#8221;?op=register&#8221; method=&#8221;POST&#8221;&gt;<br />
Username: &lt;input name=&#8221;userID&#8221; MAXLENGTH=&#8221;16&#8243;&gt;&lt;br /&gt;<br />
Password: &lt;input type=&#8221;password&#8221; name=&#8221;password&#8221; MAXLENGTH=&#8221;16&#8243;&gt;&lt;br /&gt;<br />
&lt;input type=&#8221;submit&#8221;&gt;<br />
&lt;/form&gt;<br />
&lt;?php<br />
// first we include the dbconfig file<br />
// note that we do not need to open the connection<br />
// when the file is included and this page is loaded, the code is automatically executed<br />
echo(&#8217;hello&#8217;);<br />
include (&#8221;dbConfig.php&#8221;);</p>
<p>/*</p>
<ul>
<li>Here is the plan of action, this is the page that contains the registeration form</li>
<li>also this is the page that will connect to the database and execute the insert query</li>
<li>so we need to know if the page is being run from a</li>
<li>/</li>
</ul>
<p>// we suppose that a variable named op would be passed with a value<br />
// register if there is a registeration request.<br />
if ($_GET["op"] == &#8220;register&#8221;)<br />
{</p>
<p>if (!($userID = $_POST["userID"]) )<br />
{<br />
echo(&#8217;UserName field is missing!&#8217;);<br />
}<br />
else if (!($password = $_POST["password"]))<br />
{<br />
echo(&#8217;Password field is missing!&#8217;);<br />
}</p>
<p>$query = &#8220;INSERT INTO `USERSTABLE` VALUES( &#8216;&#8221;.$_POST["userID"].&#8221;&#8216;, Password(&#8217;&#8221;.$_POST["password"].&#8221;&#8216;))&#8221;;<br />
//note that my sql requires the password filed to be casted<br />
// also note that . is the string concatenation operator<br />
// also not the &#8220; around the table name</p>
<p>$result = mysql_query($query, $conn);<br />
//you can also redirect to a different page</p>
<p>if (!$result )<br />
{<br />
// make sure that the user was inserted<br />
echo(&#8217;&lt;br&gt;&lt;h2&gt;The user could not be inserted&lt;br&gt;&#8217;);<br />
echo(&#8217;The error cant be displayed&lt;/h2&gt;&#8217;);<br />
}<br />
else<br />
{</p>
<p>REdirectTo($RegisterSuccess);<br />
}</p>
<p>}<br />
else if ($_GET["op"] == &#8220;thanks&#8221;)<br />
{<br />
echo(&#8217;&lt;br&gt;&lt;h2&gt;The user was added successfully!&lt;/h2&gt;&#8217;);<br />
}<br />
//The web form for input ability</p>
<p>?&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p></blockquote>
<p>After this step you have successfully created a user account. All you need to do now is to create a login page.</p>
<p><strong>Step 4:</strong></p>
<p>The plan of action for our login page is that we pass it an query string argument, &#8216;op&#8217; that dictates what the page does. If the value of this variable is &#8216;login&#8217; the page gets the POST information and tries to login.</p>
<blockquote><p>//phpLogin.php</p>
<p>&lt;?php<br />
include (&#8217;dbConfig.php&#8217;);<br />
if ($_GET["op"] == &#8220;login&#8221;)//check if this is a login request<br />
{<br />
$query = &#8220;Select * from `userstable` where `userName`=&#8217;&#8221;.$_POST["userID"].&#8221;&#8216; AND `password`=Password(&#8217;&#8221;.$_POST["password"].&#8221;&#8216;) &#8220;;</p>
<p>$result = mysql_query($query, $conn);<br />
$obj = @mysql_fetch_object($result);<br />
if ($obj)<br />
{<br />
// means sucessful login<br />
$loginSucessful = 1;<br />
//create session variables<br />
// i will create both a login cookie<br />
// and session variables<br />
// and show how to use both for authentication</p>
<p>$_SESSION["valid_userID"] = $_POST["userID"];<br />
$_SESSION["valide_time"] = time();<br />
$_SESSION["valid_expire_time"] = time()+$timeout;</p>
<p>//set the cookies<br />
//i create a cookie where i set the cookie information to the user name<br />
// the userID can be encrypted also for better security.<br />
setcookie(&#8221;login&#8221;, $_POST["userID"], time()+$timeout);</p>
<p>// create a cookie<br />
}<br />
else<br />
{<br />
$loginSucessful = 0;<br />
}</p>
<p>if ($loginSucessful == 1)<br />
{<br />
//login has succeeded proceed to members area<br />
if ($_GET["referrer"] )<br />
{<br />
// if we were sent to the login page due to some request of a members area page<br />
// go to that page<br />
RedirectTo($_POST["referrer"]);<br />
}<br />
else<br />
{<br />
RedirectTo($loginSuccess);<br />
}<br />
}<br />
else<br />
{<br />
echo(&#8217;&lt;br&gt;&lt;h2&gt;Login Information is not correct&lt;br&gt;&#8217;.$_POST["userID"].&#8217; does not exist or password is incorrect&lt;/h2&gt;&#8217;);<br />
}<br />
}<br />
else if($_GET["op"] == &#8220;loginFirst&#8221;)<br />
{<br />
echo(&#8217;&lt;br&gt;&lt;h2&gt;You must login first.&lt;/h2&gt;&#8217;);<br />
}<br />
?&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=UTF-8&#8243;&gt;<br />
&lt;title&gt;&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?php echo(&#8221; &lt;form action=&#8221;?op=login&#8221; method=&#8221;POST&#8221;&gt;&#8221;); ?&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;<br />
UserID:<br />
&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;input type = &#8220;text&#8221; name=&#8221;userID&#8221; maxlength=&#8221;16&#8243;&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;<br />
Password:<br />
&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;input type = &#8220;password&#8221; name=&#8221;password&#8221; maxlength=&#8221;16&#8243;&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;</p>
<p>&lt;/td&gt;<br />
&lt;td&gt;<br />
&lt;input type =&#8221;submit&#8221; name=&#8221;subit&#8221; maxlength=&#8221;16&#8243;&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/form&gt;</p>
<p>&lt;/body&gt;<br />
&lt;/html&gt;</p></blockquote>
<p>Caution: There must be nothing before the &#8216;&lt;php&#8217; at the start of the page, if there is something the cookie would not be created. It took me some time to figure this out. Keeping this in mind can save a lot of time. Even an empty line or a space can mess things up.</p>
<p>PS: When a members area page is accessed without being logged in, the page redirects to the login page giving it a &#8216;referrer&#8217; argument. I however could not make it work, if anyone can help please look at:</p>
<p>&lt;?php echo(&#8221; &lt;form action=&#8221;?op=login&#8221; method=&#8221;POST&#8221;&gt;&#8221;); ?&gt;</p>
<p>I was trying to replace this with</p>
<p>&lt;?php echo(&#8221; &lt;form action=&#8221;?op=login&amp;referrer=&#8221; . $_GET["referrer"]  . &#8220;&#8221; method=&#8221;POST&#8221;&gt;&#8221;); ?&gt;</p>
<p>However, whenever there was a valid referrer the username and password is not verified, the page works fine otherwise.</p>
<p><strong>Step 5:</strong></p>
<p>The members area page.</p>
<blockquote><p>//phpMembersArea.php</p>
<p>&lt;?php<br />
session_start();<br />
include (&#8217;dbConfig.php&#8217;);<br />
$loggedInUser = CheckLogin();<br />
if (!$loggedInUser)// no one is logged in<br />
{<br />
echo(&#8217;You are not logged in&#8217;);<br />
RedirectTo(&#8221;phpLogin.php?referrer=phpMembersArea.php&#8221;);<br />
}<br />
else<br />
{<br />
echo(&#8217;&lt;h2&gt;Welcome &#8216;.$loggedInUser.&#8217;!&lt;/h2&gt;&lt;br&gt;&#8217;);<br />
}<br />
?&gt;<br />
&lt;!DOCTYPE HTML PUBLIC &#8220;-//W3C//DTD HTML 4.01 Transitional//EN&#8221;&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=UTF-8&#8243;&gt;<br />
&lt;title&gt;&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;This is the members area&lt;/h1&gt;<br />
&lt;br&gt;<br />
&lt;br&gt;<br />
&lt;a href = &#8220;phpLogout.php&#8221;&gt;Logout&lt;/a&gt;<br />
&lt;?php</p>
<p>?&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p></blockquote>
<p>The page calls the CheckLogin() function defined in the dbConfig.php. This function checks the <a href="http://w3schools.com/php/php_cookies.asp" target="_blank">cookie</a> or the <a href="http://w3schools.com/php/php_sessions.asp" target="_blank">session variable </a>for login information. If this information is found the login proceeds otherwise the user is redirected to the login page.</p>
<p><strong>Step 6:</strong></p>
<p>The logout page simply deletes the <a href="http://w3schools.com/php/php_cookies.asp" target="_blank">cookie </a>and the <a href="http://w3schools.com/php/php_sessions.asp" target="_blank">session</a>.</p>
<blockquote><p>//phpLogout.php</p>
<p>&lt;?php<br />
setcookie(&#8221;login&#8221;, &#8220;&#8221;, time()-100);<br />
include (&#8217;DbConfig.php&#8217;);</p>
<p>RedirectTo(&#8221;index.php&#8221;);<br />
?&gt;<br />
&lt;!&#8211;<br />
To change this template, choose Tools | Templates<br />
and open the template in the editor.<br />
&#8211;&gt;<br />
&lt;!DOCTYPE HTML PUBLIC &#8220;-//W3C//DTD HTML 4.01 Transitional//EN&#8221;&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=UTF-8&#8243;&gt;<br />
&lt;title&gt;&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p></blockquote>
<p>And there you go, you are done.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/03/31/creating-simple-member-login-area-using-php/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/03/31/creating-simple-member-login-area-using-php/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/03/31/creating-simple-member-login-area-using-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

