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 ūüėÄ


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

  $_SESSION['products'][$barcode] ++;
  $_SESSION['products'][$barcode] = 1;

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

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

11 thoughts on “PHP : Script to count with barcode reader.

  1. or in a class form.

    $counter = new BarCodeCounter;

    class BarCodeCounter{

    private $Post;
    private $Get;

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

    function __destruct(){

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

    private function addCountValue($value){
    $_SESSION[‘products’][$value] ++;
    $_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){
    return addslashes($value);
    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??

    1. 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) {
  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.
    Library files:-

    1. 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.

      1. 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?

    1. 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 :

      # 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 (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. 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.

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

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


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s