2

I am currently working on a project with attendance management system using attendance devices connected on LAN and i own the model SC700 of ZKTeco company. Unfortunately, the device isn't implemented with a web server, so the only way to interact is using the UDP Port 4370. That also means that i can't use this wonderful library https://github.com/cobisja/tad-php

The project is custom, written in PHP(Codeigniter 3) and i have used the dnaextrim's library from github (https://github.com/dnaextrim/php_zklib) to interact with the devices. So far so good, everything works great, except the SetUser functionality. I can set a new user with id,name,password,role from my app to the device but i can't set the RFID of the card he is going to use.

The function builds a 74 character long string and sends it to the device like below

function zksetuser($self, $uid, $userid, $name, $password, $role, $card) {
    $command = CMD_SET_USER;
    //$command_string = str_pad(chr( $uid ), 2, chr(0)).chr($role).str_pad($password, 8, chr(0)).str_pad($name, 28, chr(0)).str_pad(chr(1), 9, chr(0)).str_pad($userid, 8, chr(0)).str_repeat(chr(0),16);
    $byte1 = chr((int) ($uid % 256));
    $byte2 = chr((int) ($uid >> 8));

    $command_string = $byte1 . $byte2 . chr($role) . str_pad($password, 8, chr(0)) . str_pad($name, 24, chr(0)) . str_pad($card, 13, chr(0)) . str_pad($userid, 8, chr(0)) . str_repeat(chr(0), 16);

    $chksum = 0;
    $session_id = $self->session_id;

    $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr($self->data_recv, 0, 8));
    $reply_id = hexdec($u['h8'] . $u['h7']);

    $buf = $self->createHeader($command, $chksum, $session_id, $reply_id, $command_string);

    socket_sendto($self->zkclient, $buf, strlen($buf), 0, $self->ip, $self->port);

    try {
        @socket_recvfrom($self->zkclient, $self->data_recv, 1024, 0, $self->ip, $self->port);

        $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr($self->data_recv, 0, 8));

        $self->session_id = hexdec($u['h6'] . $u['h5']);
        return substr($self->data_recv, 8);
    } catch (ErrorException $e) {
        return FALSE;
    } catch (exception $e) {
        return False;
    }
}

The parameter $card is implemented by me, so i can pass the RFID number of the card, but the problem is that the device "converts" the RFID i send, to another number! I can't figure out what manipulations it does for this conversion or what should i do to map a user with his RFID card.

Has anyone solved this before with ZKTeco SC700 using PHP?

GeorgeGeorgitsis
  • 1,262
  • 13
  • 29
  • did you find a solution? thanks for sharing it if it is the case – Borealis Apr 02 '18 at 16:12
  • 1
    @Borealis actually i have found a solution a long time ago (in my previous company). If you are still interested I can search for that and if you are lucky I can find it again. – GeorgeGeorgitsis May 18 '18 at 11:27
  • @GeorgeGeorgitsis can you share your solution. I am facing the same problem during the last few days. – Mahfuz Shishir Sep 12 '19 at 08:59
  • @MahfuzShishir sorry i'm late. I tried to find the code but couldn't. If i remember right, i think i had to use the `hex2bin` for the $card or the char of the result of it and also do something about the little endian. So in the above code of mine, try to change the `str_pad($card, 13, chr(0))` with what i've told you and hopefully you'll get lucky. Sorry that i can't help you more – GeorgeGeorgitsis Oct 16 '19 at 08:14
  • Thanks for your reply. I had solved the issue. – Mahfuz Shishir Oct 22 '19 at 06:43
  • That's great @MahfuzShishir. Can you please share it here as an answer? – GeorgeGeorgitsis Oct 22 '19 at 06:58
  • In this library, you find the updated solution. `https://github.com/vodvud/php_zklib` `$cardno = hex2bin(Util::reverseHex(dechex($cardno)));` `str_pad($cardno, 4, chr(0))` – Mahfuz Shishir Oct 22 '19 at 07:31

1 Answers1

0

this works for me!

function reverseHex3($hex)
{
    $tmp = '';

    for ($i = strlen($hex); $i >= 0; $i--) {
        $tmp .= substr($hex, $i, 2);
        $i--;
    }

    return $tmp;
}

$cardno = hex2bin(reverseHex3(dechex($card)));

    $command_string = implode('', [
        $byte1,
        $byte2,
        chr($role),
        str_pad($password, 8, chr(0)),
        str_pad($name, 24, chr(0)),
        str_pad($cardno, 4, chr(0)),
        str_pad(chr(1), 9, chr(0)),
        str_pad($userid, 9, chr(0)),
        str_repeat(chr(0), 15)
    ]);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Albert Logic Einstein Nov 27 '22 at 14:55