<?php
session_start();
if(strpos(strtolower($_SERVER['REQUEST_URI']), 'phpsessid') !== false)
{
  session_destroy();
  session_regenerate_id();
  session_start();
                              
}
include("phpconfig/config.php");

$username = addslashes(trim($_POST['username']));
$senha = md5(trim($_POST['senha']).sti('salt'));


if (empty($username) OR empty($senha) ){
	
	header('location: index.php?log=3');
	
	} else {


$resultado = mysql_query("SELECT * FROM usuarios WHERE username='$username'");
$linhas = mysql_num_rows($resultado);


if($linhas==0){
	
	header('location: index.php?log=1'); 
	
} else {
	
		if($senha != mysql_result($resultado, 0, "senha")){
	
			
			header('location: index.php?log=1');	
			
		} else {
			
			$resultado = mysql_query("SELECT * FROM usuarios WHERE username='$username'");
			$line = mysql_fetch_assoc($resultado);
			
			$_SESSION['adm']['nome_usuario'] = $username;
			$_SESSION['adm']['senha_usuario'] = stripslashes($senha);
			$_SESSION['adm']['id_user'] = $line['id'];
			$_SESSION['nivel'] = $line['nivel'];
			$_SESSION['status'] = true;
	
			$datalog = date('Y-m-d H:i:s');
			$key = md5($datalog.sti('salt').$username);
	
			$_SESSION['adm']['chave'] = $key;
			$ip_log = $_SERVER["REMOTE_ADDR"];
			
			$sql = "update usuarios set ultimolog = '$datalog', chavelog='$key' , ip = '$ip_log' where username = '$username' and senha='$senha'";
			$lin = mysql_query($sql, $con);
			
			if(!$lin){header("location:".sti('urlbase'));} else { header("location: home/index.php"); }	
			
			
		}	
	
	}

}