<?php
@error_reporting(0);

@header("Content-type:text/html; charset=utf-8");
//开启session对话
session_start();
//加载文件
require_once("../include/common.php");

//得到参数,并对其进行addslashes
$action=$_GET["action"] ? $_GET["action"] : $_POST["action"];
if($action=="login")
{
	$username=addslashes($_GET["username"]);
	$password=md5(addslashes($_GET["password"]));
	$code=addslashes($_GET["code"]);

	//验证码判断
	if($code!=$_SESSION["code"])
	{
		showmsg("验证码错误","login.php","2");
		die("验证码错误");
	}

	//信息验证
	if($username=="" || strlen($username)<5 || $password=="" || $code=="")
	{
		showmsg("请核对好数据重新登录","login.php","2");
		die("请核对好数据重新登录");
	}


	//用户验证
	
    $result=$mysql->query("select * from userinfo where username='$username'");
	$pass=$mysql->fetcharray();
	$time=gettime("3");
	$ip=getip();
	$logincount=intval($pass["logincount"]);
	$id=$pass["id"];
	if($pass)
	{
		if($password==$pass["password"])
		{
			$mysql->query("update userinfo set logintime='$time',loginip='$ip',logincount=$logincount+1 where id=$id");
			$_SESSION["username"]=$username;
			$_SESSION["password"]=$password;
			unset($username,$password);
			$mysql->close();
			@header("location:index.php");

		}
		else
		{
			showmsg("密码有错误","login.php","2");
			die("密码有误");
		}
	}
	else
	{
		showmsg("无此用户","login.php","2");
	}
}

if($action=="logout")
{
	$_SESSION["username"]="";
	$_SESSION["password"]="";
	showmsg("登出成功","login.php","2");
}
?>
