Using PHP

PHP is a server-side scripting language used to dynamically generate HTML pages, among other uses. If you're developing code locally that uses PHP, you'll need to have PHP installed locally and to have the page served to your browser in order to see you work.

Installing PHP

Ubuntu 16.04: $ sudo apt-get install php7.0

PHP test file

It's frequently useful to get info about your PHP installation, which is hard to do from the command line, for some reason. Instead, create a new file test.php with the content

To use this file, move or copy it to the working folder, which must be "servable", whether by Apache, Tomcat, or the built-in PHP server (see below). Opening this file in your browser by whatever URI you normally open files in that folder should display a PHP info page.

Important: don't leave a test.php file in a network-accessible folder, since it reveals information that might be used by an attacker. In particular, it reveals the version of PHP installed, which can help identify vulnerabilities if it's an old version. It's safe to save a copy in your home folder on the VMs, for example, but be sure to delete any test.php in the servable directories under tomcat/webapps/ when you're done with them.

Using PHP locally

In a separate terminal window, run $ php -S localhost:8000 from the directory of the .php file you want to open. This starts the built-in PHP server, which will remain running and report activity to the terminal window (that's why it needs to be in a separate window). In this case, it will listen on port 8000, but you can change that to any unused port.

You can now point your browser to http://localhost:8000/filename.php to open filename.php and see the dynamic results of your PHP code. Specifying the port is important, since otherwise the default will be 80 (HTTP/Apache).

-- Main.JoelG - 2016-11-17


Topic revision: r1 - 2016-11-17, JoelG
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback