Create a Basic Login Script in PHP
Interested in creating your own forum page, blog, or even a shopping cart? A login area is the first step. Read below the jump to learn how to create a script for it in PHP.
Steps
- Create the login database and table. For the purposes of the code examples below, we will assume the database name to be "test" and the table name to be "members". The database and table can be created through the control panel of your webhost. The "members" table will store the username and the passwords of all the people would be allowed access via this login script. The table should have a unique id field as the primary key, a username field, and a password field.
- Create the login interface. This is an HTML page containing a form, two text boxes for the user name and password, and a submit button. As long as you have these elements, you may design the layout however you wish.
- Create the php script that will check the login input against the database. This script will connect to the database, send a query and retrieve the result, check to see if the username and password is correct and send you to the next page depending on the result of the comparison.
- Create the page that will display after login success. This script will start your session and display an html message of your choosing.
- Create log-out script. This step is optional as sessions can time out. If you wish to create one, you will need a button or link to initiate the script and a message to confirm the success of logging out.
Template:Video:Create a Basic Login Script in PHP
Tips
- Never use JavaScript or any other client-side scripting language for authorization.
- Try using CSS and various HTML elements to create an interesting look for your login and message pages.
- Depending on your requirements, add security checks to your script.
- Replace the database functions in your script with the appropriate functions for the database you are using (e.g. SQLITE, Microsoft SQL, etc.)
Warnings
- This script stores passwords in clear text which is a security risk. A live production database would at the minimum store the passwords in encrypted form using hash algorithm such as SHA-1 or SHA-256 and use salting as well.
- The examples have been stripped to the bare requirements for ease of understanding. It is recommended to add security checks and error checking.
Related Articles
- Write PHP Scripts
- Create a Chat Room for Your Website Using a Server Side Language
- Write an HTML Page
- Create a Secure Login Script in PHP and MySQL
- Create a Secure Session Managment System in PHP and MySQL