A Simple Login Code using PHP+MySQL
First of all you must create a table. For a example we name this table as member.
| id | username | password |
| 1 | karlo | 123 |
| 2 | toni | 143 |
So first you must create your own form. That looks like any ordinary login box with a username, password and a login button.
<form method=”POST” action=”">
<p>
<label>Username: </label>
<input type=”text” name=”username” />
</p>
<p>
<label>Password: </label>
<input type=”password” name=”password />
</p>
<p>
<input type=”submit” value=”Login” />
</p>
</form>
As you can see i have set the action=”" to empty so you have to change it to something like process_login.php or any .php page that would verify if the user input is valid or not.
So on your process_login.php you this simple code under a php tag.
$username = $_POST['username'];
$password = $_POST['password'];
Then you connect to your databse. Please use your own mysql username & password if you are using one. I would be using the default username & password for WAMP.
$db = mysql_connect(’localhost’,'root’,”);
Then you choose your database to be use. For example we would be using the “tequida” database name. So to connect to that database we do this.
mysql_select_db(’tequida’);
OK we are now connected to mysql and using the tequida database! Now to verify if the username & password is valid analyze the code below.
$query=”SELECT * FROM member WHERE username LIKE ‘$username’ AND password LIKE ‘$password’”;
$result=mysql_query($query);
$nr=mysql_num_rows($result);
Take note do not copy the code please do analyze it. The first line is the query. We have requested for a username and password under the member table. This table is the same table given above. The second line is where we process the query. And lastly the third line is where we see how many rows matched to your query. So to verify if the user is valid you simply see if the $nr is zero.
if ( $nr == 0 )
{
echo ‘Invalid Username/Password!’;
}
if ( $nr > 0 )
{
echo ‘Correct!’;
}
Hope this helps all of you! ^_^








03/19/2008
salamat kaau dre q na bara :p
03/19/2008
ahahah sure master! no probz!
03/19/2008
Lolz, ahaha!! Master! session na pud atong problema. kapoi kaw na bahala ato. . unta magpost si karlo usab sessioning ug cookie!!. .
03/19/2008
la ko kblo nsaon ng cookie. naga session lang ko. cge post lang ko unya kng dle ko kpoyon. lol
03/21/2008
ok ok!!
04/15/2008
Tip: Refrain from using the keyword like when comparing values from forms to the values from the SQL Tables especially for authentication. The LIKE keyword would return values those who matched with its pattern. If possible, use the equal sign when comparing.P.S.: The keyword LIKE would be best for searching in your application.Have nice programming trip…