Autentimise näide php sessiooniga

Allikas: Lambda

Töötavana vt http://dijkstra.cs.ttu.ee/~tammet/k18.php (proovi kasutaja tanel parool lenat)

<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
  echo $_SESSION['count'];
  echo '<p>';
}
$username="X";
$password="X";
if (isset($_REQUEST['logout'])) {
  session_destroy();
  die("Head aega!");
}
if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
  $username=$_SESSION['username'];
  $password=$_SESSION['password'];
}
?>

Algus.
<p>

<?php



$con = mysqli_connect("localhost","st2014","progress","st2014");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

$email = "X";
$auth=0;

if (isset($_REQUEST['username'])) {
  $username = $_REQUEST['username'];
}
if (isset($_REQUEST['password'])) {
  $password = $_REQUEST['password'];
}
if (isset($_REQUEST['email'])) {
  $email = $_REQUEST['email'];
}

$aq = "select username from tt1v1_users where username='$username' and password='$password'";

$result = mysqli_query($con,$aq);
if (! mysqli_fetch_array($result)) {
 $auth=0;
 echo('<p>wrong username, try again!!<p>');
} else {
 $auth=1;
 $_SESSION['username']=$username;
 $_SESSION['password']=$password;
} 

if ($auth) {
echo "Tere $username!"
?>

<form>
<input type="hidden" name="logout" value="yes">
<input type="submit" value="Logi valja">
</form>
<p>

<?php  
  echo "tere " . $username . "<p>";

  $q="SELECT * from tt1v1_users where email='$email'";

  echo "query: $q <br>";
  $result = mysqli_query($con,$q);

  echo "leitud:<p>";
  while($row = mysqli_fetch_array($result)) {
    echo $row['username'] . " " . $row['fullname'];
    echo "<br />";
  }
}

mysqli_close($con);

?>

<form method="post">
<?php
if(!$auth) {
?>
sinu kasutajatunnus:
<input type="text" name="username"><p>
parool: <input type="password" name="password"><p>
otsitav email: <input type="text" name="email" autocomplete="off"><br>
<?php
} else {
?>
<p>
<?php
}
?>
<input type="submit">
</form>

<p>
Lopp.
</body>