<?php
Function RandomString()
{
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randstring = '';

        for ($i = 0; $i < 100; $i++)
        {
                $randVal =  rand (0,61);

                $randstring = $randstring . $characters[$randVal];
        }


        return $randstring;
}
$var1 = $_POST['user'];
$var2 = $_POST['pass'];

$var1 = mysql_real_escape_string($var1);
$var2 = mysql_real_escape_string($var2);


 

$con = mysql_connect('localhost', 'root', 'Boom95z') or die(mysql_error());
mysql_select_db('orion9', $con);


//TRY-CONTRACTOR
$result = mysql_query("SELECT con_id FROM contractors  WHERE username = '" . $var1 . "' and password = password('" . $var2 . "')");
$rowCount = mysql_num_rows($result);

$con_id = '';
// We have a Contractor!
if ($rowCount > 0)
{
        while($row = mysql_fetch_array($result))
        {
                $con_id = $row['con_id'];
        }

        //$sesh = RandomString();
        //$insString12 = "INSERT INTO history (userid, scode, timestamp_is) values (" . $uid . ",'" . $sesh . "', now())";
        
		//mysql_query($insString12);
        //mysql_close($con);
		$cookie_name = "user_con";
		$cookie_value = $con_id;
		setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
		
        header("Location: contractor-home.php?");
		exit;
}



//------------------
//TRY-CLIENT
$result = mysql_query("SELECT org_id FROM organisations  WHERE username = '" . $var1 . "' and password = password('" . $var2 . "')");
$rowCount = mysql_num_rows($result);

$org_id = '';
// We have a client logging in!
if ($rowCount > 0)
{
        while($row = mysql_fetch_array($result))
        {
                $org_id = $row['org_id'];
        }

        //$sesh = RandomString();
        //$insString12 = "INSERT INTO history (userid, scode, timestamp_is) values (" . $uid . ",'" . $sesh . "', now())";
        
		//mysql_query($insString12);
        //mysql_close($con);
		$cookie_name = "user_client";
		$cookie_value = $org_id;
		setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
		
        header("Location: client-home.php?");
		exit;
}



//------------------
//TRY-WHOLESALER
$result = mysql_query("SELECT gp_id FROM general_people  WHERE username = '" . $var1 . "' and password = password('" . $var2 . "')");
$rowCount = mysql_num_rows($result);

$gp_id = '';
// We have a client logging in!
if ($rowCount > 0)
{
        while($row = mysql_fetch_array($result))
        {
                $gp_id = $row['gp_id'];
        }

        //$sesh = RandomString();
        //$insString12 = "INSERT INTO history (userid, scode, timestamp_is) values (" . $uid . ",'" . $sesh . "', now())";
        
		//mysql_query($insString12);
        //mysql_close($con);
		$cookie_name = "user_ws";
		$cookie_value = $gp_id;
		setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
		
        header("Location: wholesaler-home.php?");
		exit;
}

 
       mysql_close($con);  // 24-Mar
       header("Location: sign-in.php?f=b" );
 










?>