<?php
  require_once ("../admin/config.php");

  if(! @mysql_select_db($dbname,$dbcnx))
  {
    echo("<p>At this moment database is not available, therefore correct display of page is impossible.<br />If you do not install project previously, run script <a href=install.php>http://".$_SERVER['SERVER_ADDR']."/personal/install.php</a></p>");
    exit();
  }

  if(empty($_POST['login'])) puterror2("<br />Login empty.");
  if(strlen($_POST['login'])>100) puterror2("<br />Login very long. Use maximum 100 chars.");
  if(strlen($_POST['password'])>100) puterror2("<br />Password very long. Use maximum 100 chars.");

  if(isset($_POST['login'])) $login=trim($_POST['login']);
    else $login="";
     if(empty($login)) puterror2("<br />Spaces do not approach at login...");

  if(isset($_POST['password'])) $password=trim($_POST['password']);
    else $password="";
     if(empty($password)) puterror2("<br />Spaces do not approach at password...");

  $_POST['login'] = str_replace("'","`",$_POST['login']);
  $_POST['password'] = str_replace("'","`",$_POST['password']);
  $_POST['login'] = htmlspecialchars(stripslashes($_POST['login']));
  $_POST['password'] = htmlspecialchars(stripslashes($_POST['password']));

  $query = "SELECT * FROM `tbl_logins` WHERE `login`='".$_POST['login']."' ";
  $result = mysql_query($query);
  if(!$result) puterror2("<br />Error login checking...");
      $result2 = mysql_fetch_array($result);
      $login = $_POST['login'];
      $ident = $result2['login_id'];
      $passwrd = md5($_POST['password']);
     if($passwrd == $result2['pass'])
     {
       setcookie("login", $login, time()+3600*8);
       setcookie("passwrd", $passwrd, time()+3600*8);
       setcookie("ident", $ident, time()+3600*8);

       echo "<html><head>
             <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=main.php' />
             </head></html>";
     } else puterror2("<br />The pair login-password is wrong! Back to the previouse page and login again.");
?>