CREATING A LOGIN SCRIPT: CODEIf 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.
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...
No Comments for this page.