<?php
ob_start();
require("dbconfig.php");
mysql_connect($dbconfig['address'],$dbconfig['user'],$dbconfig['password']);
mysql_select_db($dbconfig['db']);

if (isset($_POST['login'])  && isset($_POST['password'])) {
 
 $login = mysql_real_escape_string($_POST['login']);

 $query = "SELECT * FROM `teatr_users` WHERE `name` = '$login'";
 $result = mysql_query($query);
 
 
  $file = fopen("http://www.sarmacja.org/integracja/haslo_mieszkanca.php?id=$login&haslo=$password", "r");
  $pass_result = fread($file,20);
  fclose($file);
 
 if (mysql_num_rows($result) != 1 || $pass_result != "OK") {
 echo "<p><em>Nie ma takiego loginu w bazie lub złe hasło.</em></p>";
 }
 else {
 if ( !session_id() ) { session_start(); }
 $_SESSION = mysql_fetch_assoc($result);
 echo "<p><em>Zalogowano!</em></p>";
 }
}

elseif (isset($_SESSION['userID'])) {
setcookie(session_name(), '', time()-42000); 
session_unset();
session_destroy();
echo"<em>Wylogowano!</em>";
}

else { ?>

<form action="" method="post">
<label>Login: <input name="login" /></label>
<label>Hasło: <input name="password" type="password" /></label>
<input type="submit" value="Zaloguj!" />
</form>
<?php }


?>