<?php
session_start();
header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");

if(isset($_POST['user_id']) && isset($_POST['code']))
{
include 'config.php';
$user_id=$_POST['user_id'];
$user_id=stripslashes($user_id);

$code=$_POST['code'];
$code=stripslashes($code);

$user_id=mysqli_escape_string($con, $user_id);
$code=mysqli_escape_string($con, $code);


$password=$_POST['password'];
$password=stripslashes($password);
$password=mysqli_escape_string($con, $password);

$pass = sha1($password);


$sql="SELECT * FROM users WHERE  user_id ='$user_id' AND ver_code = '$code' ";
$result=$con->query($sql) or die ("error: Server error ".mysqli_error($con));
$count=mysqli_num_rows($result);

if($count == 1)
{

$rows = mysqli_fetch_array($result);
$expiry = $rows['code_expiry'];
$user_id = $rows['user_id'];
$now = strtotime("now"); 

if($now > $expiry)
{
 $suc = "Verification code has expired";
$status = false;  
}

else
{

$sqlx="UPDATE users set ver_code = '', password = '$pass'  WHERE user_id = '$user_id' AND ver_code='$code'";
$resultx=$con->query($sqlx) or die ("error2: Server error ".mysqli_error($con));

if($resultx)
{
$suc = "Successful";
$status = true;
}
}

}
else
{
$suc = "Wrong Code!"; 
$status = false; 
 
}

$con->close();

header('Content-Type: application/json; charset=utf-8');

$output = array('status'=>$status, 'success'=>$suc, 'user_id'=>$user_id, 'code'=>$code);
echo json_encode($output); //output JSON data
}

?>
