PHP - Reading data from magnetic card

Lucifer picture Lucifer · Oct 8, 2012 · Viewed 9k times · Source

I have to make a application where card reader will be attached to system and I have to read magnetic card reader data if someone swap card on card reader attached.

Magnetic card basically holds user details for registration. So user will give card on reception. receptionist will swap card and system will read data from card and register that user in my party management application.

No I idea how to do this? Is it possible with Php ?

Please share idea how to do this.

Answer

LSerni picture LSerni · Oct 8, 2012

I have to make a application where card reader will be attached to system and I have to read magnetic card reader data if someone swap card on card reader attached.

Since you want to do this in PHP, I assume this is a web application and that the card is swiped at the client machine where the browser is running.

You may have a keyboard-emulation swiper (you usually get a text such as '123456'), in which case you need to intercept the keyboard/focus and upload the data as form field or AJAX. This can be tricky if the field or the browser lose the focus, and you'd need to run the browser in "kiosk mode".

Or you can have a swiper using other technologies such as serial (RS232[C]), which I believe is quite obsolete, or custom interface (which means normally an USB swiper and a set of API). If this is the case, it all depends on what API are implemented; some devices come with an ActiveX applet which will force you to use Internet Explorer technologies on Windows platform. Some others use Java applets, and you will have to integrate them in the Web page.

Finally some of them have a completely custom application which may interface with the system in several ways (the case "custom LIBRARY" is the same, except that you have to also develop the custom application yourself!):

  • By sending a custom HTTP query to a programmable server. If this is the case you're lucky; you can program a query for each terminal, e.g. "http://yourserver/gotcard?terminal=LODGE&card=$$CODE$$". The custom application will lurk in the background, grab the swiped code, send it to the server; and you shall poll the server to know when each terminal has received a code. You'll need to handle spurious and duplicate codes. Such applications come with several 'placeholders' such as $$CODE$$, $$TIMESTAMP$$, and sometimes $$SWIPEDIRECTION$$; they're used for personnel timetracking (swipe left to right when entering work, right to left when going home).
  • By uploading the code into a MySQL or SQL Server, again with programmable placeholders. Here you'll be perhaps forced to use a VPN/SSH connection to be able to intercept the query, and the DB drivers supplied might limit your architectural choices.
  • By running a custom application or script with the usual placeholders. In this case your best options is, IMHO, to write a script calling lynx (either Linux or Windows) and dovetail into case #1, "custom HTTP query".

Unfortunately the various suppliers tend to either the "hacky" side (i.e., they supply you the hardware and a minimum of software, a library and a sample MFC app or source code, and you're on your own) or on the "everything included and nonnegotiable" side (i.e., you get your reader with a fully configured POS or employee timelogger app, and no easy way of doing anything else with it).

If you're running server side, i.e., PHP script (not its HTML/text output) and badge reader are on the same machine, then again it depends on what software you were supplied with. For servers, keyboard emulation are either the best (if you have a headless server with no reason to ever connect a keyboard) or asking for trouble (if the magnetic reader and the real keyboard find themselves in competition). RS232[C] are also very simple to use, in Linux you can ttytail or maybe even tail -f the device on a log file and poll the log file. YOU MAY HAVE PRIVACY ISSUES WITH THAT (think "recording all customers' credit card numbers in plain text"). Or you can attach the device in PHP with fopen(), no trouble there.

Custom-API libraries might be tricky. You might have to resort to the trick of creating a socket or named pipe "server" which opens the device and makes its results available to PHP through fsockopen(). Unix sockets and client/server programming knowledge required.

UPDATE

I saw in a comment that you have the library for the barcode reader. I still assume you're in a web app scenario (say, three POS with readers, one server).

A way of doing this could be:

// PHP "server" application running as CLI on POS #1
// (will read some data from an INI file)

* application initialization

for(;;)
{
    /* Hypothesis 1: blocking library */
    $code = read_barcode();

    /* Hypothesis 2: nonblocking, but buffering library */
    if (!barcode_available())
    {
        sleep(1);
        continue;
    }
    /* Hypothesis 3 is left as an exercise ;-) - remember to reinit the buffer */


    if (!checks_if_valid_barcode($code))
       continue;

    // Unless there's some reason of using cURL, we keep it simple
    // We might want to use mcrypt() function to encrypt the code, though.
    $response = file_get_contents(
         $CONFIG['server_url'].'?terminal='.$CONFIG['terminal']
         .'&code='.$code
    );
    // Do something with the response - maybe just an OK or an ERROR
    // (e.g. play a sound, just for kicks).
    // The server, which generates the error, knows all about the error itself
    // and the webapp will poll details from the server, not from this app.
}

The web application will query the server at intervals; we have two paralle workflows:

  • (1) AJAX checks for swipe
  • (2) CLI listens for a swipe
  • (1) Server responds 'none yet'
  • (2) CLI app sends a swipe
  • (1) AJAX checks for swipe
  • (1) Server responds 'none yet'
  • (2) Server validates the swipe
  • (1) AJAX checks for swipe
  • (1) Server responds 'I got it'
  • (2) goes back to listening
  • (1) client displays form and starts processing
  • ...