PHP : Script to count with barcode reader.

Well these are boring times… I just wrote a script using php to count barcodes with a barcode reader. Though this might be a nice thing to share, maybe someone will find it usefull.

It doesnt use a database, so at some point you`ll need to copy paste the results somewhere. Ow well have funūüėÄ

 
session_start();

if(isset($_GET['barcode']) || isset($_POST['barcode'])){
 $barcode = (isset($_POST['barcode'])) ? $_POST['barcode'] : $_GET['barcode'];
}else{
 $barcode = false;
}

if($barcode){
 if(@$_SESSION['products'][$barcode]){
  $_SESSION['products'][$barcode] ++;
 }else{
  $_SESSION['products'][$barcode] = 1;
 }
}

if(is_array($_SESSION['products'])){
 foreach($_SESSION['products'] as $barcode => $count){
  echo " Counted $barcode, $count times allready!<br\>\r\n";
 }
}

Print('
  <html>
  <head>;
  <script type="text/javascript">;
  var formInUse = false;
  function setFocus(){ if(!formInUse) {document.BarForm.barcode.focus();}}
  </script>;
  </head>;
  <body onload="setFocus()">
  <form name="BarForm" action="'.$_SERVER['PHP_SELF'].'" method="GET">;
  <table>
   <tr>
    <td><input type="text" name="barcode" onchange="form.submit()" onfocus="formInUse = true;"></td>
   </tr>
  </table>
  </body>;
  </html>;
');

About Chris Gralike

Momenteel ben ik manager van de afdeling business continuity bij de zakelijke IT dienstverlener AMIS Services BV. Sinds 2003 ben ik actief in de ICT branche. Tussen 2003 en nu heb ik verschillende rollen vervuld. In de rollen: systeem- en netwerkbeheer, system engineer, servicemanager en nu practice manager ben ik in contact gekomen met uiteenlopende technologie√ęn, methodologie√ęn, idee√ęn, oplossingen en innovaties. Een rijke ervaring waarmee ik de klanten van Conclusion en AMIS elke dag probeer te ondersteunen. Mijn credo: 'Altijd opzoek een win-win tussen business en technologie.'

Posted on December 24, 2008, in Apache 2.2 Windows, Linux, PHP and tagged , , , , , , , , , , . Bookmark the permalink. 11 Comments.

  1. or in a class form.

    $counter = new BarCodeCounter;

    class BarCodeCounter{

    private $Post;
    private $Get;

    function __construct(){
    session_start();
    FetchPostGet();
    $this->addCountValue(($barcode = ($this->Post[‘barcode’]) ? $this->Post[‘barcode’] : $this->Get[‘barcode’]));
    $this->showCounts();
    $this->showForm();
    }

    function __destruct(){
    }

    private function showCounts(){
    if(is_array($_SESSION[‘products’])){
    foreach($_SESSION[‘products’] as $barcode => $count){
    if(!empty($barcode)){
    echo “$barcode is al $count x geteld.\r\n”;
    }
    }
    }
    }

    private function addCountValue($value){
    if(!empty($_SESSION[‘products’][$value])){
    $_SESSION[‘products’][$value] ++;
    }else{
    $_SESSION[‘products’][$value] = 1;
    }
    }

    private function FetchPostGet(){
    foreach($_POST as $key => $value){
    $this->Post[$this->makeSave($key)] = $this->makeSave($value);
    }
    foreach ($_GET as $key => $value){
    $this->Get[$this->makeSave($key)] = $this->makeSave($value);
    }
    return true;
    }

    private function makeSave($value){
    if(!empty($value)){
    return addslashes($value);
    }else{
    return null;
    }
    }

    private function showForm(){
    Print(‘{form HTML HERE}’);
    }

    }

  2. hello Chris Gralike ,
    This is a nice stuff you posted. I have a query regarding this.
    I want to ask as per my requirement I want to use barcode reader to fill login form. Will this script work?
    I used this script but uploading this getting a Parse error: parse error, expecting `’)”.
    the regarding line is foreach($_SESSION[‘products’] as $barcode => $count){ …..
    do you have any idea about this??

    • Concerining the error, the sourcecode highlighter seems to have broken the script replacing some of the code with entities…:/

      Also try to place the $_SESSION[‘key’] inside curly opperators like so.

      foreach({$_SESSION[‘key’]} as $key => $val){
      }

      Concerning your original question,

      A barcode reader is simply an input device simular to your keyboard. Each time a barcode reader is used to read a supported barcode it will simply pass the string followed by an break, the same way you would type it on a keyboard and confirming it using a enter stroke.

      When you like to use a barcode reader on your loginform, you only need to be sure the website focus is on the correct field(s). The only problem might be how to handle the break that is passed by the reader. I think you should be able to tweak your code using Javascript to do this.

      Using something like this.

       function putFocus(formInst, elementInst) {
          if (document.forms.length &gt; 0) {
            document.forms[formInst].elements[elementInst].focus();
          }
        }
      
  3. Hello Chris Gralike,

    Thanks for your kind reply and the useful information. I am using a barcode reader for the following project to login employees. I am new to php and javascripts can you help me how can I modify the source code to get my requirement fix.
    I have some barcode generation library files I am sure these can fix my requirement at some level but don’t know how to implement it.If you can help me I’ll be grateful to you.

    http://sourceforge.net/projects/orangehrm/
    Library files:- http://www.codeproject.com/KB/graphics/BarcodeLibrary.aspx

    • Hi NIKHIL,

      I am sorry to say that I do not have all the time om my hands needed to instruct you on how to accomplish this. Reading up, testing, and coding yourself is usually the way to go.

      I also am convinced that the Library files posted might help you generate the required Barcodes. But for testing I would advice to simply use a Barcode font that works with your reader.

      Concerning modification of the HRM tool listed, I would ask the local community to help you. They are bound to have much more knowledge about the specifics then I do at this point.

      Hope this helps,
      Rgrds, Chris.

      • Thank you very much Chris for your reply I can understand your feelings.
        I am working myself at this project. I was doubtful regarding these files so I preferred to ask you.
        In future I’ll contact you for any further help.
        Thanks again !!

  4. Totally new to PHP here but this looks like something I could use. I have a USB barcode scanner, do I just install PHP on my windows machine and run this script and scan away? what type of table are the files temporarily stored in?

    • Hi Ben,

      I will try to answer your questions as good as I can.

      # Do i just install PHP ?

      To use this script you should indeed install PHP. The catch is that PHP (Pre Hypertext Processor) in this specific case needs an apache or IIS webserver to run.

      If you have no experience installing / configuring Apache and PHP i would recommend to download and install an so called “(L)AMP package” that will do all these steps for you.
      Example AMP :
      http://www.apachefriends.org/en/xampp-windows.html

      # Do i just run this script and scan away?

      You run the script by opening the file using a webbrowser. As stated before the file “must” be served using and webserver like apache with the PHP core installed. Next yes, you should be able to scan away (top post script)

      Simply save the code in a file called “index.php” and store it in the Apache HTML dir (should be somewhere in the lamp documentation) Open it using http://127.0.0.1/ (if on the local machine) and scan awayūüėČ

      # What type of tables are the files temp. stored in?
      At this moment the script uses the PHP session global vars. This is a special PHP var that saves the data to so called session files. The files are stored somewhere on the system as defined by the localy used php.ini. The files are cleaned by PHP and maintained by PHP as long as you have a connected session (connected to you local webserver that is)

      It the webbrowser is closed the session is discarded by PHP and the files will be cleaned with a fixed interval by a internal PHP process. So the data collected inst stale at this moment.

      If you want the data to be stored for instance, in a database. Then you need to add this functionality in the php code. You could add a “Save” function to store the collected (Session) data when you are done counting, and insert them in a database table. This functionality isnt added as of yet.

  5. Nice discussion….
    really very helpful script…….

  6. how to start payroll system with attendance monitoring using barcode scanner?? are there any site or tutorials??? thank you so much

  7. Hi rai.26

    The most important thing to realize is that a barcodereader is similar to a keyboard. The only difference is that instead of keys you use a pre-printed barcode to fill a designated form.

    What the barcode reader does is interpret the barcode, then sends it (just like a keystroke on a keyboard) back to the UI like any “input” device, and it always ends with an return (enter stroke) => \r

    If you want to fill out more information then only one form element, then you need to do this by using a predefined delimiter inside the barcode. i.e.

    Chris101Gralike101sysengineers.wordpress.com

    then break the string up inside the php code using the delimiter

    $result = explode(“101”, $input);
    var_dump($result);

    Regards,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: