{"id":19338,"date":"2016-03-22T03:01:56","date_gmt":"2016-03-21T17:01:56","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=19338"},"modified":"2022-01-05T10:59:54","modified_gmt":"2022-01-05T00:59:54","slug":"php-port-process-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-port-process-primer-tutorial\/","title":{"rendered":"PHP Port Process Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/port_process_info.php\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"PHP Port Process Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/ppi_huh.jpg\" title=\"PHP Port Process Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">PHP Port Process Primer Tutorial<\/p><\/div>\n<p>When there is a concept that somehow links hardware to software things fall into place better for me, personally, with understanding.  For me, this is because hardware is that interesting thing out there that is more unknown, and so much more mysterious, to me, than software.<\/p>\n<p>One conduit to the hardware world, for me, then, is the concept of a <a target=_blank title='Port information from Wikipedia' href='https:\/\/en.wikipedia.org\/wiki\/Port_%28computer_networking%29'>port<\/a>, which is, to quote Wikipedia &#8230;<\/p>\n<blockquote><p>\nIn computer networking, a port is an endpoint of communication in an operating system.\n<\/p><\/blockquote>\n<p> &#8230; linking the software, via networking principals, to hardware.  So if there is a way to find an underlying operating system <a target=_blank title='PID information from Wikipedia' href='https:\/\/en.wikipedia.org\/wiki\/Process_identifier'>PID<\/a> (process identifier) associated with a designated port (number), then my ears prick up.<\/p>\n<p>What initially puts you off such an endeavour is that there is no way the Windows command line would have the same command as the Linux or unix command line to do this?   Is there?  Well, actually, there almost is, with <i>netstat -an<\/i> but the lack of an &#8220;o&#8221; switch in Linux (or unix) is the reason this cannot be used &#8230; as &#8220;o&#8221; supplies a PID column to the result set.  However Linux has alternatives with the <a target=_blank title='Linux lsof command' href='http:\/\/linux.die.net\/man\/8\/lsof'>lsof<\/a> command, so the project will be undertaken by PHP using its ever useful <a target=_blank title='PHP exec method information' href='http:\/\/php.net\/manual\/en\/function.exec.php'>exec<\/a> method to &#8230;<\/p>\n<ul>\n<li>Linux (or unix) &#8230;<br \/>\n<code><br \/>\nlsof -i:[portNumber]<br \/>\n<\/code><\/li>\n<li>Windows &#8230;<br \/>\n<code><br \/>\nnetstat -ano | findstr \":[portNumber] PID\"<br \/>\n<\/code><\/li>\n<\/ul>\n<p> &#8230; and so, that brings us to the question of how the PHP tells what operating system underlies it.  We make use of <a target=_blank title='Usefil link' href='http:\/\/stackoverflow.com\/questions\/1482260\/how-to-get-the-os-on-which-php-is-running'>this link<\/a>&#8216;s great advice (to make use of the pre-defined constant <a target=_blank title='PHP_OS pre-defined constant' href='http:\/\/php.net\/manual\/en\/reserved.constants.php'>PHP_OS<\/a>) to come up with (where $gp contains port number of interest) the PHP code snippet &#8230;<\/p>\n<p><code><br \/>\nif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {<br \/>\n    \/\/ echo 'This is a server using Windows!';<br \/>\n    exec('netstat -ano | find \":' . $gp . ' PID\" &gt; ppi.txt');<br \/>\n} else {<br \/>\n    \/\/ echo 'This is a server not using Windows!';<br \/>\n    exec('sh -c \"lsof -i:' . $gp . '\" &gt; ppi.txt');<br \/>\n}<br \/>\n<\/code><\/p>\n<p>To look at the PHP source code, try this <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/port_process_info.php_GETME\" title='port_process_info.php'>port_process_info.php<\/a> or try a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/port_process_info.php\" title='Live run'>live run<\/a> link.  We worked with a Windows local Apache web server called <a target=_blank title='EasyPHP landing page' href='http:\/\/www.easyphp.org\/'>EasyPHP<\/a> ( for URL http:\/\/localhost:8887\/port_process_info.php ) and a Mac OS X local Apache web server called <a target=_blank title='MAMP landing page' href='http:\/\/mamp.info\/'>MAMP<\/a> ( for URL http:\/\/localhost:8888\/port_process_info.php ) to test the PHP code today.<\/p>\n<p><b><i>Stop Press<\/i><\/b><\/p>\n<p>The last <a target=_blank title='MAMP landing page' href='http:\/\/mamp.info\/'>MAMP<\/a> for Windows install we did, in 2021, ended up with HTTP:\/\/localhost\/ being the resultant local Apache\/MySql\/PHP&#8217;s $_SERVER[&#8216;DOCUMENT_ROOT&#8217;] C:\\MAMP\\ disk directory URL (part).<\/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='#d19338' onclick='var dv=document.getElementById(\"d19338\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/hardware\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d19338' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When there is a concept that somehow links hardware to software things fall into place better for me, personally, with understanding. For me, this is because hardware is that interesting thing out there that is more unknown, and so much &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-port-process-primer-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,18,25,29,33,37],"tags":[234,372,405,555,707,714,744,835,885,932,1772,997,1166,1200,1771,1339,1435],"class_list":["post-19338","post","type-post","status-publish","format-standard","hentry","category-elearning","category-hardware","category-networking","category-operating-system","category-software","category-tutorials","tag-command-line","tag-easyphp","tag-exec","tag-hardware-2","tag-linux","tag-local-web-server","tag-mamp","tag-network","tag-operating-system-2","tag-php","tag-port","tag-programming","tag-software-2","tag-stop-press","tag-tutirial","tag-unix","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/19338"}],"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=19338"}],"version-history":[{"count":6,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/19338\/revisions"}],"predecessor-version":[{"id":54525,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/19338\/revisions\/54525"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=19338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=19338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=19338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}