| Sunday, September 05th, 2010 | Welcome Guest! - Register |
|
Sign In
Forget your password? Donations to date: $62.50 Support This Site Today! Latest Forum Posts |
Sale is OverThe sale in the download's section is now over.Posted by ~Ntsmarkv on Monday, August 31st, 2010 10:03 PM MST
PHP - Login/Logout Script
When building a dynamic website, one of the best and most common thing that a webmaster does is store the information of users in a database. But how does the website know who is who? That is where the login page comes in. Here is how a login script works: 1. The user enters their username and password and submits the form 2. The server compares the information that the user entered against the database. 3. If the info matches a row in the database, the row will be returned. If it doesn't match, the number of rows is 0 and nothing is returned. 4. Assuming that the info matched what was in the database, the website sets a cookie. A cookie is a file that stores the user data for the amount of time that the website specifies (the webmaster sets the parameters for the time). 5. While the cookie is set, the information about the user can be retrieved from the cookie. This article will show you how to match a user's info against the info in the MySql database. I will give you instructions that you can try as you read this article. To start out, you first need a table in the database. I will call the table "users" - if you already have a users table in your database, you may call it something else - but keep in mind I will reference the users table only. Here is the sql code to generate the users table. -------------------------------- CREATE TABLE `users` ( `user_id` int(10) NOT NULL auto_increment, `username` varchar(30) NOT NULL default '', `password` varchar(32) NOT NULL default '', `email` varchar(50) NOT NULL default '', PRIMARY KEY (`user_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ; INSERT INTO `users` VALUES (1, "john", "johnspassword", "john@void.null"); INSERT INTO `users` VALUES (2, "bob", "bobspassword", "bob@void.null"); INSERT INTO `users` VALUES (3, "suzy", "suzyspassword", "suzy@void.null"); INSERT INTO `users` VALUES (4, "cheryl", "cherylspassword", "cheryl@void.null"); INSERT INTO `users` VALUES (5, "monkey", "monkeyspassword", "monkey@void.null"); -------------------------------- Next you will create your connect.inc page. For info on how to do this, click here. Now that you have created your users table and are connected to it, we will write some code to execute a query to match a user in it. Here is our script that will do the query: -------------------------------- <?php -------------------------------- Now that we know how to compare info with the database using a static name such as bob, we can do the same with variables... like this (I also modified the query structure a bit - read the comments to understand what I did): -------------------------------- <?php -------------------------------- I'll bet that now you are wondering how this works with a form. Now I will just dive in and show you (Keep in mind that anything that is not in the <?php tags ?> but is in between the <!-- tags --> is a comment in HTML): Page: login.php -------------------------------- <html> <head> <title>Login Script</title> </head> <body> Please Sign In<br /> <!-- For the form action, I use <?php echo $_SERVER['PHP_SELF']; ?>. This isa variable for the current page name. For example, if you look at the url for this page, it is pages.php. pages.php is equal to <?php echo $_SERVER['PHP_SELF']; ?>.If you want to, you can just make the action like this: action="login.php" but I don't reccommend it --> <?php // include the MySql connection script so we can check the database?><!-- open the form --> <form action=" <?php echo $_SERVER['PHP_SELF']; ?> " method="POST"><!-- Show the input text box for the username --> Username: <input type="text" name="username"><br /> <!-- Show the input text box for the password --> Password: <input type="password" name="password"><br /> <!-- Show the button to submit the form --> <input type="submit" name="submit"> <!-- Close the form --> </form> <?php} </body> </html> -------------------------------- If you want to log the user out, just reverse the cookie process. Instead of a time()+, use a time()- like this: logout.php (create a link to this page from an already existing page): -------------------------------- <?php -------------------------------- This concludes this tutorial.
This page has been accessed 2,778 times |
Articles Site Stats
Since July 23, 2005:
1,075,800 hits 783,208 unique visitors Today's Info: 223 hits 74 unique visitors More Traffic Stats Who's Online: Active Users: 0 Guests: 75 KASL Members: Currently 2,539 Newest member: Gilues Forum Stats: There are 9 categories There are 16 topics There are 67 posts Page Load Time Page loaded in 0.032 secs. |