CPSC 343 | Database Theory and Practice | Fall 2024 |
This document is intended to describe the local environment for working with HTML and PHP in CPSC 343. Refer to the PHP Quick Start for information on the PHP language, the PHP Web Quick Start for information on getting information about the web server environment, processing HTML forms, and working with sessions and cookies, or the PHP MySQL Quick Start for information on interacting with MySQL from PHP.
Your HTML and PHP files will need to be viewed through a webserver configured to run PHP files. It is recommended to use VSCode as an editor. You'll then need to install the PHP Server extension to view/run your files:
In VSCode, go to View→Extensions.
In the search box, type php server.
The extension you want (PHP Server, author brapifra) should be at the top of the search results. Click Install.
It's recommended that you also install a code formatter for PHP/HTML — this makes it easy to auto-format your files for greater readability.
In VSCode, go to View→Extensions.
In the search box, type php intelephense.
The extension you want (PHP Intelephense, author Ben Mewburn) should be at the top of the search results. Click Install.
To use the code formatter, use ctrl-shift-I or invoke the Command Palette (ctrl-shift-P) and search for "format document". You can also configure VSCode to automatically format on save: File→Preferences→Settings, then search for "format on save" and toggle the checkbox on.
It is strongly recommended that you use VSCode in the HWS Linux environment, whether on the computers in the Rosenberg 009 and Lansing 310 labs or through the Linux virtual desktop. Working on your own computer requires also downloading and installing php, which can be somewhat involved depending on your operating system.
Files containing PHP code must have a filename ending with .php; files containing only HTML code must have a filename ending with .html (or .php, but there's no reason to make the PHP processor handle a file without any PHP).
To view the file using VSCode:
Install the PHP Server extension if you haven't already.
Open an editor tab with the file you want to view. (Make sure it is the currently active tab.)
Open the Command Palette with View→Command Palette... or ctrl-shift-P.
If the desired options don't appear at the top of the command list, type php server in the search box.
If the PHP server isn't running, choose PHP Server: Serve project to start the PHP server and open the current file in a browser tab.
If the PHP server is running, save the file and reload in the browser to see changes, or choose PHP Server: Open file in browser to open a new file in a browser tab.
Stop the server with PHP Server: Stop server.
The examples used in the other Quick Start documents are available for you to try out. The URL:
http://bridgeman-vm.hws.edu/cs343f24/quickstart
Note that this link only works on campus. See the instructions on Canvas about off-campus access to the HWS network in the "systems and software" module.
Because the PHP is processed by the webserver, PHP syntax errors and other problems which prevent PHP from executing are logged on the server. You can find these error messages in the Output window in VSCode. (This view is opened automatically when you start the PHP server, but if you've lost it, you can use View→Output to re-open it. Make sure "PHP Server" is selected in the drop-down list in the title bar within the view.)
To examine the HTML received by the web browser (useful for debugging PHP output problems), use your browser's View Source feature. In Firefox, you can right-click on the page and choose "View Page Source" or use the hamburger menu followed by More Tools → Page Source. However, Firefox also has a "feature" where it resends the page request to get the source instead of using a cached copy, so this may not work correctly when session variables and similar things are used. In that case, you can use the Inspector: from the hamburger menu, choose More Tools → Web Developer Tools and then click on the Inspector tab.
If you are not on the campus network, you can still view your HTML and PHP files but you will need to run VPN in order to access the database server. See the instructions on Canvas about off-campus access to the HWS network in the "systems and software" module.