Friday, October 29, 2010

Week12 – PHP and MYSQL LOGIN part

I finished administrator login function on my web system.

I think the following code might be help to who also has login function on the website.
Step1:
create table on your database.
create table tab_admin(
id int(10) auto_increment primary key,
loginame varchar(10) not null,
password varchar(12) not null
)
insert into tab_admin values (' ','adminname','adminpassword');

Step2:
create form on the web page
Step3:
create new .php page

?php
$user="******";
$password="**********";
$database="******";
$link = mysql_connect("rerun.it.uts.edu.au", $user, $password) or die("Unable to connect to database");
$db = mysql_select_db($database, $link) or die(mysql_error());$host="localhost"; // Host name

$tbl_name="tab_admin"; // Table name

$loginame=$_POST['loginame'];
$password=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$loginame = stripslashes($loginame);
$password = stripslashes($password);
$loginame = mysql_real_escape_string($loginame);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM $tbl_name WHERE loginame='$loginame' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("loginame");
session_register("password");
header("location:login_success.php");

}
else {
echo 'Wrong Username or Password';
}
?>



Step4:
create login_successful.php
session_start();
session_destroy();
?>

DONE~~~~Finish the login function

Reference: http://php.about.com/od/finishedphp1/ss/php_login_code.htm

No comments:

Post a Comment