<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Yes Alimul Speaking &#187; login script</title>
	<atom:link href="http://alimul.wordpress.com/tag/login-script/feed/" rel="self" type="application/rss+xml" />
	<link>http://alimul.wordpress.com</link>
	<description>This blog is still actually for myself. It can help you having coding trips on web development. If that than really that was not my trying.</description>
	<lastBuildDate>Mon, 06 Jul 2009 20:16:03 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='alimul.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/ca233e52d2cb0104b2fc0393d7d1af46?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Yes Alimul Speaking &#187; login script</title>
		<link>http://alimul.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://alimul.wordpress.com/osd.xml" title="Yes Alimul Speaking" />
		<item>
		<title>Regular login script using session</title>
		<link>http://alimul.wordpress.com/2008/10/20/regular-login-script-using-session/</link>
		<comments>http://alimul.wordpress.com/2008/10/20/regular-login-script-using-session/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 19:59:02 +0000</pubDate>
		<dc:creator>Md. Alimul Razi</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[login script]]></category>

		<guid isPermaLink="false">http://alimul.wordpress.com/?p=48</guid>
		<description><![CDATA[PHP login script is the thing about which you find lots of people searching it. Specially the people who are new to the PHP language it is very important for them to learn user authentication stuff. So I am gonna take this initiative to post a secure php login script and explain it.
For your information [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alimul.wordpress.com&blog=1090347&post=48&subd=alimul&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>PHP login script is the thing about which you find lots of people searching it. Specially the people who are new to the PHP language it is very important for them to learn user authentication stuff. So I am gonna take this initiative to post a secure php login script and explain it.<br />
For your information we will be using sessions for login. I will mostly emphasize on PHP coding but will give specifications about the html login form. So first things first, make a login form with two fields and one login button. First field must be named &#8220;username&#8221; and second must be named &#8220;password&#8221;. Post form using method=&#8221;POST&#8221; and action must be another page lets say called doLogin.php which will actually contain the login PHP code.</p>
<p>Now before going further into the PHP lets see out MySQL tables&#8217; details which we will be using for login purpose. We will be using two table, one for actual user listing and second will contain the user types available with their IDs, those IDs from second table will be used in first one to specify each user&#8217;s type or privileges level.</p>
<p>Table One Details:-<br />
Table Name: user<br />
Table fields are as follows:-<br />
<span style="color:#993300;">userID            INT                    Auto-Increment</span><br />
<span style="color:#993300;">userName        Varchar(100)        Must be unique, used for login</span><br />
<span style="color:#993300;">password        Varchar(100)        Must contain MD5 encrypted password</span><br />
<span style="color:#993300;">userStatusID    INT(3)                The user type ID coming from &#8220;userstatus&#8221; table and will be used to assign previllages.</span><br />
<span style="color:#993300;">email            Varchar(100)</span><br />
<span style="color:#993300;">userActive        TINYINT(2)            An optional field but if you choose to use it you can disable users from being login.</span><br />
<span style="color:#993300;">Name            Varchar(45)            User&#8217;s Actual First and Second Name</span><br />
<span style="color:#993300;">Country            Varchar(45)</span><br />
<span style="color:#993300;">Gender            Char(1)                &#8216;M&#8217; for Male and &#8216;F&#8217; for Female</span></p>
<p>Table Two Details:-<br />
Table Name: userstatus<br />
Table fields are as follows:-<br />
<span style="color:#993300;">userStatusID        INT(3)        Auto-Increment</span><br />
<span style="color:#993300;">userStatus            Varchar(40)</span></p>
<p>Sample data for table two aka userstatus<br />
<span style="color:#993300;">1        Admin</span><br />
<span style="color:#993300;">2        Operator</span><br />
<span style="color:#993300;">3        Visitor</span></p>
<p>Before jumping on to the PHP actual authentication script next step for us to populate the tables. For data insertion into login table you will yourself design the form and PHP script I will just guide you with some most important things.</p>
<p>As I have given sample data for table two you will have to similarly do some brainstorming for identify how many user types you are gonna support in your login script and will have to just put in to &#8220;userstatus&#8221; table as given in above sample. After you are done with it now its time to make a form and php script to populate table one called &#8220;user&#8221;.</p>
<p>While entering data into table one &#8220;user&#8221; choose user type of user by entering the &#8220;userStatusID&#8221; from userstatus table to &#8220;userStatus&#8221; field of &#8220;user&#8221; table. I mean if you are gonna enter a user who is &#8220;operator&#8221; so according to my sample data you will set the &#8220;userstatus&#8221; field of &#8220;user&#8221; table to &#8216;2&#8242;.</p>
<p>&#8216;userActive&#8217; field must be set &#8216;1&#8242; for an active user and &#8216;0&#8242; for disabled user, which you don&#8217;t want this time to be able to get login.</p>
<p>Data in password field must be encrypted with &#8216;MD5&#8242; encryption before entering it to the table. This can be easily done by a &#8216;md5()&#8217; php function which will give you an encrypted string.<br />
e.g see following how data is being encrypted while getting from user insertion form.</p>
<p><span style="color:#cc0000;">$password=md5($_POST['password']);</span></p>
<p>Now data in this $password variable will be entered in &#8216;user&#8217; table&#8217;s &#8216;password&#8217; field. As the &#8216;MD5&#8242; encryption is the one way encryption which can&#8217;t be decrypted so even some one looking into db can&#8217;t retrieve the password of the user. While user will get login you will again decrypt the password entered by user with md5 and compare it to md5 decrypted string coming from database to verify the correctness of password.</p>
<p>Now after making login form exactly according to specifications given above, lemme mention them again here, which are as follows.</p>
<p>Make a login form with two fields and one login button. First field must be named &#8220;username&#8221; and second must be named &#8220;password&#8221;. Post form using method=&#8221;POST&#8221; and action must be another page lets say called doLogin.php which will actually contain the login PHP code.<br />
Its time to write the code in doLogin.php which will be as follows.</p>
<p><span style="color:#990000;">&lt;?php</span><br />
<span style="color:#990000;"> session_start();</span><br />
<span style="color:#006600;">//    Following Include File called &#8220;config.inc.php&#8221; contains DB connection code so make such and include it here</span><br />
<span style="color:#990000;"> include_once(&#8220;inc/config.inc.php&#8221;);</span><br />
<span style="color:#990000;"> </span><br />
<span style="color:#990000;"> $userName = $_POST["username"];</span><br />
<span style="color:#990000;"> $password = $_POST["password"];</span><br />
<span style="color:#990000;"> $errMsg=&#8221;";</span><br />
<span style="color:#990000;"> if($userName != &#8220;&#8221; &amp;&amp; $password != &#8220;&#8221;)</span><br />
<span style="color:#990000;"> {</span><br />
<span style="color:#990000;"> $encryptPassword = md5($password);</span><br />
<span style="color:#990000;"> $authSql = &#8220;SELECT userID,userStatusID FROM user</span><br />
<span style="color:#990000;"> WHERE userName = &#8216;&#8221;.$userName.&#8221;&#8216;</span><br />
<span style="color:#990000;"> AND password = &#8216;&#8221;.$encryptPassword.&#8221;&#8216;</span><br />
<span style="color:#990000;"> AND userActive = 1&#8243;;</span><br />
<span style="color:#990000;"> </span><br />
<span style="color:#990000;"> $authResult = mysql_query($authSql)OR die(&#8216;Couldn\&#8217;t Authenticate Visitor:&#8217;.mysql_error());</span><br />
<span style="color:#990000;"> $authRow = mysql_fetch_array($authResult);</span><br />
<span style="color:#990000;"> $userID= $authRow['userID'];</span><br />
<span style="color:#990000;"> $userStatus= $authRow['userStatusID'];</span><br />
<span style="color:#990000;"> </span><br />
<span style="color:#990000;"> if ($userID &gt; 0)<span style="color:#006600;">//If user with this username and password actually fetched from db</span></span><br />
<span style="color:#990000;"> {</span><br />
<span style="color:#990000;"> $_SESSION['USER_ID'] = $userID;</span><br />
<span style="color:#990000;"> $_SESSION['USR_LOGIN'] = $userName;</span><br />
<span style="color:#990000;"> $_SESSION['USR_STATUS'] = $userStatus;</span><br />
<span style="color:#990000;"> <span style="color:#006600;">//Redirect user after being login to a page where you want.</span></span><br />
<span style="color:#990000;"> header(&#8220;Location: mainPage.html&#8221;);</span><br />
<span style="color:#990000;"> }</span><br />
<span style="color:#990000;"> else<span style="color:#006600;"> //User have entered either password or username wrong or he is disabled or does not exist.</span></span><br />
<span style="color:#990000;"> {</span><br />
<span style="color:#990000;"> header(&#8220;Location: login.html?msg=invalid&#8221;);</span><br />
<span style="color:#990000;"> }</span><br />
<span style="color:#990000;"> }</span><br />
<span style="color:#990000;"> else</span><br />
<span style="color:#990000;"> {</span><br />
<span style="color:#006600;">//        The value of user name or password not entered</span><br />
<span style="color:#990000;"> header (&#8220;Location: login.html?msg=missing&#8221;);</span><br />
<span style="color:#990000;"> }</span><br />
<span style="color:#990000;">?&gt;</span></p>
<p>Up-till now we have complete the initial login process but one small but extremely important thing still remains is to check on every page that if user came here after getting login or just putted in the address. If he/she didn&#8217;t came through the login then he/she must be redirected to the login page. So include following small piece of code in beginning for every page before every thing else and it will do it for us.</p>
<p><span style="color:#990000;">&lt;?php</span><br />
<span style="color:#990000;">session_start();</span><br />
<span style="color:#990000;">if (isset($_SESSION['USR_LOGIN'])==&#8221;")</span><br />
<span style="color:#990000;"> {</span><br />
<span style="color:#990000;"> header(&#8220;Location:login.html?msg=Login_Required&#8221;);</span><br />
<span style="color:#990000;"> }</span><br />
<span style="color:#990000;">?&gt;</span></p>
<p>Now this will redirect user to login page if he/she didn&#8217;t came after login.</p>
<p>While login process we had set another session variable called $_SESSION['USR_STATUS'] this will actually help us to enforce privileges after login.</p>
<p>For example a piece of content of page is if only for admin and as admin&#8217;s user status ID is &#8216;1&#8242; we will put a simple check.</p>
<p><span style="color:#990000;">&lt;?php</span><br />
<span style="color:#990000;">session_start();</span><br />
<span style="color:#990000;">if (isset($_SESSION['USR_STATUS'])==1)</span><br />
<span style="color:#990000;"> {</span><br />
<span style="color:#006600;"> //display admin content here</span><br />
<span style="color:#990000;"> }</span><br />
<span style="color:#990000;">?&gt; </span></p>
<p>Or for an operator</p>
<p><span style="color:#cc0000;">&lt;?php</span><br />
<span style="color:#cc0000;">session_start();</span><br />
<span style="color:#cc0000;">if (isset($_SESSION['USR_STATUS'])==2)</span><br />
<span style="color:#cc0000;"> {</span><br />
<span style="color:#006600;"> //display Operator content here</span><br />
<span style="color:#cc0000;"> }</span><br />
<span style="color:#cc0000;">?&gt; </span></p>
<p>Now finally for logout make a page named logout.php and put a link to it on any page. When some one will click that link he will go to logout page and will automatically logout and will be redirected to another page. Content of &#8220;logout.php&#8221; will be as follows.</p>
<p><span style="color:#cc0000;">&lt;?php</span><br />
<span style="color:#cc0000;"> session_start();</span><br />
<span style="color:#cc0000;"> $_SESSION = array();</span><br />
<span style="color:#cc0000;"> session_destroy();</span><br />
<span style="color:#006600;">//    Redirect to following page after logout</span><br />
<span style="color:#cc0000;"> header(&#8220;Location: index.html&#8221;);<span style="color:#006600;">//Change the page according to requirement</span></span><br />
<span style="color:#cc0000;">?&gt; </span></p>
<p>Now go and implement this all stuff and enjoy new knowledge of being login and logout. So start playing with it.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alimul.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alimul.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alimul.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alimul.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alimul.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alimul.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alimul.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alimul.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alimul.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alimul.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alimul.wordpress.com&blog=1090347&post=48&subd=alimul&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://alimul.wordpress.com/2008/10/20/regular-login-script-using-session/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e1a241ebcf34f408a6710858e520f15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alimul</media:title>
		</media:content>
	</item>
	</channel>
</rss>