Building Database Applications in PHP
Subscribe to more Courses.
COLLABORATOR CHANNELS
YOUCOURSE - COURSES FROM BEST UNIVERSITYS IN THE WORLD - https://www.youtube.com/@YouCourse
ARQUITECTURAS - ARCHITECTURE CHANNEL, DOCUMENTALS AND MORE - https://www.youtube.com/@ArquitecturasYT
CADESIGNERS - SOFTWARE CHANNEL AND MODELS - https://www.youtube.com/@CADesigners
UNDERGROUND - Subscribe! Free Music - Creative Commons - https://www.youtube.com/@UndergroundMC
FACEBOOK GROUPS
ARQUITECTURAS - ARCHITECTURE CHANNEL - https://www.facebook.com/groups/arquitecturasyt
YOUCOURSE - COURSES FROM BEST UNIVERSITYS IN THE WORLD - https://www.facebook.com/groups/youcourse/
CADESIGNERS - SOFTWARE DESIGN CHANNEL - https://www.facebook.com/groups/cadesignersyt
UNDERGROUND - Free Music - Creative Commons - https://www.facebook.com/groups/undergroundyt
Facebook - https://www.facebook.com/YouCourseYT
Twitter - https://twitter.com/YouCourseYT
Tik Tok - https://www.tiktok.com/@youcourseyt
Kwai - YouCourse - @yourcourse
Thank's For Support!
University of Michigan
The mission of the University of Michigan is to serve the people of Michigan and the world through preeminence in creating, communicating, preserving and applying knowledge, art, and academic values, and in developing leaders and citizens who will challenge the present and enrich the future.
About this Specialization
This Specialization is an introduction to building web applications for anybody who already has a basic understanding of responsive web design with JavaScript, HTML, and CSS. Web Applications for Everybody is your introduction to web application development. You will develop web and database applications in PHP, using SQL for database creation, as well as functionality in JavaScript, jQuery, and JSON.
Over the course of this Specialization, you will create several web apps to add to your developer portfolio. This Specialization (and its prerequisites) will prepare you, even if you have little to no experience in programming or technology, for entry level web developer jobs in PHP.
You’ll demonstrate basic concepts, like database design, while working on assignments that require the development of increasing challenging web apps. From installing a text editor to understanding how a web browser interacts with a web server to handling events with JQuery, you’ll gain a complete introductory overview of web application development.
About this Course
In this course, we'll look at the object oriented patterns available in PHP. You'll learn how to connect to a MySQL using the Portable Data Objects (PDO) library and issue SQL commands in the the PHP language. We'll also look at how PHP uses cookies and manages session data. You'll learn how PHP avoids double posting data, how flash messages are implemented, and how to use a session to log in users in web applications. We'll then build the first 'complete' application that has multiple screens to Create, Read, Update and Delete (CRUD) our data. This brings all the previous concepts together and will form the basis for all later web applications. It is assumed that learners have already taken the Building Web Applications course in this specialization.
Slides & Embedded Links
http://php.net/manual/en/ref.datetime.php
http://php.net/manual/en/class.datetime.php
https://www.wa4e.com/code/objects/date.php
https://www.wa4e.com/code/objects/nonobj.php
https://www.wa4e.com/code/objects/withobj.php
Object Oriented Libraries in PHP
http://php.net/manual/en/language.oop5.magic.php
Object Life Cycle in PHP
https://www.wa4e.com/code/objects/party.php
https://www.wa4e.com/code/objects/hello.php
Object Inheritance in PHP
https://www.wa4e.com/code/objects/goodbye.php
https://www.wa4e.com/code/objects/scratch.php
https://www.wa4e.com/code/objects/visibility.php
http://www.php.net/manual/en/language.oop5.visibility.php
https://milneopentextbooks.org/the-missing-link-an-introduction-to-web-development-and-programming/
In this page, you will install a programmer text editor, install a bundled all-in-one PHP/MySql application, and explore that application briefly.
We have separate pages for each of the commonly used Operating Systems:
- Setting up the MAMP PHP/MySql Environment on a Macintosh
- Setting up the MAMP PHP/MySql Environment on a Windows
- Setting up the XAMPP PHP/MySql Environment in Microsoft Windows
A simple approach is to search for "How to install LAMP on Ubuntu" - put your variant of Linux in the search - You should find lots of pages. If we find good guides, we will list them here:
Ask Ubuntu: How to set up a LAMP stack
You can use NGrok to make this tunnel through your firewall. We provide documentation on how to use this application below as well as video walkthroughs both on YouTube and Coursera.
If you have the more advanced skills to put your applications up on the Internet with a real domain name, you do not need to use a tunnel program.
http://www.wa4e.com/code/pdo.zip
http://www.wa4e.com/code/rps.zip (Course 1 Web Apps & PHP Week 8 login.php)
Slides & Embedded Links
http://php.net/manual/en/pdo.error-handling.php
http://ophilipp.free.fr/op_tail.htm
You can explore a sample solution for this problem at http://www.wa4e.com/solutions/autosdb/
Resources
There are several resources you might find useful:
- Review the SQL language
- Using PDO in PHP
- Documentation on HTML Injection
- Documentation from www.php.net on how to use PDO to connect to a database.
- Documentation on SQL Injection
- Documentation on PHP PDO Prepared Statements
- You can look through the sample code from the lecture. It has examples of using PDO to communicate with a database:
Here are some general specifications for this assignment:
- You must use the PHP PDO database layer for this assignment. If you use the "mysql_" library routines or "mysqli" routines to access the database, you will receive a zero on this assignment.
- Your name must be in the title tag of the HTML for all of the pages for this assignment.
- Your program must be resistant to HTML Injection attempts. All data that comes from the users must be properly escaped using the htmlentities() function in PHP. You do not need to escape text that is generated by your program.
- Your program must be resistant to SQL Injection attempts. This means that you should never concatenate user provided data with SQL to produce a query. You should always use a PDO prepared statement.
- Please do not use HTML5 in-browser data validation (i.e. type="number") for the fields in this assignment as we want to make sure you can properly do server side data validation. And in general, even when you do client-side data validation, you should still validate data on the server in case the user is using a non-HTML5 browser.
You already should have a PHP hosting environment such as MAMP or XAMPP installed or have some other access to a MySQL client to run commands.
You will need to create a database, a user to connect to the database and a password for that user using commands similar to the following:
You will also need to create and configure a table in the new "misc" database using the following SQL commands:
The changes to index.php are new wording and pointing to autos.php to test for login bypass.
Specifications for the Login Screen
Much of the login.php is reused and extended from the previous assignment [Course 1 Week 8 - Building Web Applications in PHP, Rock Paper Scissors]. The salt and hash computation and most of the error checking comes across unchanged. The password continues to be 'php123'.
The login screen needs to have some error checking on its input data. If either the name or the password field is blank, you should display a message of the form:
Email and password are required
Note that we are using "email" and not "user name" to log in in this assignment.
If the password is non-blank and incorrect, you should put up a message of the form:
Incorrect password
For this assignment, you must add one new validation to make sure that the login name contains an at-sign (@) and issue an error in that case:
Email must have an at-sign (@)
If the incoming password, properly hashed matches the stored stored_hash value, the user's browser is redirected to the autos.php page with the user's name as a GET parameter using:
header("Location: autos.php?name=".urlencode($_POST['who']));
You must also use the error_log() function to issue the following message when the user fails login due to a bad password showing the computed hash of the password plus the salt:
error_log("Login fail ".$_POST['who']." $check");
When the login succeeds (i.e. the hash matches) issue the following log message:
error_log("Login success ".$_POST['who']);
Make sure to find your error log and find those error messages as they come out:
[11-Feb-2016 15:52:03 Europe/Berlin] Login success csev@autos.com
[11-Feb-2016 15:52:13 Europe/Berlin] Login fail csev@autos.com 047398bd0e0171f4954760f5f542121a
In order to protect the database from being modified without the user properly logging in, the autos.php must first check the $_GET variable to see if the user's name is set and if the user's name is not present, the autos.php must stop immediately using the PHP die() function:
die("Name parameter missing");
To test, navigate to autos.php manually without logging in - it should fail with "Name parameter missing".
If the user is logged in, they should be presented with a screen that allows them to append a new make, mileage and year for an automobile. The list of all automobiles entered will be shown below the form. If there are no automobiles in the database, none need be shown.
If the Logout button is pressed the user should be redirected back to the index.php page using:
header('Location: index.php');
When the "Add" button is pressed, you need to do some input validation.
The mileage and year need to be integers. If is suggested that you use the PHP function is_numeric() to determine if the $_POST data is numeric. If either field is not nummeric, you must put up the following message:
Mileage and year must be numeric
Also if the make is empty (i.e. it has less than 1 character in the string) you need to put out a message as follows:
Make is required
Note that only one of the error messages need to come out regardless of how many errors the user makes in their input data. Once you detect one error in the input data, you can stop checking for further errors.
If the user has pressed the "Add" button and the data passes validation, you can add the automobile to the database using an INSERT statement.
When you successfully add data to your database, you need to put out a green "success message:
Record inserted
Once there are records in the database they should be shown below the form to add a new entry.
Optional Challenges
This section is entirely optional and is here in case you want to explore a bit more deeply and test your code skillz.
Here are some possible improvements:
- Always show the automobiles sorted by make regardless of the order they were entered into the application. Hint: use "ORDER BY".
- Add an optional URL field to your tables and user interface. Validate the URL to make sure it starts with "http://" or "https://". If the user enters a URL, in the list of autos, have the make be a clickable anchor tag that opens the image in a new window:
- Medium Difficulty: Use the PHP cURL library to do a GET to the image URL from within PHP and if the URL does not exist, issue an error message to the user and do not add the automobile.
The data in your screen shot(s) should not be the same as these examples.
In this assignment you will build a web based application to track data about automobiles and store the data in a MySQL database.
Assignment: https://www.wa4e.com/assn/autosdb/
Solution: https://www.wa4e.com/solutions/autosdb/
In this assignment you will build a web based application to track data about automobiles and store the data in a MySQL database.
Assignment: https://www.wa4e.com/assn/autosdb/
Solution: https://www.wa4e.com/solutions/autosdb/
Sample Code
https://www.wa4e.com/code/sessions.zip
Sessions
www.tsugi.org
http://php.net/manual/en/function.session-start.php
http://php.net/manual/en/function.session-destroy.php
http://www.wa4e.com/code/sessions/sessfun.php
Sessions Without Cookies
http://www.wa4e.com/code/sessions/nocookie.php
Auto-Grader: Cookies and Session
In this assignment we learn about viewing cookies in our browser.
Sample Code: https://www.wa4e.com/code/sessions.zip
Sample Code
https://www.wa4e.com/code/route.zip
Slides & Embedded Links
http://www.wa4e.com/code/sessions/cookie.php
Wiki - Post/Redirect/Get
https://en.wikipedia.org/wiki/Post/Redirect/Get
Mozilla Developer Network - Sending form data
Where does the data go, and how do we handle it when it gets there?
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_and_retrieving_form_data
Sample solution
You can explore a sample solution for this problem at http://www.wa4e.com/solutions/autosess/
Resources
There are several resources you might find useful:
- Review the SQL language
- Using PDO in PHP
- Documentation on Post-Redirect-GET Pattern
- You can look though the sample code from the lecture. It has examples of using sessions and POST-Redirect: https://www.wa4e.com/code/route.zip
Here are some general specifications for this assignment:
You must use the PHP PDO database layer for this assignment. If you use the "mysql_" library routines or "mysqli" routines to access the database, you will receive a zero on this assignment.
Your name must be in the title tag of the HTML for all of the pages for this assignment.
Your program must be resistant to HTML Injection attempts. All data that comes from the users must be properly escaped using the htmlentities() function in PHP. You do not need to escape text that is generated by your program.
Your program must be resistant to SQL Injection attempts. This means that you should never concatenate user provided data with SQL to produce a query. You should always use a PDO prepared statement.
Please do not use HTML5 in-browser data validation (i.e. type="number") for the fields in this assignment as we want to make sure you can properly do server side data validation. And in general, even when you do client-side data validation, you should still validate data on the server in case the user is using a non-HTML5 browser.
Databases and Tables Required for the Assignment
This assignment reuses the tables from the previous assignment. No additional tables are necessary.
The changes to index.php are new wording and pointing to view.php to test for login bypass.
The basic functionality, password checking using salt and hashing, error logging, and data validation for the login.php is the same as in the previous assignment.
There are several changes that are needed for this assignment as follows:
- The script must redirect after every POST. It must never produce HTML output as a result of a POST operation.
Specifications for the Auto Database Screens
The autos.php script from the previous assignment is broken into two scripts in this assignment. The view.php script shows the list of automobiles in the database and the add.php script handles adding new automobiles to the database but does not list any autos. The view.php includes a link to add.php and logout.php and the add.php has a Cancel button.
In order to protect the database from being modified without the user properly logging in, the view.php and add.php must first check the session to see if the user's name is set and if the user's name is not present, the view.php must stop immediately using the PHP die() function:
In view.php if the Logout button is pressed the user should be redirected back to the logout.php page. The logout.php page should clear the session and immediately reditect back to index.php:
In the add.php script, when you successfully add data to your database, you need to redirect back to view.php and pass a "success message" to view.php using the session:
For this assignment you will hand in:
- A screen shot (including the URL) of your login.php rejecting an account without an at-sign (@). You must also include the developer console network tab showing both the POST and GET.
- A screen shot of your error log showing correct messages for both a successful and failed login attempt.
- A screen shot (with URL) of your add.php showing a data validation error. You must also include the developer console network tab showing both the POST and GET.
- A screen shot (including the URL) of your view.php with three vehicles in the list. At least one of the vehicles must have '<b>' in its title and it must be shown properly (i.e. the title should not be bold)
Some of the screenshots ask to see the developer console demonstrating the POST-Redirect pattern similar to the following:
In this assignment you will expand a web based application to track data about automobiles and store the data in a MySQL database. All interactions will follow the POST-Redirect pattern where appropriate.
Assignment: https://www.wa4e.com/assn/autosess/
Solution: https://www.wa4e.com/solutions/autosess/
In this assignment you will expand a web based application to track data about automobiles and store the data in a MySQL database. All interactions will follow the POST-Redirect pattern where appropriate.
Assignment: https://www.wa4e.com/assn/autosess/
Solution: https://www.wa4e.com/solutions/autosess/
Sample Code
https://www.wa4e.com/code/crud.zip
Slides & Embedded Links
The sample code from the CRUD lecture is a simple working CRUD application: http://www.wa4e.com/code/crud.zip
General Specifications
Here are some general specifications for this assignment:
- Your name must be in the title tag of the HTML for all of the pages for this assignment.
- All data that comes from the users must be properly escaped using the htmlentities() function in PHP. You do not need to escape text that is generated by your program.
- You must follow the POST-Redirect-GET pattern for all POST requests. This means when your program receives and processes a POST request, it must not generate any HTML as the HTTP response to that request. It must use the "header('Location: ...');" function and "return" to send the location header and redirect the browser to the same or a different page.
- Please do not use HTML5 in-browser data validation (i.e. type="number") for the fields in this assignment as we want to make sure you can properly do server side data validation. And in general, even when you do client-side data validation, you should still validate data on the server in case the user is using a non-HTML5 browser.
You can experiment with a reference implementation at: http://www.wa4e.com/solutions/autoscrud
This assignment will be automatically graded and so your web server will need an Internet-accessible URL so you can submit it for autograding. To achieve this you will need to install and use a piece of software called "ngrok". Instructions for installing and using ngrok are available at: http://www.wa4e.com/ngrok.php
Please see the process for handing in the assignment at the end of this document.
Important: The autograder will demand that your name is in the <title> tag in the head of your document. If the autograder does not find your name, it will run all the tests but will not treat the grade as official.
You can reuse or adapt a table from a previous assignment. This assignment will need a table as follows:
In order to protect the database from being modified without the user properly logging in, the add.php and edit.php must first check the session to see if the user's name is set and if the user's name is not set in the session the they must stop immediately using the PHP die() function:
die("ACCESS DENIED");
To test, navigate to add.php manually without logging in - it should fail with "ACCESS DENIED".
If the user is not logged in, they will be presented a screen with a welcome and a link to login.php - they should not see the table of data.
The autograder will log in to your program with the following account and password:
Account: umsi@umich.edu
Password: php123
The login screen needs to have some error checking on its input data. If either the name or the password field is blank, you should display a message of the form:User name and password are required
If the password is non-blank and incorrect, you should put up a message of the form:
Incorrect password
Note: Please name your form fields in login.php exactly as follows for autograding:
Once the user is logged in, they should should be redirected to index.php where they will be shown see a list of the automobiles in the database in a table similar to the following:
If there are no rows in the table, do not print out the table, but simply print out "No rows found".
There should also be options to Add a New Entry and Log Out presented after the table.
If the Logout link is pressed the user should be sent to the logout.php page which clears session variables and redirects back to index.php.
Adding New Records
When the user asks to add a new record, they should be presented with a screen that allows them to append a new automobile. Each automobile will have the following fields:
- Make
- Model
- Year - must be an integer - use is_numeric() to validate
- Mileage - must be an integer - use is_numeric() to validate
If the data validates and the add is successful, redirect to index.php with a successful flash message:
Record added
Editing Records and Validation Errors
When you edit a record, the prior data must be shown and properly escaped. All of the data validation must be performed on the edit data as required in the add.php. Make sure to include the "id" parameter (you may name this variable differently) on the redirect statement in the edit.php when an error is detected:
Deleting Records
When the user selects the "Delete" link from the list of Automobiles you should put up a form with "Delete" and "Cancel" options.
If the "Delete" button is pressed, the record is deleted and the user is redirected to index.php with a success message:
Record deleted
Auto-Grader: Autos CRUD
In this assignment you will expand a web based application to Create, Read, Update, and Delete (C.R.U.D.) data in a MySQL database. All interactions will follow the POST-Redirect and Flash Message patterns where appropriate. All generated HTML must be properly protected using htmlentities().
Assignment: https://www.wa4e.com/assn/autoscrud
Solution: https://www.wa4e.com/solutions/autoscrud
In this assignment you will expand a web based application to Create, Read, Update, and Delete (C.R.U.D.) data in a MySQL database. All interactions will follow the POST-Redirect and Flash Message patterns where appropriate. All generated HTML must be properly protected using htmlentities().
Assignment: https://www.wa4e.com/assn/autoscrud
Solution: https://www.wa4e.com/solutions/autoscrud





No hay comentarios:
Publicar un comentario