PHP Port Process Primer Tutorial

PHP Port Process Primer Tutorial

PHP Port Process Primer Tutorial

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.

One conduit to the hardware world, for me, then, is the concept of a port, which is, to quote Wikipedia …

In computer networking, a port is an endpoint of communication in an operating system.

… linking the software, via networking principals, to hardware. So if there is a way to find an underlying operating system PID (process identifier) associated with a designated port (number), then my ears prick up.

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 netstat -an but the lack of an “o” switch in Linux (or unix) is the reason this cannot be used … as “o” supplies a PID column to the result set. However Linux has alternatives with the lsof command, so the project will be undertaken by PHP using its ever useful exec method to …

  • Linux (or unix) …

    lsof -i:[portNumber]
  • Windows …

    netstat -ano | findstr ":[portNumber] PID"

… and so, that brings us to the question of how the PHP tells what operating system underlies it. We make use of this link‘s great advice (to make use of the pre-defined constant PHP_OS) to come up with (where $gp contains port number of interest) the PHP code snippet …

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// echo 'This is a server using Windows!';
exec('netstat -ano | find ":' . $gp . ' PID" > ppi.txt');
} else {
// echo 'This is a server not using Windows!';
exec('sh -c "lsof -i:' . $gp . '" > ppi.txt');

To look at the PHP source code, try this port_process_info.php or try a live run link. We worked with a Windows local Apache web server called EasyPHP ( for URL HTTP://localhost:8887/port_process_info.php ) and a Mac OS X local Apache web server called MAMP ( for URL HTTP://localhost:8888/port_process_info.php ) to test the PHP code today.

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

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

14 Responses to PHP Port Process Primer Tutorial

  1. Hey, you used to write great, but the last several posts have been kinda boring¡K I miss your tremendous writings. Past several posts are just a little out of track! come on!

  2. I intended to put you a very little observation to be able to say thanks yet again considering the amazing knowledge you have discussed on this page. It was quite wonderfully open-handed with you to deliver freely what exactly a lot of folks might have distributed as an electronic book to generate some dough on their own, precisely now that you could have done it in case you wanted. The basics as well worked as the easy way to recognize that other people have similar dreams just as mine to know the truth great deal more with regard to this matter. I believe there are some more fun sessions up front for those who take a look at your website.

  3. hello!,I like your writing so much! percentage we be in contact more about your article on AOL? I require a specialist in this area to solve my problem. May be that is you! Looking ahead to peer you.

  4. Lynette Nowack says:

    You have some great ideas.

  5. What your saying is totally correct. I understand that absolutely everyone need to have to say the very same point, but I just assume that you just location it inside a way that completely absolutely everyone can comprehend. I also actually such as the pictures you place within the following. They suit so nicely with what youre attempting to say. Im good youll obtain so rather several men and women with what youve obtained to say.

  6. I loved as much as you’ll receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this hike.

  7. you are in point of fact a excellent webmaster. The web site loading pace is amazing. It kind of feels that you are doing any unique trick. Furthermore, The contents are masterpiece. you have done a magnificent task on this subject!

  8. Fidel Powsey says:

    naturally like your web-site however you have to take a look at the spelling on quite a few of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the reality however I will certainly come back again.

  9. Donald Phou says:

    I think this is one of the most important information for me. And i’m glad reading your article. But wanna remark on some general things, The website style is great, the articles is really great : D. Good job, cheers

  10. Thanks a lot for sharing this with all people you really know what you are talking about!
    Bookmarked. Please additionally visit my web site =).
    We can have a link change agreement between us

  11. homepage says:

    Great beat ! I wish to apprentice whilst you amend your
    website, how could i subscribe for a weblog web site?

    The account helped me a appropriate deal. I had been tiny bit acquainted of this your
    broadcast provided vibrant clear idea

  12. Unblocked says:

    It’s an amazing piece of writing in favor of all the internet
    visitors; they will take benefit from it I am sure.

  13. solarmovie says:

    It’s hard to come by knowledgeable people on this
    subject, however, you sound like you know what you’re talking about!


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>