This code assumes you've Posted data from a form on the last page and the user hit 'log in'
<?php
session_start();
$username = $_POST[username];
$password = $_POST[password];
$db_name_x = "database";
$table_name_x = "table";
$connection_x = @mysql_connect("localhost","root", "password") or die(mysql_error());
$db_x = @mysql_select_db($db_name_x,$connection_x) or die(mysql_error());
$sql_x ="SELECT * FROM $table_name_x ORDER BY username";
$result_x = @mysql_query($sql_x,$connection_x) or die(mysql_error());
while ($row_x = mysql_fetch_array($result_x)) {
$Username = $row_x['username'];
$Password = $row_x['password'];
$name = $row_x['name'];
$admin = $row_x['admin'];
if ($Username == $username) {
$PASSWORD = md5($password);
if ($PASSWORD == $Password) {
$_SESSION[logged] = "yes";
$_SESSION[name] = $name;
$_SESSION[username] = $username;
$_SESSION[admin] = $admin;
}
}
}
?>
When you stored the usernames and passwords, just store the password as md5($password); this hashes the password so people can't see what they are if they hack your sql.
name and admin are optional... you just have a header piece of php that displays alternate pages for admin, member, and non-logged people. And you can use their name on the page somewhere.... you could store any data you want into the database....
the _X's are because this is from an include I use in lots of pages, and I added the _X so if for some reason I used the non-_Xed version of the same SQL loader, it wouldn't overwrite the varaibles... I'm too lazy to remove them... it doesn't matter.
on the password check IF statement you could echo error msgs if the password isn't correct but the username was... or not.. I normally don't, cause that would alert someone that the username they used was right and the password wasn't... I typically go by name, so the username and password are both hidden, so it adds that much more security.
Also... you can SELECT the username rather than everything, and not run through it all... you would want to do that for lots of users (wont matter under 100 or so) but I didn't trust it when I wrote this...
regardless.. this works.