![]() |
|
[Mediocrity] My six Steps To A Login Page - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: PHP (https://sinister.ly/Forum-PHP) +--- Thread: [Mediocrity] My six Steps To A Login Page (/Thread-Mediocrity-My-six-Steps-To-A-Login-Page) |
[Mediocrity] My six Steps To A Login Page - Mediocrity_mybb_import6293 - 03-30-2012 The production of login page using PHP and MySQL is actually very simple. I assume that you use local web server connection (Apache and PHP) and your MySQL database configuration use ‘localhost’ as hostname and ‘root’ as username You will need at host and domain to do this as you are using a Msql Db Free Host www.host1free.com www.x10hosting.com www.freewebs.com paid hosts www.techhosts.co.uk www.hostgator.com okay so lets get started i notice that these type of scritps are become ever so more valuable as people of the computing society want loging pages for there websites and projects i noticed that this site dosnt have a sufficient package for you guys too look at and study so i just thought about creatign a VERY simple and easy few codes to follow Six Very Easy Steps Step One Let’s create a database with PHPMyAdmin. Open your favorite browser, then type ‘http://localhost/phpmyadmin’ at your browser address bar. Create database ‘phpmysimplelogin’. Click ‘Create’. create a table, name it ‘user’ with ’2′ (two) number of fields. Click ‘Go’. First field, name it ‘username’, type ‘varchar’, lenght/values ’25′. Second field, name it ‘password’, type ‘varchar’, lenght/values ’255′. Click ‘Save’. After that, we will fill the table. Click ‘SQL’ menu, then type this query on textbox: INSERT INTO user (username, password) VALUES (‘admin’, md5(‘admin’)) Click ‘Go’. Quote:It means, you fill ‘username’ field with string ‘admin’ and ‘username’ field with an encryption string of ‘admin’. Okay, now let’s prepare the web pages. Run your favorite PHP code editor, e.g: PHP Expert Editor, RapidPHP, etc; or just Microsoft Notepad or Notepad++. Second Step Save document below with name ‘dbconnect.php’. PHP Code: <?php
$hostname = 'localhost'; // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet.
$dbname = 'phpmysimplelogin'; // Your database name.
$username = 'root'; // Your database username.
$password = ''; // Your database password. If your database has no password, leave it empty.
// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
?>Step Three Next step, save document below and name it as ‘index.php’: PHP Code: <?php
// Inialize session
session_start();
// Check, if user is already login, then jump to secured page
if (isset($_SESSION['username'])) {
header('Location: securedpage.php');
}
?>
<html>
<head>
<title>My Login Script Provided By Mediocrity At www.Hackcommunity.com</title>
</head>
<body>
<h3>User Login</h3>
<table border="0">
<form method="POST" action="loginproc.php">
<tr><td>Username</td><td>:</td><td><input type="text" name="username" size="20"></td></tr>
<tr><td>Password</td><td>:</td><td><input type="password" name="password" size="20"></td></tr>
<tr><td> </td><td> </td><td><input type="submit" value="Login"></td></tr>
</form>
</table>
</body>
</html>
Quote:As you see, there is ‘session_start();’. Step Four Now, prepare a file and give it name ‘loginproc.php’ to check the validity of username and password. PHP Code: <?php
// Inialize session
session_start();
// Include database connection settings
include('dbconnect.php');
// Retrieve username and password from database according to user's input
$login = mysql_query("SELECT * FROM user WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')");
// Check username and password match
if (mysql_num_rows($login) == 1) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
// Jump to secured page
header('Location: securedpage.php');
}
else {
// Jump to login page
header('Location: index.php');
}
?>Quote:If username and password are correct, then we’ll be directed to ‘securedpage.php’. Step Five This is content of ‘securedpage.php’: So Creat The document ‘securedpage.php’ PHP Code: <?php
// Inialize session
session_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
header('Location: index.php');
}
?>
<html>
<head>
<title>SMembers Area</title>
</head>
<body>
<p>This is secured page with session: <b> Welcome <?php echo $_SESSION['username']; ?></b>
<br>You can put your restricted information here.<br> please Visit <a href='www.hackcommunity.com'> HC</a></p>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
Step Six This is the ‘logout.php’: page which give your members the oppourtunity to log out PHP Code: <?php
// Inialize session
session_start();
// Delete certain session
unset($_SESSION['username']);
// Delete all session variables
// session_destroy();
// Jump to login page
header('Location: index.php');
?>Thanks Guys Hope You like this tutorial . I hope it comes in useful Dont hesitate to leave any feed back or problems you have .? i would be more than happy to help people get this code up and running if your having trouble. RE: [Mediocrity] My six Steps To A Login Page - The Alchemist - 04-26-2012 The first link is not working. This one : http://localhost/phpmyadmin How to proceed? RE: [Mediocrity] My six Steps To A Login Page - Mediocrity_mybb_import6293 - 04-27-2012 Do You have your Website host. ? eg . www.Yourdomain.com:2082 or www.Yourdomain.com/Cpannel Go into that and then you should see the option of phpmyadmin if you have any trouble just keep asking i will always reply |