| Business Forum | Buy, Sell & Trade | Search Engine Forum | Domains & Webhosting |
| |||||||
| REMOVE the ads below ! |
![]() |
| | LinkBack | Thread Tools |
| ||
| PHP : Simple Login/Logout System with Sessions -
11-21-2007
This tutorial will help you create a really basic user login system. Here is what we are going to do in English: The user can login with an email & password. If the email and password match then create a session and allow the user to access the private members page. If an unauthorized user tries to access the members page without logging in, they will be forced to return to the login page. So lets start off by creating a new database to store emails and password. PHP Code: PHP Code: Now create a file called include.php. This file will store all the code we want place at the top of every page. PHP Code: One thing to note: If you get an error saying that the headers were already sent, your start_session() probably wasn’t at the very top of the page. The start_session() has to be at the very top of the page. Now create a login.php with this code. PHP Code: Lets break this code down so it a little easier to understand. In English, here is what the code means: Include the include.php file. Set the $error variable to a blank value. If the form was submitted, put the values into the variables, $form, $email, $password so we can use them later. If the form is submitted.... And the email and password fields are filled out and they aren't blank... Check to see if there are any rows in the database that have a matching email and password. If there are, that means the user entered a matching email and password. Set the session. Change the page to members.php. And stop running the php code. If the user didn't enter a matching email and password, throw an error. Now create the members.php. This page will only be available to people who have signed in on the prior page. Here is the code that goes in that page. PHP Code: This block of code is actually pretty simple. Just like on all the other pages, we include the include.php file at the top. The include.php file sets the session up and connects to the MySQL database. Then, if the appropriate session is not set (the user didn’t login), we move the user to the login page and exit the code. If the users has logged in, and their session is appropriately set, they will see the rest of the page. This pages format can be copied to create additional hidden pages. Now to the final step. Create logout.php and enter this code: PHP Code: This page doesn’t have any HTML in it. The first line starts the session, and then if the user is logged in, the session will be destroyed and the user will be redirected to the login page. I hope you enjoyed the tutorial. Please leave me feedback through the comment system below so I can improve my tutorials in the future. Source : Webmasters Arena: PHP : Simple Login/Logout System with Sessions | ||
|
| |||
| Re: PHP : Simple Login/Logout System with Sessions -
11-22-2007
Note : Edit your post and Added the PHP bbcode for some nice formatting display. ![]() Btw, thank you for posting the tutorial. This tutorial along with the one posted here ( http://www.geekpoint.net/tips-tutori...-with-php.html ) should be really helpful for users just starting to learn PHP with MySQL. Thanx, Shadab. | |||
|
![]() |
| Thread Tools | |