{"id":9586,"date":"2014-09-14T05:09:28","date_gmt":"2014-09-13T19:09:28","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=9586"},"modified":"2014-09-14T05:09:28","modified_gmt":"2014-09-13T19:09:28","slug":"php-modes-of-use-follow-up-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-modes-of-use-follow-up-tutorial\/","title":{"rendered":"PHP Modes of Use Follow Up Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/more_http_vs_curl_vs_php_command_line.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"PHP Modes of Use Follow Up Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/http_vs_curl_vs_php_command_line.jpg\" title=\"PHP Modes of Use Follow Up Tutorial\"  \/><\/a><p class=\"wp-caption-text\">PHP Modes of Use Follow Up Tutorial<\/p><\/div>\n<p><a target=_blank title='PHP information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/PHP'>PHP<\/a> is a very flexible programming language, as we saw during the previous <a target=_blank href='#pmoupt' title='PHP Modes of Use Primer Tutorial'>PHP Modes of Use Primer Tutorial<\/a> as shown below, where, below, we quote from this blog post largely, and highlight ideas that do not always work in <font color='orange'>orange<\/font> and updated ideas to help &#8220;shore up&#8221; the ideas in <font color='green'>green<\/font>.  It is probably best known as a server-side language called by a web browser using the http transport layer (ie. web browsing, or &#8220;surfing the net&#8221;).  Let&#8217;s list three modes of use of PHP below:<\/p>\n<ol>\n<li>Via http transport layer (ie. web browsing, or &#8220;surfing the net&#8221;) in an <a target=_blank title='Internet information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Internet'>Internet<\/a> mode of use (eg: http:\/\/localhost:8888\/more_in_tra_ter_net.php?SERVER_NAME=localhost) in address bar of a web browser<\/li>\n<li style='color:blue;'>Via <a target=_blank title='PHP information from Wikipedia' href='http:\/\/en.wikipedia.org\/wiki\/CURL'>curl<\/a> in an Internet mode of use (eg. curl http:\/\/localhost:8888\/more_in_tra_ter_net.php?SERVER_NAME=localhost) at Linux or unix or Windows command line to default output (and you can redirect as required via &gt; <i>outfile<\/i> or &gt;&gt; <i>outfile<\/i> or 2&gt; <i>errfile<\/i> or 2&gt;&gt; <i>errfile<\/i>)<\/li>\n<li style='color:brown;'>Via command line PHP in Linux or unix or Windows command line in a command or <a target=_blank title='Intranet information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Intranet'>Intranet<\/a> mode of use (eg. php more_in_tra_ter_net.php SERVER_NAME=localhost) at a Linux or unix or Windows command line (where the PHP code has been placed, in our example (ie. it is not a URL, but is a file specification of your (Intranet) server)) to default output (or you can redirect as required via &gt; <i>outfile<\/i> or &gt;&gt; <i>outfile<\/i> or 2&gt; <i>errfile<\/i> or 2&gt;&gt; <i>errfile<\/i>)<\/li>\n<\/ol>\n<p>Does it matter how the PHP is accessed?  Well, yes, it does &#8230; sometimes, but not if the code is restricted to a particular mode of usage (but part of what this tutorial is about is to get across how flexible and powerful PHP can be).  With the last option above, lots of <a target=_blank title='PHP $_SERVER[] variables' href='http:\/\/au2.php.net\/reserved.variables.server.php'>$_SERVER[]<\/a> variables are undefined, for example.<\/p>\n<ol>\n<li><font color='orange'>$_SERVER[&#8216;HTTP_COOKIE&#8217;] is defined, unlike with curl usage (this seems to be a possibility as a way to detect curl usage).<\/font><font color='green'> (see green bits below, because $_SERVER[&#8216;HTTP_COOKIE&#8217;] became undefined on MAMP by the passing of time between the two tutorials, so we looked to find another way to tell the difference between web surfing mode and curl mode of PHP usage\/access).<\/font>  Most $_SERVER[] variables are defined, but $argv[] array is not.<\/li>\n<li style='color:blue;'>$_SERVER[&#8216;HTTP_COOKIE&#8217;] is undefined, <font color='green'> &#8230; as a follow up idea here today, brought on by the fact that here with the local MAMP server we are testing on, as with the old live link from the rjmprogramming.com.au web server (sometimes (though not just this minute of writing) &#8230; clearly, with all this you may need more of your own research into the behaviour of the PHP $_SERVER[] array, as per links below) in tutorial below $_SERVER[&#8216;HTTP_COOKIE&#8217;] can become undefined for both web surfing and curl modes of use (and so becomes a less than effective means of detecting curl supervision, in the PHP code) over time (am not aware, exactly when this happened) &#8230; in any case, another approach can be to examine $_SERVER[&#8216;HTTP_USER_AGENT&#8217;] for the existence of the word <i>&#8220;curl&#8221;<\/i> at its beginning to test for the <i>curl<\/i> mode of use &#8230; again, this can be an effective test from a HTTP friendly client, but this may not be a perfect test or solution because $_SERVER[&#8216;HTTP_USER_AGENT&#8217;] can be manipulated (ref: <a target=_blank title='Ideas here ... thanks' href='http:\/\/stackoverflow.com\/questions\/6577645\/how-to-know-the-url-that-is-calling-the-destination-url-using-curl-in-the-destin'>link<\/a>)<\/font>, but a lot of the rest of $_SERVER[] variables are as above.<\/li>\n<li style='color:brown;'>Lots of $_SERVER[] variables are undefined, with the exception of $_SERVER[&#8216;SCRIPT_FILENAME&#8217;] which may help you with Intranet application usage.  You cannot enter $_GET[] arguments (on the command line (ie. you will get a syntax error if you try)) so these are undefined, but $argv[] array can be used in that awkward way shown, so that $argv[1] can be arranged to be a lot like $_SERVER[&#8216;QUERY_STRING&#8217;] (ie. the bits after ? in a URL).<\/li>\n<\/ol>\n<p>So, maybe you have the one mode of use in mind, but you need to think a bit to support all the modes of use, if they are all to have a role with the PHP you write, because, despite these differences it is good to use $_SERVER[] and $_GET[] and $_POST[] and $_SESSION[] and $argv[] and whatever else in terms of variable information PHP offers in terms of attempting to write generic code.<\/p>\n<p>Hope you get something out of today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/more_http_vs_curl_vs_php_command_line.jpg\" title='Click picture'>discussion<\/a> of our Mac OS X MAMP Apache local Web server environment example (hence the <i>http:\/\/localhost:8888\/<\/i> you see in the URLs above), but please bear in mind that there would be other modes of use once you think of other client HTML element ways of accessing PHP (all a lot like the &#8220;web browsing&#8221; of above) or other possibilities within PHP code itself via PHP commands <a target=_blank title='PHP exec' href='http:\/\/php.net\/manual\/en\/function.exec.php'><i>exec<\/i><\/a> and <a target=_blank title='PHP file_get_contents' href='http:\/\/php.net\/manual\/en\/function.file-get-contents.php'><i>file_get_contents<\/i><\/a> the latter of which can be very useful during Intranet usage where there is no <i>curl<\/i> available, perhaps.  Instead of curl you could use <a target=_blank title='wget' href='http:\/\/www.gnu.org\/s\/wget\/'>wget<\/a> as another approach.<\/p>\n<p>You may want to see a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/more_in_tra_ter_net.php\" title='Live Run'>live run (web browsing)<\/a> here at the rjmprogramming.com.au domain or you may want to download the PHP programming source code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/more_in_tra_ter_net.php_GETME\" title='more_in_tra_ter_net.php'>more_in_tra_ter_net.php<\/a> as you require.<\/p>\n<hr \/>\n<p id='pmoupt'>Previous relevant <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=9356' title='PHP Modes of Use Primer Tutorial'>PHP Modes of Use Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/http_vs_curl_vs_php_command_line.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"PHP Modes of Use Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/http_vs_curl_vs_php_command_line.jpg\" title=\"PHP Modes of Use Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">PHP Modes of Use Primer Tutorial<\/p><\/div>\n<p><a target=_blank title='PHP information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/PHP'>PHP<\/a> is a very flexible programming language.  It is probably best known as a server-side language called by a web browser using the http transport layer (ie. web browsing, or &#8220;surfing the net&#8221;).  Let&#8217;s list three modes of use of PHP below:<\/p>\n<ol>\n<li>Via http transport layer (ie. web browsing, or &#8220;surfing the net&#8221;) in an <a target=_blank title='Internet information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Internet'>Internet<\/a> mode of use (eg: http:\/\/localhost:8888\/in_tra_ter_net.php?SERVER_NAME=localhost) in address bar of a web browser<\/li>\n<li style='color:blue;'>Via <a target=_blank title='PHP information from Wikipedia' href='http:\/\/en.wikipedia.org\/wiki\/CURL'>curl<\/a> in an Internet mode of use (eg. curl http:\/\/localhost:8888\/in_tra_ter_net.php?SERVER_NAME=localhost) at Linux or unix or Windows command line to default output (and you can redirect as required via &gt; <i>outfile<\/i> or &gt;&gt; <i>outfile<\/i> or 2&gt; <i>errfile<\/i> or 2&gt;&gt; <i>errfile<\/i>)<\/li>\n<li style='color:brown;'>Via command line PHP in Linux or unix or Windows command line in a command or <a target=_blank title='Intranet information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Intranet'>Intranet<\/a> mode of use (eg. php in_tra_ter_net.php SERVER_NAME=localhost) at a Linux or unix or Windows command line (where the PHP code has been placed, in our example (ie. it is not a URL, but is a file specification of your (Intranet) server)) to default output (or you can redirect as required via &gt; <i>outfile<\/i> or &gt;&gt; <i>outfile<\/i> or 2&gt; <i>errfile<\/i> or 2&gt;&gt; <i>errfile<\/i>)<\/li>\n<\/ol>\n<p>Does it matter how the PHP is accessed?  Well, yes, it does &#8230; sometimes, but not if the code is restricted to a particular mode of usage (but part of what this tutorial is about is to get across how flexible and powerful PHP can be).  With the last option above, lots of <a target=_blank title='PHP $_SERVER[] variables' href='http:\/\/au2.php.net\/reserved.variables.server.php'>$_SERVER[]<\/a> variables are undefined, for example.<\/p>\n<ol>\n<li>$_SERVER[&#8216;HTTP_COOKIE&#8217;] is defined, unlike with curl usage (this seems to be a possibility as a way to detect curl usage).  Most $_SERVER[] variables are defined, but $argv[] array is not.<\/li>\n<li style='color:blue;'>$_SERVER[&#8216;HTTP_COOKIE&#8217;] is undefined, but a lot of the rest of $_SERVER[] variables are as above.<\/li>\n<li style='color:brown;'>Lots of $_SERVER[] variables are undefined, with the exception of $_SERVER[&#8216;SCRIPT_FILENAME&#8217;] which may help you with Intranet application usage.  You cannot enter $_GET[] arguments (on the command line (ie. you will get a syntax error if you try)) so these are undefined, but $argv[] array can be used in that awkward way shown, so that $argv[1] can be arranged to be a lot like $_SERVER[&#8216;QUERY_STRING&#8217;] (ie. the bits after ? in a URL).<\/li>\n<\/ol>\n<p>So, maybe you have the one mode of use in mind, but you need to think a bit to support all the modes of use, if they are all to have a role with the PHP you write, because, despite these differences it is good to use $_SERVER[] and $_GET[] and $_POST[] and $_SESSION[] and $argv[] and whatever else in terms of variable information PHP offers in terms of attempting to write generic code.<\/p>\n<p>Hope you get something out of today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/http_vs_curl_vs_php_command_line.jpg\" title='Click picture'>discussion<\/a> of our Mac OS X MAMP Apache local Web server environment example (hence the <i>http:\/\/localhost:8888\/<\/i> you see in the URLs above), but please bear in mind that there would be other modes of use once you think of other client HTML element ways of accessing PHP (all a lot like the &#8220;web browsing&#8221; of above) or other possibilities within PHP code itself via PHP commands <a target=_blank title='PHP exec' href='http:\/\/php.net\/manual\/en\/function.exec.php'><i>exec<\/i><\/a> and <a target=_blank title='PHP file_get_contents' href='http:\/\/php.net\/manual\/en\/function.file-get-contents.php'><i>file_get_contents<\/i><\/a> the latter of which can be very useful during Intranet usage where there is no <i>curl<\/i> available, perhaps.  Instead of curl you could use <a target=_blank title='wget' href='http:\/\/www.gnu.org\/s\/wget\/'>wget<\/a> as another approach.<\/p>\n<p>You may want to see a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/in_tra_ter_net.php\" title='Live Run'>live run (web browsing)<\/a> here at the rjmprogramming.com.au domain or you may want to download the PHP programming source code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/in_tra_ter_net.php_GETME\" title='in_tra_ter_net.php'>in_tra_ter_net.php<\/a> as you require.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d9356' onclick='var dv=document.getElementById(\"d9356\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=PHP\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d9356' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<p><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d9586' onclick='var dv=document.getElementById(\"d9586\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=clientserver\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d9586' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>PHP is a very flexible programming language, as we saw during the previous PHP Modes of Use Primer Tutorial as shown below, where, below, we quote from this blog post largely, and highlight ideas that do not always work in &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-modes-of-use-follow-up-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,37],"tags":[213,234,284,581,621,624,707,725,744,932,997,1319,1424],"class_list":["post-9586","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-clientserver","tag-command-line","tag-curl","tag-http","tag-internet","tag-intranet","tag-linux","tag-mac-os-x","tag-mamp","tag-php","tag-programming","tag-tutorial","tag-wget"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/9586"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=9586"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/9586\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=9586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=9586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=9586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}