Search
Left Quote    I have not failed. I've just found 10,000 ways that won't work.
- Thomas Alva Edison    
Right Quote
 
[login] | [Register]
 

Complete Members System

by: bs0d
Page: 6 of 9
(View All)

CREATING A LOGIN SCRIPT: CODE

If you have followed the tutorial so far, the members table has been made in the database, and a registration script has been coded where members may signup and the values they enter on the page will be inserted into the members database.

This login script will allow them to enter their username and password. Our script will detect that there is an existing username matching what they entered, and check the password (md5()) aginst the password on record for that username. If login is successful, a cookie will be applied (if desired), and redirect them to the main page. If an error is encountered, prompt user appropriately.

Lets go:



Ok, the first thing you see is ob_start(); If you're unfamiliar with ob_start, see php.net/ob_start. It has to do with output before headers and such so this way its buffered instead. Could boil down to the way the error messages are setup. If it has to do with empty or fields or not enough characters in a field, javascript is a nice client side solution. But, this is just an example and we're just presenting the basic idea, remember.

Next, you will see that just like the registration script, we needed to require the database connect file beause we will be communicating with the database.

The next bit of code checks if the user is logged in. Notice the session variables.These are variables that we set when a login is successful, along witha few others you will soon see. So we are checking the values of these variables to see if the user is already logged in. If so, there is no reason for them to be able to visit the login page. So, instead we will just redirect them to the main page.

Lets carry on...




The code above goes directly under the last block of code we discussed. We are opening the if condition that checks if the submit button was pressed on the form. The end } for this block will be followed by the else condition to show the form (submit button not pressed). So everything in between will analyze what we need when the button is pressed.

So first thing is first, if the username or password field is blank, prompt an error. The next part checks if the visitor selected to "remember me" or not. If so, apply cookie. This is achieved with the setcokie() function, which uses this format:

bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )

Taken from php.net...



1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

330,939 Views
41 Total Comments
4.8 Rating of 5 (5 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Variable Scope



"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank