Escolar Documentos
Profissional Documentos
Cultura Documentos
Part 1: Introduction
Sometimes its important to restrict access to certain parts of a website, and the best way to regulate this access is through a user login system. This article is going to cover the creation of a very simple login system that will help get you started. This system is by no means 100% secure, so we do not recommend using it in a production environment, however we will examine one of the ways a login can be created and well also look at some of the pitfalls of such a system. Before we begin, lets establish the pseudo code of our script and walk through the general process of a login. First, and most importantly, we need a user account. This will include the users login identification number, a username, password, and any other credentials we may want such as access levels or email addresses. To keep this simple, well stick to user id, username, and password. When a user enters his or her login credentials on your site, we will have to verify the credentials and then establish some means to keep track of a users login status. Remember, whenever someone visits a webpage, that page is always loaded as if its the first time someone has been on the site. We need a way to tell the site who the user is so that the site can make adjustments for the logged in account. There are essentially three different parts to this login system. The first part is the PHP code were going to write that will regulate the user access. Well also need a database to store both the user account information as well as temporary session information that remembers what accounts are logged in. The latter can be setup several ways, including the use of PHP sessions, however for this example were going to store the sessions in a database. The third thing well need is a way for the users computer to identify itself. For this were going to store a cookie on the users computer that will be sent to our website every time the user accesses a new page. Lets begin creating the code. If you want to test this code on a working site, we suggest you create a separate user.php file (or equivalent) that will store all of the user functions. In this example well simply reference all of the different parts necessary. To view the final working code as it should appear on a site, go to http://blog.readysetconnect.com/sample.php.txt.
www.akshaybaweja.co.cc
www.baweja.co.cc
www.akshaybaweja.co.cc
www.baweja.co.cc
www.akshaybaweja.co.cc
www.baweja.co.cc
www.akshaybaweja.co.cc
www.baweja.co.cc
www.akshaybaweja.co.cc
www.baweja.co.cc
www.akshaybaweja.co.cc
www.baweja.co.cc
www.akshaybaweja.co.cc
www.baweja.co.cc
<?php if($userid > 0) { echo Welcome to our site, user #$userid (<a href=?logout>Click here to logout</a>); } else { if($login_status != && $login_status == 0) { echo Invalid username/password combo.<br>; } ?> <form action=sample.php method=POST> <input type=text name=username> <input type=password name=password> <input type=submit value=Log In> </form> <?php } ?>
We first to check to see if our status function call returned a user id. If it did we can print out a welcome to the user, giving them his or her user id. We also provide a link for logging out; simply call the same page with logout specified as a URL GET variable to log out. If we dont have a user id, well print out the login form. We also check the login_status variable to see if there was an error. If there was well print this out to the user. This tutorial covers the basic concepts of a user login system, however it certainly doesnt cover everything. There are many websites on the internet that can assist you with the specifics of setting up a database table, basic programming concepts, and anything in between. The full sample program can be found here. Note: In our example we assume you already have a connection to the database established, however sample.php includes the basic MySQL database connection functions.
www.akshaybaweja.co.cc
www.baweja.co.cc