<?php
// авторизация

// по сессии
if(!$user and isset($_SESSION['id']) and isset($_SESSION['pass']))
{
$t_id=intval($_SESSION['id']);
$t_pass=ft_nic($_SESSION['pass']);
$user=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE (`id`='$t_id' AND  `pass`='$t_pass') LIMIT 1"),MYSQL_ASSOC);
}
// по кукам
if(!$user and isset($_COOKIE['id']) and isset($_COOKIE['inkey']))
{
$t_id=intval($_COOKIE['id']);
$t_inkey=ft_nic($_COOKIE['inkey']);
$user=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE (`id`='$t_id' AND `inkey`='$t_inkey') LIMIT 1"),MYSQL_ASSOC);
}
// ручная авторизация
if(!$user)
{
  if(isset($_POST['inlog']))
  {
  $t_login=ft_nic($_POST['login']);
  $t_pass=md5($_POST['pass']);
  $user=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE (`login`='$t_login' AND `pass`='$t_pass') LIMIT 1"),MYSQL_ASSOC);
    if($user)
    {
      $t_inkey=gen_key();
      $time=time();
      @mysql_query("UPDATE `user` SET `inkey`='$t_inkey', `r_key`='' WHERE `id`='$user[id]' LIMIT 1");
      setcookie ('id',$user['id'],($time+604800),'',$_SERVER["SERVER_NAME"]);
      setcookie ('inkey',$t_inkey,($time+604800),'',$_SERVER["SERVER_NAME"]);
      setcookie ('reg',1,($time+31557600),'',$_SERVER["SERVER_NAME"]);
      $_SESSION['id']=$user['id'];
      $_SESSION['inkey']=$user['inkey'];
      $_SESSION["reg"]=1;
      $_SESSION['msg'].="Вы успешно авторизованы!";
    }else{
      $_SESSION['error'].="Данные не верны!!";
    }  
header("Location: index.php?l=main"); exit;  
  }
  $_SESSION['title']="авторизация";
  heads($headers_page, $set_ob_start);   //подключение шапки
  echo "<form class='smottb' method='post' action=''>";
  echo "Имя сталкера<br />";
  echo "<input type='text' name='login' value='' /><br />";
  echo "Персональный код (пароль)<br />";
  echo "<input type='password' name='pass' value='' /><br />";
  echo "<input type='submit' name='inlog' value='вход' />";
  echo "</form>";
  echo "<div class='amenu'>";
  echo "<a href='".M."/reg.php?'>Регистрация</a><br />";
  echo "<a href='".M."/index.php?l=rep'>Напомнить пароль</a><br />";
  echo "</div>";
}
?>