portfolio - PHP
Here are my school (British Columbia Institute of Technology) Labs/Assignments I have worked on.
| Title |
Lab 2 |
[Code Files]
lab2.txt
|
| Client |
BCIT COMP3015 |
| Due Date |
May 5, 2018 |
| Objective |
The purpose of this lab is to work with functions and time stamps. |
| Specifications |
- trim(), date().
- Define your own function at the top of file called moments that takes in one parameter
|
| Skills |
PHP |
| Notes |
- A starter_kit.zip was provided by my instructor.
- HTML, CSS, bootstrap, JavaScript were set up by my instructor.
|
| Link |
Lab 1 |
| Title |
Lab 3 |
[Code Files]
lab3.txt
functions.txt
|
| Client |
BCIT COMP3015 |
| Due Date |
May 13, 2018 |
| Objective |
The purpose of this lab is to work with forms and arrays.
|
| Specifications |
- Refactor the code so that the moments() function is in a separate file named functions.php
- require() this file in lab3.php
- Modfiy the form so that it POSTs to itself – lab3.php
- be sure to also add in the name attributes for the input elements
- All input fields are required. If an input field is missing, display an error message
- If all input fields are provided, display a thank you message with the authors full name and current time.
- ie. this will do fine
Thank you Gary Tong for posting! September 25th, 2017.
|
| Skills |
PHP |
| Notes |
- A starter_kit.zip was provided by my instructor.
- HTML, CSS, bootstrap, JavaScript were set up by my instructor.
|
| Link |
Lab 3 |
| Title |
Lab 4 |
[Code Files]
lab4.txt
includes/functions.txt
posts.txt
|
| Client |
BCIT COMP3015 |
| Due Date |
May 20, 2018 |
| Objective |
The purpose of this lab is to work with files and file upload.
|
| Specifications |
- Move the uploaded file to the uploads/ directory
- After a successful POST, append the POST’d data to text file called posts.txt
- separate each value by a ,
- include the value of time() in your data
- include the uploaded file name in your data
- each POST will be on it’s own line
- you may assume the user will never enter a , character
- an example line may look like
Gary,Tong,A Title,A comment,1,image.jpg,1481808630
- On each page load, open the text file called posts.txt
- loop through the lines in posts.txt, each line will represent a post
- display each post in a panel, similar to the First Post! one that was done for you
- [Challenge] display each post in a panel, similar to the First Post! Sort the display boxes by priority
- [Challenge] Deal with users having , in their input
|
| Skills |
PHP |
| Notes |
- A starter_kit.zip was provided by my instructor.
- HTML, CSS, bootstrap, JavaScript were set up by my instructor.
|
| Link |
Lab 4 |
| Title |
Lab 5 |
SIGNUP |
LOGIN
|
[Code Files]
lab5.txt
signup.txt
loginmain.txt
includes/functions.txt
posts.txt
signup.txt
login.txt
|
| Client |
BCIT COMP3015 |
| Due Date |
May 27, 2018 |
| Objective |
The purpose of this lab is to work with input validation and regular expressions.
|
| Specifications |
- All fields are required. If any one is missing, provide an error message
- You must use regular expressions to validate the following fields
- First Name and Last Name – alphabetical only
- Password – check for at least a mix of digits and alphabets
- Phone Number – 7 or 10 digits only, with or without brackets for the area code, with or without dashes/spaces separating each grouping. ie.
- (604)1231234 is valid
- 604 123 1234
- 604-123-1234
- (604) 123 1234 is valid
- 6041231234 is valid
- 1231234 and 1231231234 is valid
- (6041231234 and 604)1231234 is not valid
- Date of Birth – in the format of MMM-DD-YYYY exactly. ie.
- JAN-01-1980 is valid
- 123-01-1980 is not valid
- ABC-01-1980 is not valid
- [Challenge] Save the input to a text file
- [Challenge] Create a login based on that text file. I’ve started this for you with login.php
|
| Skills |
PHP |
| Notes |
- A starter_kit.zip was provided by my instructor.
- HTML, CSS, bootstrap, JavaScript were set up by my instructor.
|
| Link |
Lab 5 |
| Title |
Lab 6 |
SIGNUP |
LOGIN
|
[Code Files]
lab6.txt
signup.txt
login.txt
includes/functions.txt
posts.txt
|
| Client |
BCIT COMP3015 |
| Due Date |
June 10, 2018 |
| Objective |
The purpose of this lab is to work with cookies.
|
| Specifications |
You will notice the login form now has a Remember Me checkbox.
Edit login.php
- On each POST request,
- if the Remember Me checkbox is checked, store the value of the phoneNumber input field in a cookie. Have the cookie expire in 20 days.
- if the Remember Me checkbox is unchecked, expire the cookie that stored the phoneNumber.
- On each GET request, check if a cookie for phoneNumber was received
- if received, set the value of the phoneNumber input field to the value of cookie
Edit signup.php
- On each POST request,
- set a cookie to remember the values the user entered for each field
- set the cookie regardless if the input fields met validation criteria
- do not set the password field
- On each GET request,
- check if a cookie for each input field was received
- set the value of each corresponding input field to the value of the cookie
|
| Skills |
PHP |
| Notes |
- A starter_kit.zip was provided by my instructor.
- HTML, CSS, bootstrap, JavaScript were set up by my instructor.
- login.php
- * It sets cookie on your machine.
- If the Remember Me checkbox is checked with the Phone Number and Password, submitting the form, a cookie for the Phone Number will be set.
- To confirm that, click on the address bar, and hit the Enter key. (GET Request)
- The phone number you entered will be displayed in the Phone Number field.
- signup.php
- * It sets cookie on your machine.
- Please fill up the form. Enter a First Name, a Last Name, (a Password), a Phone Number, and a Date of Birth.
- Hit the Enter key to submit the form. (POST Request)
- Cookie for those inputs will be set.
- To confirm that, click on the address bar, and hit the Enter key. (GET Request)
- The information you entered will be displayed in the form.
|
| Link |
Lab 6 |
SIGNUP |
LOGIN
|
| Title |
Lab 7 |
SIGNUP |
LOGIN |
LOGOUT
|
[Code Files]
lab7.txt
signup.txt
logins.txt
includes/functions.txt
posts.txt
|
| Client |
BCIT COMP3015 |
| Due Date |
June 10, 2018 |
| Objective |
The purpose of this lab is to work with sessions.
|
| Specifications |
Edit signup.php
- On each POST request,
- save the POST’d information in CSV format, into a logins.txt file on a new line.
- create a session variable to flag that the user is signed up aka logged in.
- redirect to lab7.php.
Edit login.php
- On each POST request,
- check if the POST’d credentials match the entries in logins.txt.
- if a match is found, create a session variable to flag that the user has had a successful login, then redirect to lab7.php.
- if a match was not found, display a simple error message.
Edit lab7.php
- Add a link to logout.php
- On each page load, check if the session variable used to flag a logged in user exists.
- Only run the rest of the script if the session variable exists.
- if the session variable doesn’t exist, redirect the user to login.php.
Create a logout.php
- Destroy the current session and redirect the user to login.php.
Challenge
- Store the passwords with a one way hash, such as MD5.
- Take it step further, store the passwords with a bcrypt library.
-
If a non-logged in user attempts to access lab7.php, not only redirect them to login.php, but provide an error message saying “Break in attempt detected!”
- Take it a step further, send proper HTTP redirect codes.
-
log the users IP address of break in attempts to unauthorized.txt
- Look into $_SERVER.
|
| Skills |
PHP |
| Notes |
- A starter_kit.zip was provided by my instructor.
- HTML, CSS, bootstrap, JavaScript were set up by my instructor.
- signup.php
- * It sets session on your machine.
- Fill out the form with First Name (alphabet only), Last Name (alphabet only), Password (alphanumeric only), Phone Number ((000) 000 0000), Date of Birth (MMM-DD-YYYY).
- Click Sign Up! button.
- To confirm the sign up, log in with the information you entered for the sign up.
- login.php
- * It sets session on your machine.
- Enter the Phone Number and Password you chose for the sign up.
- On a successful login, you are redirected to lab7.php page.
- To log out, click the Logout button.
|
| Link |
Lab 7 |
SIGNUP |
LOGIN |
LOGIN
|
| Title |
Lab 8 |
PICTURE |
VIEWER
|
[Code Files]
picture.txt
viewer.txt
|
| Client |
BCIT COMP3015 |
| Due Date |
June 17, 2018 |
| Objective |
The purpose of this lab is to work with headers and GD image functions. There is no starter kit.
|
| Specifications |
Create a picture.php
- Have a form that asks for a file upload type and for a text input type:
- POST to picture.php
- the upload destination location is up to you
- you may assume only JPEG pictures are uploaded; no need to enforce validation
- On each POST request:
- write the submitted text on to the JPEG image
- the location of the text is up to you, top left corner is fine
- the color of the text is up to you, red is fine
- Save a new copy of this modified image
- Display this modified image within a
element
Create a viewer.php
- On each GET request:
- the parameter name will be provided
- look to see if a file matching name exists
- if the file exists, send this image back to the client for rendering
- do not use any HTML; use proper HTTP headers
- Hint: look up imagecreatefromjpeg(). This is in both your notes and in the PHP manual.
- Challenge
- Modify your lab 7 solution so that when a picture is uploaded, a thumbnail is created as well. You can store this in a separate folder.
- For each panel, change the src attribute to grab from the thumbnail instead.
|
| Skills |
PHP |
| Notes |
|
| Link |
Lab 8 |
PICTURE |
VIEWER
|
| Title |
Lab 9 |
|
| Client |
BCIT COMP3015 |
| Due Date |
June 24, 2018 |
| Objective |
The purpose of this lab is to work with SQL and PHP.
There is no starter kit.
|
| Specifications |
- Create a create.txt – put your SQL statements into a plain text file named create.txt.
Provide SQL statements that will do the following
- Create a database named lesson09
- Create a table named books that has the following structure
- ID must auto increment
- Create an insert.txt – put your SQL statements into a plain text file named insert.txt.
Provide SQL statements that will do the following
- Insert 3 rows of data into the books table you created
- Your table should look similar to this
- Create a table.php – this script will do the following
- Connects to the local database server
Use good file name conventions
Make sure your username and passwords are variables that your instructor can change
Selects the database lesson09 to use
Select all rows from the table.
Display all rows selected into an HTML table
|
| Skills |
PHP, MySQL, Terminal(mac) |
| Notes |
|
| Link |
|
| Title |
INDEX.PHP |
SEARCH.PHP
|
[Code Files]
index.txt
search.txt
includes/functions.txt
posts.txt
|
| Client |
BCIT COMP3015 |
| Due Date |
June 3, 2018 |
| Objective |
The purpose of this assignment is to cover material you’ve learned in lessons 1 - 5.
|
| Specifications |
- The assignment will be a basic input and output web application; messages will be posted and presented with a few PHP scripts. This should build off of the ideas from your labs.
- index.php
- This is the landing page that your visitors will start on when they visit your web application. All posts inside the provided posts.txt file will be displayed as a panel.
- Modify this page so that on each GET request
- If posts.txt doesn’t exist or doesn’t have any valid lines, display an error
- Each line in posts.txt is displayed in a panel, similar to the sample panel
- Group posts top-down, by priority in the order of: Important (1), High (2), Normal (3)
- Each panel will have a color based on the priority. This is made easy with CSS
- Important priority will have the CSS class panel-danger
- High priority will have the CSS class panel-warning
- Normal priority will have the CSS class panel-info
- And on each valid POST request
- Append a line to posts.txt
- Move uploaded files to uploads/. You can assume this directory always exists
- PHP should check that only JPEG is uploaded. An extension check is not enough!
- Add some uniqueness to the final file name
- Validate all input fields using regular expressions. All fields are required.
- First name, last name and title are alphabetical only. You may allow spaces.
- The comment can contain alphabets, digits, punctuation but not < or >
- Priority can only be digits
- Display a basic error message if any of the inputs, including the image, is invalid
- posts.txt
- This file is provided.
- The format of each line aka each post is
- FirstName|LastName|Title|Comment|Priority|image|post time
- Some lines in posts.txt are missing fields. This is intentional – you are to ignore these lines as they are considered invalid entries
- posts.txt is not , separated rather it’s | separated. This is intentional – this allows your users to use , in their comments
- search.php
- This page will display a basic search bar for your visitor to search the posts of the web application.
- Modify the form so that it GET’s to itself
- Validate input using regular expressions so that alphabet-only text is accepted
- A simple error message on invalid input is fine
- Do not include invalid lines/posts from the posts.txt file in your results
- Display each found result in a simple table
- Include the full name of the author, title of the post and a human readable version of the posted time
- Group results top-down, by priority in the order of: Important (1), High (2), Normal (3)
- Each result row will have a color based on the priority. This is made easy with CSS
- Important priority will have the CSS class danger
- High priority will have the CSS class warning
- Normal priority will have the CSS class info
- At the bottom of the page, display the total number of results found
- A search result is qualified if the input string is in the comment field of a post
- includes/functions.php
- There will be a folder called includes/ with a file named functions.php. This is simply a design choice to help you architect your code.
- Any functions that you wish to define and assist your PHP pages should go into this file.
- Careful design of functions can make this assignment much easier.
- uploads/
- You can assume this directory always exists. All uploaded, validated images will be placed here.
|
| Skills |
PHP |
| Notes |
- A starter_kit.zip was provided by my instructor.
- HTML, CSS, bootstrap, JavaScript were set up by my instructor.
|
| Link |
INDEX.PHP |
SEARCH.PHP
|
| Title |
Assignment2 |
|
| Client |
BCIT COMP3015 |
| Due Date |
July 1, 2018 |
| Objective |
The purpose of this assignment is to cover material you’ve learned in lessons 6 - 9.
Start with the starter_kit.zip that your instructor will provide you. This is a combination
of your assignment 1 solution and a few labs. You may use your own solution from assignment 1.
The assignment builds on top of the input and output of data. However in this assignment, you
will not be persisting and reading data to/from a flat file. You will be using a MySQL database.
|
| Specifications |
- signup.php
- This page allows visitors to create an account.
- Modify this page so that on each POST request
- insert the POST’d data to the database table logins
- when inserting the valid phoneNumber field, filter out anything that isn’t a digit.
- i.e. if 604-123-1234 was entered, save 6041231234.
- this function may help - preg_replace()
- it is intentional that this specific function has been left out of your notes; having to
read the PHP documentation is intentional
- if valid data was successfully inserted,
- set a session variable to flag a valid login,
- set a session variable for the first name,
- set a session variable for the last name, and
- redirect to index.php
- login.php
- This page will allow visitors to log in to the web application. It currently POST’s to itself and
sets the cookies accordingly.
- Modify this page so that
- it validates the phoneNumber field the same way as the signup.php page.
- it validates the phoneNumber field the same way as the signup.php page.
- it checks credentials against the database table logins
- if the credentials exist,
- set a session variable to flag a valid login,
- set a session variable for the first name,
- set a session variable for the last name, and
- redirect to index.php
- index.php
- This is the landing page that your visitors will start on when they visit your web application, after
a successful login/signup.
- Modify this page so that on each request
- if the client is not logged in, redirect to login.php
- instead of reading from posts.txt, read from the database table posts
- The starter kit makes use of the getPosts() function – modify this function to
read from the database table posts
- using the session variables for first name and last name, pre-populate the firstName
and lastName fields of the New Post modal
-
You will also notice a new button below every picture labeled Download. You will notice the
current link is to download.php
- Modify this link so it tags on a GET parameter named filename
- the value of filename is the file name of the picture being displayed
- And on each valid POST request
- insert the POST’d values into the database table posts
- The starter kit makes uses of the insertPost() function – modify this function to
insert into the database
- There’s no need to change the existing validations, but also do not remove them.
- search.php
- This page will display a basic search bar for your visitor to search the posts of the web
application.
- Modify this page so that on each request
- If the client is not logged in, redirect to login.php
- Instead of reading from posts.txt, read from the database table posts
- The starter kit makes use of the searchPosts() function – modify this function to
read from the database table posts
- You can search at the database level by use the LIKE clause your query
select * from tablename where a_field like "%phrase%"
- This will find all columns from the table tablename, where the a_field column
contains the word phrase in it, with characters before and after phrase.
- download.php
-
This page will be used to prompt the user to download a picture. This page isn’t meant to return
any DOM elements for the client to render. However, this page will send the contents of a picture
back to client, prompting the client to download the file.
- Setup this page so that
- it takes a GET parameter named filename
- based on the value of filename, check if a file in uploads/ has the same file name
- if so, send the contents to the client
- be sure to set the proper headers so that the client saves the file rather than rendering
the file
- logout.php
- Simple page that clears all session data and redirects the client back to login.php.
- data/sql.txt and data/comp3015.sql
- sql.txt is simply a series of MySQL statements that you can copy and paste into your
MySQL client to seed your database before starting the assignment.
- comp3015.sql is a database dump of the exact same data produced by sql.txt. This is for
people who wishes to use import tools instead of copy and pasting the contents of sql.txt.
- includes/functions.php
- There will be a folder called includes/ with a file named functions.php. This is simply
a design choice to help you architect your code.
- Any functions that you wish to define and assist your PHP pages should go into this file.
- Careful design of functions can make this assignment much easier.
- uploads/
- You can assume this directory always exists. All uploaded, validated images will be placed here.
|
| Skills |
PHP, MySQL, Terminal (mac) |
| Notes |
|
| Link |
Assignment 2 |
| Title |
Assignment3 |
|
| Client |
BCIT COMP3015 |
| Due Date |
July 8, 2018 |
| Objective |
The purpose of this assignment is to cover material you’ve learned in lessons 6 – 9.
This is a combination
of your assignment 1 solution, assignment 2 solution, and a few labs. You may use your own
solution from assignment 2.
The assignment builds on top of the database queries you’ve learned. Also, you’ll be dealing
with session management.
|
| Specifications |
- signup.php, download.php, index.php, logout.php
- There is no change required for these 4 pages – use them as is from your starter kit.
- login.php
- This page will allow visitors to log in to the web application. For assignment 3, it will check for
an administrator login.
- Modify this page so that
- it checks credentials against the database table logins
- if the credentials exist and they are a normal user,
- set a session variable to flag a valid login,
- set a session variable for the first name,
- set a session variable for the last name, and
- redirect to index.php
- if the credentials exist and they are an administrator user
- set all the session variables for a normal user
- set a flag that marks this user as an administrator
- to determine if a user is an administrator, there will be an admin flag added to the
logins table. See the section on data/sql.txt.
- search.php
- This page will display a basic search bar for your visitor to search the posts of the web
application.
- Modify this page so that
- If the client is not logged in, redirect to login.php
- Based on the session variables, you’ll know if the user is an administrator or not
- if they are, each search result will have a link to edit.php and delete.php
- pass the posts’ id as a GET parameter to edit.php and delete.php
- if they are not an administrator, the links to edit.php and delete.php should be
hidden
- Add a link to users.php
- users.php
- This page will display a basic user information for your administrators
- Modify this page so that on each request
- If the client is not logged in as an administrator, redirect to login.php
- Simply display a list of all users of the system
- Include their first name, last name and date of birth
- This may feel useless – and it is – but if you have time and wish to take this assignment further,
provide CRUD functionality for users. No bonus marks.
- edit.php
- This page will be used by the administrator to edit a specific post.
- Setup this page so that
- if the client is not logged in as an administrator, redirect to login.php
- it takes a GET parameter named id
- be sure to sanitize the GET parameter
- based on the value of id, check if a post in the database indeed exists with that id number
- if so, populate the editable input fields with values from the database
- the editable form will POST to edit.php
- use the same input validation rules as with the index.php page
- delete.php
- This page will be used by an administrator to delete a specific post
-
Setup this page so that
- if the client is not logged in as an administrator, redirect to login.php
- it takes a GET parameter named id
- be sure to sanitize the GET parameter
- based on the value of id, find the post in the database with that id number and delete it
- if the post was deleted, display a message confirming so
- if the post was not found, display a message indicating so
- data/sql.txt and data/comp3015.sql
- sql.txt is simply a series of MySQL statements that you can copy and paste into your
MySQL client to seed your database before starting the assignment.
- comp3015.sql is a database dump of the exact same data produced by sql.txt. This is for
people who wishes to use import tools instead of copy and pasting the contents of sql.txt.
- What’s different about this set of SQL is that the logins table now has a new column admin
to flag an administrator account.
- Also, an administrator account has been inserted for you – verify administrator logins against
this row. If you wish to massage the data, such as hashing the password, feel free to do so by
dropping this row and inserting your own.
- includes/functions.php
- There will be a folder called includes/ with a file named functions.php. This is simply
a design choice to help you architect your code.
- Any functions that you wish to define and assist your PHP pages should go into this file.
- Careful design of functions can make this assignment much easier.
- uploads/
- You can assume this directory always exists. All uploaded, validated images will be placed here.
|
| Skills |
PHP, MySQL, Terminal (mac) |
| Notes |
|
| Link |
Assignment 3 |
NOTE: Requirements above are excerpts from my COMP1850 instructor's instruction sheets.