<?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>Webdevelopment at huuah.com &#187; script</title>
	<atom:link href="http://huuah.com/tag/script/feed/" rel="self" type="application/rss+xml" />
	<link>http://huuah.com</link>
	<description>webdevelopment, cms, php, javascript etc</description>
	<lastBuildDate>Sat, 04 Sep 2010 22:55:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A simple login script with PHP and MySQL</title>
		<link>http://huuah.com/a-simple-login-script-with-php-and-mysql/</link>
		<comments>http://huuah.com/a-simple-login-script-with-php-and-mysql/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 17:45:29 +0000</pubDate>
		<dc:creator>js - huuah</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://huuah.com/?p=417</guid>
		<description><![CDATA[<p>With this guide I will try to go through the very basic techniques of making a simple php script that can handle login authorization of website users.</p>
<p>The guide will be based on common Linux installations of both MySQL and PHP.</p>
<a name="Step+1+%26%238211%3B+the+database"></a>Step 1 &#8211; the database
<p>Create a table&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>With this guide I will try to go through the very basic techniques of making a simple php script that can handle login authorization of website users.</p>
<p>The guide will be based on common Linux installations of both MySQL and PHP.</p>
<a name="Step+1+%26%238211%3B+the+database"></a><h1>Step 1 &#8211; the database</h1>
<p>Create a table in your MySQL database with 2 fields, <code>username</code> and <code>password</code>. This could be done by running the following code in your MySQL shell or from phpMyAdmin or another interface:</p>
<pre class="brush: plain;">
CREATE TABLE `login` (
`username` TEXT NOT NULL ,
`password` TEXT NOT NULL
)
</pre>
<p>The database table called <code>login</code> is now created with 2 field named <code>username</code> and <code>password</code>.</p>
<a name="Step+2+%26%238211%3B+The+HTML+form"></a><h1>Step 2 &#8211; The HTML form</h1>
<p>First we have to create the web formula to show the login function:</p>
<pre class="brush: xml;">
&lt;form action=&quot;login.php&quot; method=&quot;post&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;username&quot;&gt;
  &lt;input type=&quot;password&quot; name=&quot;password&quot;&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Login&quot;&gt;
&lt;/form&gt;
</pre>
<p>This will output 3 field like this:</p>
<form action="#" method="post" onsubmit="return false;">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
<p>When submitting the form, either by hitting enter or by clicking the Login button, the formula will send the username and password to the login.php file.</p>
<a name="Step+3+%26%238211%3B+Getting+the+information+from+the+website."></a><h1>Step 3 &#8211; Getting the information from the website.</h1>
<p>The login information has now been sent to the login.php and we have to receive these value before checking them against the MySQL database.</p>
<p>Login.php:</p>
<pre class="brush: php;">
  &lt;?php
  ...
  // receiveing the login information
  $username = $_GET[&quot;username&quot;];
  $password = $_GET[&quot;password&quot;];
  ?&gt;
</pre>
<a name="Step+4+%26%238211%3B+Connecting+to+the+database"></a><h1>Step 4 &#8211; Connecting to the database</h1>
<p>Before checking the login data against the database, we have to open a new connection.</p>
<pre class="brush: php;">
&lt;?php
  ...
  //login information to the MySQL database
  $dbuser = &quot;myUsername&quot;; // change myUsername to your own username
  $dbpass = &quot;myPassword&quot;; //change myPassword to your own password
  $dbhost = &quot;localhost&quot;; // this should work in most cases. Otherwise contact your webhost
  $db = &quot;myDatabase&quot;; // change myDatabase to your the database assigned to your login

  // perform user authentication
  mysql_connect($server, $dbuser, $dbpass);
  // selecting database
  mysql_select_db($db;

  ...
?&gt;
</pre>
<a name="Step+5+%26%238211%3B+Comparing+the+username+and+password+against+the+database"></a><h1>Step 5 &#8211; Comparing the username and password against the database</h1>
<p>We have the login information from the formula and we have opened a connection to the database. Great. All we have to do, is the actually user validation.</p>
<pre class="brush: php;">
&lt;?php
  ..
  // check the content of our $username and $password variable against the login table
  $sql = &quot;SELECT username FROM login WHERE username='$username' AND password='$password'&quot;;
  // perform the query
  $query = mysql_query($sql);
  // check if the request returned 1 or 0 rows from the database
  if (mysql_num_rows($query)) {
    // 1 or more rows matched our request, which means the username and password combo are valid
  } else {
    // 0 rows matched our request. Login is not valid then.
  }
  ..
?&gt;
</pre>
<a name="Step+6+%26%238211%3B+All+done%2C+final+pointer"></a><h1>Step 6 &#8211; All done, final pointer</h1>
<p>This is a very basic type of php login script. There are a lot of other things to have in mind when making database queries, that I have not convert in the above examples:</p>
<ul>
<li>Perhaps the login table should hold more information, like a user id for easier validation later on the the script.</li>
<li>Double checking and validating user input to prevent SQL Injection. This could be done be PHP Magic Quotes or by manually applying addslashes() and stripslashs().</li>
<li>..</li>
</ul>
<p>But then again. Very simple demonstration.</p>
]]></content:encoded>
			<wfw:commentRss>http://huuah.com/a-simple-login-script-with-php-and-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
