<?php
include 'init.php';


$input_username=$_POST['username'];
$input_password=$_POST['password'];

$username=$input_username;
$password=$input_password;

//echo "<br".$username."-".$password;

$data = array('act'=>'GetToken', 'username'=>$username,	'password'=>$password);
$result_string = runWS($data, $ctype='xml');
//var_dump($result_string);
//echo "<br>";
$result = simplexml_load_string($result_string);
$result = json_decode(json_encode($result), true);
//$t=json_encode($result_string );
//echo "token : ". $t."<br>";

$gettoken=$result['data'];
$j_token=json_encode($gettoken);
$j_decode=json_decode($j_token,true);
//var_dump ($j_token);

				foreach($j_decode as $token){
					$t=$token['token'];
					$token=$token.$t;
				}

//}
$tk=substr($token,0,32);
//echo "<br>token : ".$tk;
//session_start();



if ($username =='' || ($password=='')){
	header('location:login.php?msg=1');
}

elseif (!ctype_alnum($tk)){
  header('location:login.php?msg=2');
  //echo "token tidak dapat";
} 
else{

session_start();
include "timeout.php";
$_SESSION['token']=$tk;
$_SESSION['username']=$username;
$_SESSION['password']=$password;

//$_SESSION['id_sp']=$id_sp;
//$_SESSION['nm_lemb']=$nm_lemb;

  $_SESSION['login'] = 1;
  timer();


	 header('location:media.php?module=home');
	 setcookie("username", $username, time()+3600);
     setcookie("password", $password, time()+3600);

	//header('location:session.php');
	 //echo $id_sp;
//	 echo "<br> $nm_lemb";
}
?>
