PHP
  Home arrow PHP arrow Page 7 - Socket Programming With PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Socket Programming With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 123
    2002-02-05

    Table of Contents:
  • Socket Programming With PHP
  • Putting It All Together
  • Fortune's Fool
  • Looping The Loop
  • On Web-bed Feet
  • Different Strokes
  • POP Goes The Weasel
  • Access Denied
  • Game Over

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Socket Programming With PHP - POP Goes The Weasel


    (Page 7 of 9 )

    Let's try something a little more advanced. How about a TCP client that connects to a POP3 server to retrieve the total number of messages for a user's mailbox?

    In order to build such a client, I need to first understand the sequence of commands passed to a POP3 server in order to obtain the message total, and then replicate this sequence in my PHP-based client. The best way to do this is by using a regular telnet client to interact with the server and understand the command sequence - so here goes:

    $ telnet mail.host 110 Trying 192.168.0.1... Connected to 192.168.0.1. Escape character is '^]'. +OK POP3 mail.host v5.5 server ready USER john +OK User name accepted, password please PASS doe +OK Mailbox open, 72 messages STAT +OK 72 24595628 QUIT +OK Sayonara Connection closed by foreign host.
    As you can see from the sample session above, the second element of the string returned by a STAT command

    STAT +OK 72 24595628
    holds the total number of messages (72, in this case). All that's needed, therefore, is a script that connects to the POP3 server (usually available on port 110), sends the sequence of commands above, retrieves the output of the STAT command, and extracts the message total from it.

    Here's the script to accomplish this:

    <? // mail server settings $host="192.168.0.99"; $port = 110; $user = "john"; $pass = "doe"; // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); // if a handle is not returned if (!$fp) { die("Error: could not open socket connection\n"); } else { // get the welcome message $welcome = fgets ($fp, 150); // check for success code if (substr($welcome, 0, 3) == "+OK") { // send username and read response fputs ($fp, "USER $user\n"); fgets($fp, 50); // send password and read response fputs ($fp, "PASS $pass\n"); $ack = fgets($fp, 50); // check for success code if (substr($ack, 0, 3) == "+OK") { // send status request and read response fputs ($fp, "STAT\n"); $status = fgets($fp, 50); if (substr($status, 0, 3) == "+OK") { // shut down connection fputs ($fp, "QUIT\n"); fclose ($fp); } // error getting status else { die ("Server said: $status"); } } // auth failure else { die ("Server said: $ack"); } } // bad welcome message else { die ("Bad connection string\n"); } // get status string // split by spaces $arr = explode(" ", $status); // the second element contains the total number of messages echo $arr[1] . " messages in mailbox"; } ?>
    And here's the output:

    $ /usr/local/bin/php -q popclient.php 72 messages in mailbox
    How does this work? Very simple.

    First, a connection is opened to the POP3 server using the fsockopen() function discussed previously; the arguments to this function (host, port et al) are obtained through PHP variables which are set at the top of the script.

    <? // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); ?>
    Once a socket connection has been established, the fgets() and fputs() functions are used to send POP3 commands to the server via this socket, and read the resulting output into PHP variables.

    <? // send username and read response fputs ($fp, "USER $user\n"); fgets($fp, 50); // send password and read response fputs ($fp, "PASS $pass\n"); $ack = fgets($fp, 50); ?>
    A POP3 server typically prefixes the result of every successful command with the string "+OK". This knowledge allows for simple error-checking within the script - note how the output of fgets() is checked at every stage, with subsequent commands executed only if the previous one was successful.

    <? // send status request and read response fputs ($fp, "STAT\n"); $status = fgets($fp, 50); if (substr($status, 0, 3) == "+OK") { // shut down connection fputs ($fp, "QUIT\n"); fclose ($fp); } // error getting status else { die ("Server said: $status"); } ?>
    Once the output of the STAT command has been received, the socket is closed, and the result string is split into its constituent parts with PHP's very cool explode() function. The message total is then extracted and printed.

    <? // get status string // split by spaces $arr = explode(" ", $status); // the second element contains the total number of messages echo $arr[1] . " messages in mailbox"; ?>
    Simple, huh?

    More PHP Articles
    More By icarus, (c) Melonfire


       · This information is very help for understanding the sockets in php
       · I was able to run the first sample on Linux, I cannot connect to server using MS...
       · I thank you for having writing this.Very simple.Good explanations.Perhaps...
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT