<?php
ob_start();

if (!session_start()) {
    session_start();
}
?>
<?php

//session_start();
$errors = [];
try {
    $db = new PDO('mysql:host=localhost;dbname=gdhhsxmy_monsoonvibespublica;charset=utf8', 'gdhhsxmy_monsoonvibespublica', 'monsoonvibespublica');
} catch (Exception $e) {
    echo 'Error has Occured';
}



if (isset($_POST['submit'])) {
    echo $k1 = $_POST['email'];
    echo $k2 = $_POST['password1'];



    $stmt = $db->query("select * from customer where email='$k1' ");
    $stmt->execute();
    while ($row = $stmt->fetch()) {
        $str_pass = $row['pass'];

        $pass_check = password_verify($k2, $str_pass);




        if ($pass_check) {
            $custid = $row['Cid'];
        }
    }

    if (isset($custid)) {
        if (!session_start()) {
            session_start();
        }

        $_SESSION['CustId'] = $custid;
        unset($_SESSION['LoginError']);
        unset($_SESSION['errorUser']);
        header('location:../index.php');
    } else {
        $_SESSION['errorUser'] = '<h4>Invalid Credentials<h4>';
        header('location:login.php');
    }
}

if (isset($_POST['submitRegister'])) {
    echo $k1 = $_POST['email'];
    echo $k2 = $_POST['password1'];
    echo $k3 = $_POST['name'];

    echo $k4  = $_POST['housename'];
    echo $k5  = $_POST['location'];
    echo $k6  = $_POST['city'];
    echo $k7  = $_POST['state'];
    echo $k8  = $_POST['country'];
    echo $k9  = $_POST['zipcode'];
    echo $k10 = $_POST['mobile'];


    $query = $db->prepare("select email from customer where email='$k1'  ");

    $query->execute();

    if ($query->rowCount() > 0) { # If rows are found for query
        $_SESSION['RegistererrorUser'] = "<h4>Email is Already registerd !!\n try with Other email.<h4>";
        header('location:signup.php');
    } else {
        $pass = password_hash($k2, PASSWORD_BCRYPT);
        $stmt = $db->prepare("insert into customer values(null,'$k3','$k1','$pass' ,'$k4','$k5','$k6','$k7','$k8','$k9','$k10',now())");
        $stmt->execute();
        unset($_SESSION['error']);
        header('location:login.php');
    }
}
?>

