<?php

/* Defines and returns an absolute address.
 * Argument $page is the last path segment in the URL.
 * Default value for $page is 'index.php'
 */

function absolute_url($page = 'login.php') {
    $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    $url = rtrim($url, '/\\');
    $url .= '/' . $page;
    return $url;
}

/* Validates form data (email, password)
 * If both are set, a database query is performed
 * The function demands an existing database connection
 * It returns an array with information
 * - a boolean variable (TRUE/FALSE) indicating success
 * - an array with either errors either db results
 */



function check_loginb($dbc, $userid='', $pass='') {
    $errors = array();

    if (empty($userid)) {
        $errors[] = 'You forgot to enter your userid.';
    } else {
        $u = mysqli_real_escape_string($dbc, trim($userid));
    }

    if (empty($pass)) {
        $errors[] = 'You forgot to enter your password.';
    } else {
        $p = mysqli_real_escape_string($dbc, trim($pass));
    }




    if (empty($errors)) {

        $q = "SELECT  Groupofuse as groupa ,Username as user  FROM users 
		              WHERE Username='$u' AND password=SHA1('$p')";
        $r = mysqli_query($dbc, $q);
       $y = mysqli_num_rows($r);

        // $row = mysqli_fetch_array($r1, MYSQLI_ASSOC);
         if ($y==1) {
            $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
            return array(true, $row);
        } else {
            $errors[] = "The email address and password do not match those on file.";
        }
    }

    return array(false, $errors);
}

?>
