{"id":658,"date":"2017-02-18T15:44:27","date_gmt":"2017-02-18T15:44:27","guid":{"rendered":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/?page_id=658"},"modified":"2017-12-17T02:43:36","modified_gmt":"2017-12-17T02:43:36","slug":"php-programming-language-basics-predefined-variables","status":"publish","type":"page","link":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-predefined-variables\/","title":{"rendered":"4-4: PHP programming language basics &#8211; predefined variables"},"content":{"rendered":"<p>PHP provides access to a number of predefined functions and variables.<\/p>\n<p>For an exhaustive overview of the predefined variables available in the PHP programming language please check out the related section on <a href=\"http:\/\/php.net\/manual\/en\/reserved.variables.php\">php.net website<\/a>, from which the following complete list was derived.<\/p>\n<div style=\"border:1px solid grey;padding:10px;margin:10px;\">\n<strong>A complete list of PHP predefined variables according to the <a href=\"http:\/\/php.net\/manual\/en\/reserved.variables.php\">php.net website<\/a><\/strong><\/p>\n<p>Superglobals \u2014 Superglobals are built-in variables that are always available in all scopes<br \/>\n$GLOBALS \u2014 References all variables available in global scope<br \/>\n$_SERVER \u2014 Server and execution environment information<br \/>\n$_GET \u2014 HTTP GET variables<br \/>\n$_POST \u2014 HTTP POST variables<br \/>\n$_FILES \u2014 HTTP File Upload variables<br \/>\n$_REQUEST \u2014 HTTP Request variables<br \/>\n$_SESSION \u2014 Session variables<br \/>\n$_ENV \u2014 Environment variables<br \/>\n$_COOKIE \u2014 HTTP Cookies<br \/>\n$php_errormsg \u2014 The previous error message<br \/>\n$HTTP_RAW_POST_DATA \u2014 Raw POST data<br \/>\n$http_response_header \u2014 HTTP response headers<br \/>\n$argc \u2014 The number of arguments passed to script<br \/>\n$argv \u2014 Array of arguments passed to script\n<\/p><\/div>\n<p>In this section we focus on a couple of those built-in variables that we may use in our scripts. In particular we will see how the <strong>$_SERVER array<\/strong> provides access to information on our working environment and we will just hint, for now, at the <strong>$_POST array<\/strong>, that let us retrieve data submitted by website visitors through web forms, an essential bit of the web applications that we will build later on.<\/p>\n<h2>The $_SERVER variable<\/h2>\n<p>The $_SERVER variable, an array with several predefined keys, provides access to many so called &#8220;environmental&#8221; variables about our working environment and our website visitors.<\/p>\n<p>To check all available keys for the $_SERVER array, we can use the array_keys() function that we have introduced in the previous section about <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-arrays\/\">PHP arrays basics<\/a>.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$server_array_keys = array_keys($_SERVER);\r\n\r\n\/\/ Let us format the output nicely in the form of an unordered list embedded in a paragraph\r\necho \"<p><ul>\\n\"; \r\n\r\nforeach($server_array_keys as $key){\r\n    echo \"<li>$key<\/li>\\n\"; \/\/ We include \\n so that in the source code each list element will be on it's own line\r\n}\r\necho \"<\/ul><\/p>\\n\";\r\n?>\r\n\r\n<\/code><\/pre>\n<p>At the time of this writing, the code above will output the following result on our test Linux server:<\/p>\n<ul>\n<li>HTTP_HOST<\/li>\n<li>HTTP_CONNECTION<\/li>\n<li>HTTP_UPGRADE_INSECURE_REQUESTS<\/li>\n<li>HTTP_USER_AGENT<\/li>\n<li>HTTP_ACCEPT<\/li>\n<li>HTTP_ACCEPT_ENCODING<\/li>\n<li>HTTP_ACCEPT_LANGUAGE<\/li>\n<li>PATH<\/li>\n<li>SERVER_SIGNATURE<\/li>\n<li>SERVER_SOFTWARE<\/li>\n<li>SERVER_NAME<\/li>\n<li>SERVER_ADDR<\/li>\n<li>SERVER_PORT<\/li>\n<li>REMOTE_ADDR<\/li>\n<li>DOCUMENT_ROOT<\/li>\n<li>REQUEST_SCHEME<\/li>\n<li>CONTEXT_PREFIX<\/li>\n<li>CONTEXT_DOCUMENT_ROOT<\/li>\n<li>SERVER_ADMIN<\/li>\n<li>SCRIPT_FILENAME<\/li>\n<li>REMOTE_PORT<\/li>\n<li>GATEWAY_INTERFACE<\/li>\n<li>SERVER_PROTOCOL<\/li>\n<li>REQUEST_METHOD<\/li>\n<li>QUERY_STRING<\/li>\n<li>REQUEST_URI<\/li>\n<li>SCRIPT_NAME<\/li>\n<li>PHP_SELF<\/li>\n<li>REQUEST_TIME_FLOAT<\/li>\n<li>REQUEST_TIME<\/li>\n<\/ul>\n<p>You can find the meaning of each one of these keys <a href=\"http:\/\/php.net\/reserved.variables.server\" target=\"_blank\">here<\/a>. Let&#8217;s focus on a few of those.<\/p>\n<p>The general syntax for retrieving the value for each one of these keys is the usual syntax for array with keys: the array name followed by the name of the key enclosed in square brackets:<\/p>\n<pre lang=\"php\"><code>\r\n$_SERVER['key_name']\r\n<\/code><\/pre>\n<h3>SERVER_ADDR<\/h3>\n<p>the SERVER_ADDR key returns the IP address of the server on which the script is running, the web server:<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$server_ip_address = $_SERVER['SERVER_ADDR'];\r\n\r\necho \"<p>The IP address of the server running this script is <strong>$server_ip_address<\/strong><\/p>\\n\";\r\n\r\n?>\r\n<\/code><\/pre>\n<h3>REMOTE_ADDR<\/h3>\n<p>the REMOTE_ADDR key returns the IP address of the visitor&#8217;s computer, where the web browser is running:<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$visitor_ip_address = $_SERVER['REMOTE_ADDR'];\r\n\r\necho \"<p>Welcome visitor from <strong style=\\\"color:red\\\">$visitor_ip_address<\/strong><\/p>\\n\";\r\n\r\n?>\r\n<\/code><\/pre>\n<h3>DOCUMENT_ROOT<\/h3>\n<p>the DOCUMENT_ROOT key returns the path of the web server&#8217;s web root, as defined in the Apache configuration file.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$visitor_ip_address = $_SERVER['REMOTE_ADDR'];\r\n$web_root_path = $_SERVER['DOCUMENT_ROOT'];\r\n\r\necho \"<p>Welcome visitor from <strong style=\\\"color:red\\\">$visitor_ip_address<\/strong><\/p>\\n\";\r\necho \"<p>The web root of our web server is located at <strong style=\\\"color:blue\\\">$web_root_path<\/strong> within the filesystem<\/p>\\n\";\r\n\/\/ By default, on recent Apache2 installations, the web root's path is: \/var\/www\/html\r\n\r\n?>\r\n<\/code><\/pre>\n<h3>SCRIPT_NAME<\/h3>\n<p>The SCRIPT_NAME key returns the path of the script currently running.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$visitor_ip_address = $_SERVER['REMOTE_ADDR'];\r\n\r\necho \"<p>Welcome visitor from <strong style=\\\"color:red\\\">$visitor_ip_address<\/strong><\/p>\\n\";\r\necho \"<p>You are currently running the following script: \".\r\n.\"<\/p>\\n\";\r\n\r\n?>\r\n<\/code><\/pre>\n<p>As an exercise, try running the following script on you own web server and check out the results!<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$visitor_ip_address = $_SERVER['REMOTE_ADDR'];\r\n$web_root_path = $_SERVER['DOCUMENT_ROOT'];\r\n$server_ip_address = $_SERVER['SERVER_ADDR'];\r\n$current_script_name = $_SERVER['SCRIPT_NAME'];\r\n\r\n\r\necho \"<p>Welcome visitor from <strong style=\\\"color:red\\\">$visitor_ip_address<\/strong><\/p>\\n\";\r\necho \"<p>You are currently running the <strong style=\\\"color:red\\\">$current_script_name<\/strong> script<\/p>\\n\";\r\necho \"<p>We are delighted you came to visit our web server at <strong style=\\\"color:red\\\">$server_ip_address<\/strong> whose web root is located at <strong style=\\\"color:red\\\">$web_root_path<strong><\/p>\\n\";\r\n\r\n?>\r\n<\/code><\/pre>\n<h2 id=\"post_array\">The $_POST variable<\/h2>\n<p>The $_POST variable is, as $_SERVER, an array with keys. However, unlike $_SERVER, the keys are not predefined but rather created &#8220;on the fly&#8221; when the user presses the &#8220;Submit&#8221; button of a web form. After the user submits the data from a web form, the $_POST array gets populated with one key for each of the different fields that were present in the web form. We will go through these mechanisms in great detail on the next chapter. For now, let us just say that each field of a web form is associated with a &#8220;name&#8221; attribute. For example if a user submits a form that contains fields with name=&#8221;user&#8221; and name=&#8221;sequence&#8221;, the user and sequence keys will be generated in the $_POST array, and will point to the values given to these two fields within the web form. We can then, within the PHP script, collect these values:<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\/\/ Rather than visiting this script directly, the user is directed here after pressing the submit button of a web form\r\n\/\/ If you run this code example as such it will not work\r\n\r\n$user = $_POST['user'];\r\n$sequence = $_POST['sequence'];\r\n\r\necho \"<p>Welcome $user<\/p>\\n\";\r\necho \"<p>This is the sequence you sent us $sequence<\/p>\\n\";\r\n\r\n?>\r\n<\/code><\/pre>\n<p>So in short, $_POST is used by a PHP script to collect data from a submitted web form. This will become more clear on reading the next chapter.<\/p>\n<div class=\"google-ad\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- bioinfo web dev 2 --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: inline-block; width: 728px; height: 90px;\" data-ad-client=\"ca-pub-0159360445983090\" data-ad-slot=\"3442176918\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/div>\n<h2>Chapter Sections<\/h2>\n<p>[pagelist include=&#8221;435&#8243;]<\/p>\n<p>[siblings]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP provides access to a number of predefined functions and variables. For an exhaustive overview of the predefined variables available in the PHP programming language please check out the related section on php.net website, from which the following complete list was derived. A complete list of PHP predefined variables according to the php.net website Superglobals &hellip; <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-predefined-variables\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;4-4: PHP programming language basics &#8211; predefined variables&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":435,"menu_order":4,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-658","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/comments?post=658"}],"version-history":[{"count":32,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/658\/revisions"}],"predecessor-version":[{"id":2999,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/658\/revisions\/2999"}],"up":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/435"}],"wp:attachment":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/media?parent=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}