Broad Network


The Environment and the argv Predefined Variables in PHP

PHP Variables with Security Considerations - Part 3

Foreword: In this part of the series, I talk about the environment and the argv predefined variables in PHP. I also talk about the argc variable that is associated with the argv variable.

By: Chrysanthus Date Published: 17 Oct 2018

Introduction

This is part 3 of my series, PHP Variables with Security Considerations. In this part of the series, I talk about the environment and the argv predefined variables in PHP. I also talk about the argc variable that is associated with the argv variable. You should have read the previous parts of the series before coming here, as this is the continuation.

Environment Variables
The environment to a running program (script) refers to the programming features that are related to the program as it runs. Environment variables are variables of these features, provided by the operating system. PHP can access environment variables. Some environment variables between the unix and windows operating systems have slightly different names.

Examples of Environment Variables

PATH: this is a list of directory paths. When the user types a command without providing the full path, this list is checked to see whether it contains a path that the command leads to.

LANG: this is the language the program uses. It can be English, French, German, etc.

USER: You might have gone to a cyber cafe and use a computer without logging in. This is because the owner of the cyber cafe allowed you to do so. Normally, every user of a particular computer should have a user-name and password. This variable gives the user-name for the program running.

MAIL: email is not a new thing. In fact it was invented before the Internet. This variable indicates where the mails of the user running the program, are to be found.

TEMP: When a program is running, it might need to be saving temporary files. When the program finishes running, the temporary files are erased. This variables gives the directory (and path) where running programs store temporary files.

There are other environmental variables.

Environment Variables and PHP
With PHP, the user of a script (program) can be known with the following code:

<?php

    echo 'My username is ' . $_ENV["USER"] . '!';

?>

If this does not work, then use,

<?php

    phpinfo();

?>

This will give you a lot of information including environment variables (the appropriate names) and their values.

$argv and $argc and Variables
The console, refers to the computer monochrome (black and white) screen. The monochrome screen is no longer used, today. For the Windows Operating System, you use the Command Prompt Window, in place of the monochrome screen.

In a PHP script, the $argv variable is an inbuilt PHP array that has the arguments sent to the script from the console; when you command a PHP script file to run. The first element, $argv[0] is the name of the PHP script (program) file. The rest of the arguments are the arguments typed when the DOS command was typed (and then the Enter Key pressed).

$argv is an inbuilt integer variable that holds the number of arguments sent (to the $argv array). This number includes the first element, which is the file-name.

Type the following code and safe the file as temp.php, in your home directory (inside the user's directory), of your computer:

<?php

    $stdin = fopen('php://stdin', 'r');

    $stdout = fopen('php://stdout', 'w');

    echo $argc, "n";

    echo "n";

    echo $argv[0], "n";
    echo $argv[1], "n";
    echo $argv[2], "n";
    echo $argv[3], "n";

?>

I will say more about this code later.

Open the Command Prompt window (use your operating system help feature to know how to do this).

Type the following command with the arguments, and press Enter (key):

    php temp.php one two three

The output is:

    4

    one
    two
    three

That is it for this 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

BACK

Comments