: || - HT-FEARS - || :
Welcome to HT-FEARS..
Please Log in or Register to have a fully Access in our Site..
Read the Rules and Regulation to Avoid Banned..
Thank You!!
: || - HT-FEARS - || :
Welcome to HT-FEARS..
Please Log in or Register to have a fully Access in our Site..
Read the Rules and Regulation to Avoid Banned..
Thank You!!
: || - HT-FEARS - || :
Would you like to react to this message? Create an account in a few clicks or log in to continue.



 
SearchPortalHomeLatest imagesRegisterLog in

Share  | 
 

 How to Create Login Page in PHP and MySQL with Session?

View previous topic View next topic Go down 
Author Message
Admin
Administrator
Administrator
Admin

Posts : 338
Join date : 2009-09-30
Age : 23
Location : Philippines

How to Create Login Page in PHP and MySQL with Session? Vide
http://www.ht-fears.co.cc

PostSubject: How to Create Login Page in PHP and MySQL with Session?   How to Create Login Page in PHP and MySQL with Session? Icon_minitimeThu Oct 01, 2009 9:00 pm

==========================
Due to a lot of problem reported by friends about these script writing, so here I put the complete scripts. You can grab it here: http://rapidshare.com/files/195043222/login-frozenade.co.nr.zip
Thank you for concern. Smile
==========================

The production of login page using PHP and MySQL is actually very simple.

I assume that you use local PHP connection and your MySQL database configuration use ‘localhost’ as hostname and ‘root’ as user with blank password.

First, let’s create a database:
create database dblogin;

Then use our database we’ve just created with command:
use dblogin;

Now, let’s create a table:
create table tbuser
(username varchar(50),
password varchar(255))

After that, fill the table with username ‘admin’ and password ‘adminpass’.
insert into tbuser values ('admin',md5('adminpass'));

Now, let’s prepare the login page.
<html>
<head>
<title>login page</title>
</head>
<body>
<form method="POST" action="loginproc.php">
<p>username: <input type="text" name="username" size="20"></p>
<p>password: <input type="text" name="password" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>

Save above document with name ‘loginpage.php’.

Now, prepare a file and give it name ‘loginproc.php’ to check the validity of username and password.
<?
session_start();

$server = "localhost";
$username = "root";
$password = "";
$db_name = "dblogin";

$db = mysql_connect($server,$username,$password) or DIE("Connection to database failed, perhaps the service is down !!");
mysql_select_db($db_name) or DIE("Database name not available !!");

$login = mysql_query("select * from tbuser where (username = '" . $_POST['username'] . "') and (password = '" . md5($_POST['password']) . "')",$db);
$rowcount = mysql_num_rows($login);
if ($rowcount == 1) {
$_SESSION['username'] = $_POST['username'];
header("Location: securedpage.php");
}
else
{
header("Location: loginpage.php");
}
?>

As you see above, there is ’session_start();’.
This function is used to initializes a data session. It will creates a new session or continues previous session from data session change by GET, POST or cookie.
See the detail information about session here: http://id.php.net/function.session-start

If username and password are correct, then we’ll be directed to ’securedpage.php’.
This is a page that we want to show if login is successful. This page cannot be accessed if there is not found the correct data session when login is successfully passed. This page also contains ‘logout’ menu, so we can destroy our login data session then return to login page.

This is content of ’securedpage.php’:
<?
session_start();
if (!isset($_SESSION['username'])) {
header("Location: loginpage.php");
}
?>
<html>
<head>
<title>secured page</title>
</head>
<body>
<p>secured page with session <? echo $_SESSION['username']; ?></p>
<p><a href="logoutpage.php">logout</a></p>
</body>
</html>

Script for ‘logout.php’:
<? session_start();
unset($_SESSION['username']);
session_destroy();
header("Location: loginpage.php");
?>

Store your files you have created in one location. e.g: ‘logintest’.
So, the login page could be http://localhost/logintest/loginpage.php

So easy, isn’t it? Wink

==========================
Due to a lot of problem reported by friends about these script writing, so here I put the complete scripts. You can grab it here: http://rapidshare.com/files/195043222/login-frozenade.co.nr.zip
Thank you for concern. Smile
==========================
Back to top Go down  
 

How to Create Login Page in PHP and MySQL with Session?

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum: You cannot reply to topics in this forum
: || - HT-FEARS - || :  :: ::| Graphics and Web development |:: :: »¦« PHP »¦« -
Free forum | ©phpBB | Free forum support | Report an abuse | Forumotion.com