Broad Network


Introduction to PHP

Basics of PHP with Security Considerations - Part 1

Foreword: This is the first part of my series, Basics of PHP. In this part of the series, I explain what you need to start programming in PHP.

By: Chrysanthus Date Published: 30 Aug 2018

Introduction

PHP is a scripting (programming) language, which is used at the server of the web. PHP can be used in a stand-alone program or it can be embedded (typed) into an HTML document. PHP stands for Hypertext Preprocessor. The ‘P’ in front of HP was (just) added. PHP is an interpreter language. This is the first part of my series, Basics of PHP. In this part of the series, I explain what you need to start programming in PHP.

The knowledge in this volume is applicable to PHP version 7 and higher (most of it is also applicable to previous versions).

You should read the different parts of the series in the order given.

Security Considerations
You must have heard that important politicians emails have been hacked. Today there is ransom-ware. So when programming today, you have to take precautions that limit cheating (leaks) on the user of the program (application). In this volume (a number of tutorial series), you are also taught how to prevent insecurities.

Pre-Knowledge
In order to study PHP from this volume (these tutorial series), you need to know how to use the Internet. Having basic knowledge in another language called HTML is an advantage, but not required. Having succeeded in Middle School (British O'Level) mathematics is also an advantage but not required. The little mathematics you need to complete the course (volume) is taught to you along with the course, in a simple way (have confidence).

A programming language needs a lot of logical reasoning: that will be taught to you in a simple way, as we go along. You can learn PHP as your first computer language.

Requirements
Here, I give you the requirements to study PHP in this series (volume).
- PHP Binary (application that actually executes PHP program)
- Apache Web Server (application that sends PHP result to the browser)
- Browser
- Text Editor
- Your Personal Computer (with its operating system)

You can download PHP 7 binary and Apache HTTP Server 2.4, both free from the Internet. Just search the Internet and download. They work with many operating systems. Look for the ones for your operating system.

In this series there are many code samples that you will be trying (re-typing or copy and paste and then executing).

Installation

I tell you what I have done to get the apache web server and PHP interpreter installed, and running in my personal computer. You should do a similar thing:

Apache version 2.4 for Windows Operating System

I am using Windows 10

I search the web for the zip file, httpd-2.4.35-o110i-x64-vc14.zip and downloaded it free. I unzipped the file to have the directory, httpd-2.4.35-o110i-x64-vc14 . I opened the directory and I saw the file, readme_first.html and another directory, Apache24 . I copied the directory, Apache24 to my root directory to have c:/Apache24 .

Each time I want to program in PHP, I open the directory, c:/Apache24/bin and double click the httpd.exe file. The Apache server starts in a Command Prompt window (black and white). Do not close the window while you are programming in PHP. The web server indicates that it is running by flashing the underscore character. When you finish programming, close this window by just clicking its Close Button.

Installing PHP
I went to the web page, https://windows.php.net/download#php-7.2 . I downloaded the zip file php-7.2.9-Win32-VC15-x64.zip. I unzipped it to have the directory, php-7.2.9-Win32-VC15-x64 . I copied this directory to my root directory, and renamed the directory to have c:/PHP , with PHP in uppercase.

When you install the Apache web server (software), make it to work with PHP. When you install PHP, make it to work with the server (if necessary). To make the web server work with PHP, open the configuration text file, c:/Apache24/conf/httpd.conf . In this configuration file for the server, at the bottom of the page, you have to type:

    #
    LoadModule php7_module "C:/PHP/php7apache2_4.dll"
    AddHandler application/x-httpd-php .php

    #configure the path to php.ini
    PHPIniDir "C:/PHP"

For the server, you put your PHP files (code samples) in the htdocs directory. In your computer, the path is something like:

    c:/Apache24/htdocs

I use the Windows Operating System. You can use a different operating system, if you know how that works.

PHP Script
A PHP script goes into the tag,

                    <?php - - script - - ?>

So, if you have a stand-alone script (program) then the beginning of the file would be <?php and the end of the file would be ?>.

If your script is embedded in an HTML document, then the beginning of the script would be <?php and the end of the script would be ?>. In the case of the HTML document, you effectively have a single HTML tag element, which is <?php - - script - - ?>. You can have many PHP scripts in an HTML document; that is, many <?php - - script - - ?> tags. This tag is better written as

    <?php
    - - script - -
    ?>

The web page with the PHP script (or scripts) must be in the server.  If the program (script) is alone (not in an HTML document) as most scripts in this volume are, the script still has to be in the server. The script is executed by what is called the PHP parser, at the server.

The PHP script in the HTML document at the server, is not seen when the browser is displaying the HTML document. However, after execution, the PHP script can produce HTML elements or text at the server, to fit in the position of the PHP script, replacing the PHP tag. When the HTML document is downloaded by the browser, the HTML elements or text produced at the server would be seen at the browser, in the position where the PHP script (PHP tag) was.

When one or more PHP scripts are embedded in an HTML document at the server, the file name extension of that HTML document has to become .php instead of .htm or .html. If the PHP script is stand-alone (not embedded in HTML document), the extension of the file name still has to be .php .

Your First PHP Script
Copy and paste (or re-type) the following code in your text editor:

<?php

    echo 'Hello World';

?>

Save the file as temp.php in your server’s htdocs directory or root (home) directory. Open your browser. Type the URL, http://localhost/temp.php in your browser (address bar) and click Go. Your browser should display “Hello World” as the only content of the web page. If it were in the Internet (web), you would replace “localhost” with the site address and path to the PHP file, temp.php.

If you tried this example and you did not see “Hello World” at the browser, or it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled, or is not configured properly or you have not install PHP connecting it to the server properly.

Explanation of Code
There is just one line in the tag, which is:

    echo '<p>Hello World</p>';

The word echo means output or type what is in quotes. The quotes may be single or double. What we have in the quotes is the string, “Hello World”. So, the word “echo” replaces the PHP single tag with the string, in the supposed HTML document. It is not all PHP scripts that output text to the browser. Some scripts (or part of a script) just do some execution without outputting anything.

Conclusion
In this part of the series, you have learned how to produce a PHP file and run (execute) it: Use your text editor to type the file. Place the file in the home directory of the web server. Call the file using the URL at the browser.

We stop here and continue in the next part of the series.

Chrys

Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

NEXT

Comments