kamshory / ZKLibrary

ZKLibrary is PHP library for reading and writing data to attendance device using UDP protocol. This library useful to comunicate between web server and attendance device directly without addition program. This library is implemented in the form of class. So that you can create an object and use it functions.
GNU General Public License v2.0
198 stars 179 forks source link

RFID Read and Write #62

Open tehceen opened 5 years ago

tehceen commented 5 years ago

Sir i need your help how to read RFID And write it on other device.IF this is not possible can you please help me to get the RFID value of both registered and unregistered user when user punch the card? thanks

anditsung commented 5 years ago

hello. this is how to read RFID

public function getUserV2()
    {
        $command = CMD_USERTEMP_RRQ;
        $command_string = chr(5);
        $chksum = 0;
        $session_id = $this->session_id;
        $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $this->received_data, 0, 8) );
        $reply_id = hexdec( $u['h8'].$u['h7'] );
        $buf = $this->createHeader($command, $chksum, $session_id, $reply_id, $command_string);
        //print_r($this->strToHex($buf));
        socket_sendto($this->socket, $buf, strlen($buf), 0, $this->ip, $this->port);
        try
        {
            socket_recvfrom($this->socket, $this->received_data, 1024, 0, $this->ip, $this->port);
            $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr( $this->received_data, 0, 8 ) );
            $bytes = $this->getSizeUser();
            if($bytes)
            {
                while($bytes > 0)
                {
                    socket_recvfrom($this->socket, $received_data, 1032, 0, $this->ip, $this->port);
                    array_push( $this->user_data, $received_data);
                    $bytes -= 1024;                 
                }
                $this->session_id =  hexdec( $u['h6'].$u['h5'] );
                socket_recvfrom($this->socket, $received_data, 1024, 0, $this->ip, $this->port);
            }
            $users = array();
            if(count($this->user_data) > 0)
            {
                for($x=0; $x<count($this->user_data); $x++)
                {
                    if ($x > 0)
                    {
                        $this->user_data[$x] = substr($this->user_data[$x], 8);
                    }
                }
                $user_data = implode('', $this->user_data);
                $user_data = substr($user_data, 11);
                while(strlen($user_data) > 72)
                {
                    $u = unpack('H144', substr($user_data, 0, 72));
                    $u1 = hexdec(substr($u[1], 2, 2));                              
                    $u2 = hexdec(substr($u[1], 4, 2));                              
                    $uid = $u1+($u2*256);                                           // 2 byte
                    $role = hexdec(substr($u[1], 6, 2)).' ';                        // 1 byte
                    $password = hex2bin(substr( $u[1], 8, 16 )).' ';                // 8 byte
                    $name = hex2bin(substr($u[1], 24, 48 )). ' ';                   // 24 byte
                    $tempcard = hexdec($this->reverseHex(substr($u[1], 72, 8)));    // 4 byte card
                    $card = $tempcard == "0" ? "" : $tempcard;
                    $active = hexdec(substr($u[1], 80, 2)). ' ';                    // 1 byte department / enabled ??
                    $userid = hex2bin(substr($u[1], 98, 72)).' ';                   // 36 byte ??
                    $passwordArr = explode(chr(0), $password, 2);                   
                    $password = $passwordArr[0];                                    // get password
                    $useridArr = explode(chr(0), $userid, 2);                       
                    $userid = $useridArr[0];                                        // get user ID
                    $nameArr = explode(chr(0), $name, 3);                           
                    $name = $nameArr[0];                                            // get name
                    if($name == "")
                    {
                        $name = $uid;
                    }
                    $users[$uid] = array($userid, $name, intval($role), $password, $card, intval($active));
                    $user_data = substr($user_data, 72);
                }
            }
            return $users;
        } 
        catch(ErrorException $e) 
        {
            return FALSE;
        } 
        catch(exception $e) 
        {
            return FALSE;
        }
    }

set user, havent test this yet

public function setUserV2($uid, $userid, $name, $password, $role, $card) {
        $uid = (int) $uid;
        $role = (int) $role;
        if($uid > USHRT_MAX)
        {
            return FALSE;
        }
        if($role > 255) $role = 255;
        $name = substr($name, 0, 28);
        $command = CMD_USER_WRQ;
        $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, 4, chr(0)).str_pad(chr(1), 9, chr(0)).str_pad($userid, 8, chr(0)).str_repeat(chr(0),16);
        return $this->execCommand($command, $command_string);
    }
tehceen commented 5 years ago

thank you so much sir. I'll try it if found any issue will back to you. thanks

kartono21 commented 5 years ago

thank you so much sir. I'll try it if found any issue will back to you. thanks

Hi @Tehsseen the code running well??

Mahfuz2811 commented 5 years ago

I have solved this problem. Use below code.

public function setUser($uid, $userid, $name, $cardno, $password, $role)
{
    $uid = (int) $uid;
    $role = (int) $role;
    if($uid > USHRT_MAX)
    {
        return FALSE;
    }
    if($role > 255) $role = 255;
    $name = substr($name, 0, 28);
    $command = CMD_USER_WRQ;
    $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, 28, chr(0)).str_pad(chr(1), 9, chr(0)).str_pad($userid, 8, chr(0)).str_repeat(chr(0),16);

    $cardno = hex2bin($this->reverseHex(dechex($cardno)));
    $command_string = $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, 8, chr(0)).str_repeat(chr(0),16);

    return $this->execCommand($command, $command_string);
}
JoshDavidHerr commented 5 years ago

this works!!! eureka!!!!

public function setUserV2($uid, $userid, $name, $password, $role, $card) { $uid = (int) $uid; $role = (int) $role; if($uid > USHRT_MAX) { return FALSE; } if($role > 255) $role = 255; $name = substr($name, 0, 28); $command = CMD_USER_WRQ; $byte1 = chr((int)($uid % 256)); $byte2 = chr((int)($uid >> 8));

    $card = hex2bin($this->reverseHex(dechex($card)));

    $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(chr(1), 9, chr(0)).                           
                        str_pad($userid, 8, chr(0)).
                        str_repeat(chr(0),16);

    return $this->execCommand($command, $command_string);
}
delcroip commented 4 years ago

this works!!! eureka!!!!

public function setUserV2($uid, $userid, $name, $password, $role, $card) { $uid = (int) $uid; $role = (int) $role; if($uid > USHRT_MAX) { return FALSE; } if($role > 255) $role = 255; $name = substr($name, 0, 28); $command = CMD_USER_WRQ; $byte1 = chr((int)($uid % 256)); $byte2 = chr((int)($uid >> 8));

  $card = hex2bin($this->reverseHex(dechex($card)));

  $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(chr(1), 9, chr(0)).                           
                      str_pad($userid, 8, chr(0)).
                      str_repeat(chr(0),16);

  return $this->execCommand($command, $command_string);
}

to get the 13 byte card number (therefore active is not used)

public function getUser() { $command = CMD_USERTEMP_RRQ; $command_string = chr(5); $chksum = 0; $session_id = $this->session_id; $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr($this->received_data, 0, 8)); $reply_id = hexdec($u['h8'].$u['h7']); $buf = $this->createHeader($command, $chksum, $session_id, $reply_id, $command_string); socket_sendto($this->socket, $buf, strlen($buf), 0, $this->ip, $this->port); try { socket_recvfrom($this->socket, $this->received_data, 1024, 0, $this->ip, $this->port); $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr($this->received_data, 0, 8)); $bytes = $this->getSizeUser(); if($bytes) { while($bytes > 0) { socket_recvfrom($this->socket, $received_data, 1032, 0, $this->ip, $this->port); array_push($this->user_data, $received_data); $bytes -= 1024; } $this->session_id = hexdec($u['h6'].$u['h5']); socket_recvfrom($this->socket, $received_data, 1024, 0, $this->ip, $this->port); } $users = array(); if(count($this->user_data) > 0) { $num = count($this->user_data); for($x = 0; $x < $num; $x++) { if($x > 0) { $this->user_data[$x] = substr($this->user_data[$x], 8); } } $user_data = implode('', $this->user_data); $user_data = substr($user_data, 11); while(strlen($user_data) > 72) { $u = unpack('H144', substr($user_data, 0, 72)); $u1 = hexdec(substr($u[1], 2, 2)); $u2 = hexdec(substr($u[1], 4, 2)); $uid = $u1+($u2*256);// 2 byte $role = hexdec(substr($u[1], 6, 2)).' ';// 1 byte $password = hex2bin(substr($u[1], 8, 16)).' ';// 8 byte $name = hex2bin(substr($u[1], 24, 48)). ' ';// 24 byte $tempcard = hexdec($this->reverseHex(substr($u[1], 72, 26))); // 13 byte card $card= $tempcard == "0" ? "" : $tempcard; //$active = hexdec(substr($u[1], 80, 2)). ' ' $userid = hex2bin(substr($u[1], 98, 72)).' ';// 36 byte $passwordArr = explode(chr(0), $password, 2);// explode to array $password = $passwordArr[0];// get password $useridArr = explode(chr(0), $userid, 2);// explode to array $userid = $useridArr[0];// get user ID $nameArr = explode(chr(0), $name, 3);// explode to array $name = $nameArr[0];// get name if($name == "") { $name = $uid; } $users[$uid] = array('uid' => $userid, 'name' => $name, 'role' => intval($role), 'passwd' => $password, 'card' => $card); $user_data = substr($user_data, 72); } } return $users; } catch(ErrorException $e) { return false; } catch(exception $e) { return false; } }