<?php

function check_login($con)
{

	if (isset($_SESSION['user_id'])) {

		$id = $_SESSION['user_id'];

		$query = "select * from users where user_id = '$id' limit 1";

		$result = mysqli_query($con, $query);
		if ($result && mysqli_num_rows($result) > 0) {
			$user_data = mysqli_fetch_assoc($result);
			if ($user_data['active'] == 1) {
				return $user_data;
			} else {
				$id = $_SESSION['user_id'];
				mysqli_query($con, "UPDATE `users` SET `active`=0 WHERE `user_id` ='$id'");
				mysqli_close($con);
				unset($_SESSION['user_id']);
			}
		}
	} else {
		$id = 0;
		return $id;
	}
	die;
}

function random_num($length, $con)
{

	$text = "";
	if ($length < 5) {
		$length = 5;
	}

	$len = rand(4, $length);

	for ($i = 0; $i < $len; $i++) {
		$text .= rand(0, 9);
	}
	$query = "SELECT `user_id` FROM `users` WHERE `user_id` = $text limit 1";
	$result = mysqli_query($con, $query);
	if ($result && mysqli_num_rows($result) <= 0) {
		return $text;
	} else {
		random_num(5, $con);
	}
}
