Coding time
In this week we will look at the process of creating a simple PHP login system. Please remember that this is a simple example and if used outside of a classroom setting (live) you may need to make changes to ensure that the database connections are safe and that your coding is rigorous to reduce hacking and unwanted access to your data.
Login.php
This part of our login page enables the page to check if an active cookie is available, this would then indicate that the user has already logged in to the website area, if a cookie is not present, or the cookie has timed out (a time to live is set in the cookie, so that after a period of time the cookie will expire and require the user to re-login) then the user will be directed to the login.php page.
The following section of code reviews the information that has been sent by the user once they have clicked on the submit button with the values of their username and password. The first if statements perform a check to see if any values have been entered into the form (this could be done with javascript on the client side to save processing)
The script will then connect to the database and run the query using the values in username and password against the selected database table.
The section of code above is the creation of the form using HTML. The form action is telling the page that when the user clicks submit to send the information to itself, or, to the top part of the page where the PHP is looking for the information
Logout.php
The logout script will look for the created cookie that will have been made when the user successfully logs in, and change its values so that it is destroyed/no longer valid. Once this has happened the user is then redirected to the login page.
members.php
Mysql (Database creation sql script)
CREATE TABLE users (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(60), password VARCHAR(60))
The above tells the mysql database to create a table called "users" within the database, the fields created are a primary key of ID, and username field that is limited to 60 characters and a password field that is limited to 60 characters.
Links to Learning Outcomes |
Links to Assessment criteria |
|
---|---|---|
Anonymous Assessment - Learners assess an anonymous piece of work containing deliberate mistakes against given success criteria.