PHP Modes of Use Follow Up Tutorial

PHP Modes of Use Follow Up Tutorial

PHP Modes of Use Follow Up Tutorial

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 orange and updated ideas to help “shore up” the ideas in green. It is probably best known as a server-side language called by a web browser using the http transport layer (ie. web browsing, or “surfing the net”). Let’s list three modes of use of PHP below:

  1. Via http transport layer (ie. web browsing, or “surfing the net”) in an Internet mode of use (eg: http://localhost:8888/more_in_tra_ter_net.php?SERVER_NAME=localhost) in address bar of a web browser
  2. Via curl 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 > outfile or >> outfile or 2> errfile or 2>> errfile)
  3. Via command line PHP in Linux or unix or Windows command line in a command or Intranet 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 > outfile or >> outfile or 2> errfile or 2>> errfile)

Does it matter how the PHP is accessed? Well, yes, it does … 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 $_SERVER[] variables are undefined, for example.

  1. $_SERVER[‘HTTP_COOKIE’] is defined, unlike with curl usage (this seems to be a possibility as a way to detect curl usage). (see green bits below, because $_SERVER[‘HTTP_COOKIE’] 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). Most $_SERVER[] variables are defined, but $argv[] array is not.
  2. $_SERVER[‘HTTP_COOKIE’] is undefined, … 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) … 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[‘HTTP_COOKIE’] 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) … in any case, another approach can be to examine $_SERVER[‘HTTP_USER_AGENT’] for the existence of the word “curl” at its beginning to test for the curl mode of use … again, this can be an effective test from a HTTP friendly client, but this may not be a perfect test or solution because $_SERVER[‘HTTP_USER_AGENT’] can be manipulated (ref: link), but a lot of the rest of $_SERVER[] variables are as above.
  3. Lots of $_SERVER[] variables are undefined, with the exception of $_SERVER[‘SCRIPT_FILENAME’] 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[‘QUERY_STRING’] (ie. the bits after ? in a URL).

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.

Hope you get something out of today’s discussion of our Mac OS X MAMP Apache local Web server environment example (hence the http://localhost:8888/ 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 “web browsing” of above) or other possibilities within PHP code itself via PHP commands exec and file_get_contents the latter of which can be very useful during Intranet usage where there is no curl available, perhaps. Instead of curl you could use wget as another approach.

You may want to see a live run (web browsing) here at the rjmprogramming.com.au domain or you may want to download the PHP programming source code you could call more_in_tra_ter_net.php as you require.


Previous relevant PHP Modes of Use Primer Tutorial is shown below.

PHP Modes of Use Primer Tutorial

PHP Modes of Use Primer Tutorial

PHP 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 “surfing the net”). Let’s list three modes of use of PHP below:

  1. Via http transport layer (ie. web browsing, or “surfing the net”) in an Internet mode of use (eg: http://localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) in address bar of a web browser
  2. Via curl 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 > outfile or >> outfile or 2> errfile or 2>> errfile)
  3. Via command line PHP in Linux or unix or Windows command line in a command or Intranet 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 > outfile or >> outfile or 2> errfile or 2>> errfile)

Does it matter how the PHP is accessed? Well, yes, it does … 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 $_SERVER[] variables are undefined, for example.

  1. $_SERVER[‘HTTP_COOKIE’] 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.
  2. $_SERVER[‘HTTP_COOKIE’] is undefined, but a lot of the rest of $_SERVER[] variables are as above.
  3. Lots of $_SERVER[] variables are undefined, with the exception of $_SERVER[‘SCRIPT_FILENAME’] 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[‘QUERY_STRING’] (ie. the bits after ? in a URL).

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.

Hope you get something out of today’s discussion of our Mac OS X MAMP Apache local Web server environment example (hence the http://localhost:8888/ 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 “web browsing” of above) or other possibilities within PHP code itself via PHP commands exec and file_get_contents the latter of which can be very useful during Intranet usage where there is no curl available, perhaps. Instead of curl you could use wget as another approach.

You may want to see a live run (web browsing) here at the rjmprogramming.com.au domain or you may want to download the PHP programming source code you could call in_tra_ter_net.php as you require.

If this was interesting you may be interested in this too.

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *