<?php
ob_start();
/* The above statement together with the one at the bottom (ob_end_flush())are used 
for output buffering. Using them enables us to use sessiin_start(), 
set cookies, use header (redirection), and other headers almost anywhere
in the script with no warnings and no side effects. ob stands for output
buffering. When you use it, every output to the browser (using echo, 
print, print_r, etc) will not be sent to the browser right away. Instead it
will be sent to a memory buffer. When you call ob_end_flush(), what's stored
in the buffer will be sent to the browser. You can use ob_end_clean() instead
of ob_end_flush(), but if you use this one, the buffer will be deleted instead
of being sent to the browser.
*/
session_start();

   # The following prints:
   # /abu-jeib/teaching/Fall_06/207/Examples/Lec18_Files/current_alumni_sessions.php
   // echo $_SERVER['PHP_SELF'], "<br />"; 
   # The following prints the same as the one above
   // echo $_SERVER['REQUEST_URI'], "<br />";
   # The following prints /opt/lampp/htdocs
   // echo $_SERVER['DOCUMENT_ROOT'], "<br />";
   # The following prints www.cs.fredonia.edu
   // echo $_SERVER['SERVER_NAME'], "<br />";

/* Check if cookies are disabled.*/

if (!isset($_COOKIE["PHPSESSID"]) && !isset($_GET["PHPSESSID"]))
{  
	
  $redirect = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  $id = strip_tags(SID);
  # Don't replace SID by session_id() in the above.
  header("Location: ".$redirect."?$id");
  # header("Location: ".$redirect."?SID"); # NO
  exit;
}
elseif (!isset($_COOKIE["PHPSESSID"]) && isset($_GET["PHPSESSID"]))
{  
  exit("<p align=center>You have to enable cookies to log in.</p>");
}


?>

<html>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<! The above meta tags are also used to prevent people from coming 
back to the log in page as logged in using the back button after 
logging out.
-->
 <head>
  <title></title>
 </head>
 <body onload="document.Form1.reset(); document.Form1.Username.focus();">
  <center>

<?
function display_form()
{
echo <<< BEGIN
      <center>
      <h2 align="center">Login Alumni</h2>
      <p></p>
      <form action="{$_SERVER['PHP_SELF']}" NAME="Form1" method="POST">
	<b>Username:</b> <input name="Username" size="15">
      <b>Password:</b> <input name="Password" type="password" size="15">
	<input type="hidden" name="submitted" value="true">
      <p></p>
	<input type="submit" value="SUBMIT">
	<p></p>
      </form>
	</center>
BEGIN;
}

function does_user_exist($username,$password,&$k)
{
	@$d = mysql_connect('localhost','abu-jeib','iyad911');
	if (!d)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}
	if (empty($username))
	{
		echo "You can't leave the username empty.<br>\n";
		return FALSE;
	}
	@$select = mysql_select_db('alumni');
	if (!$select)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}

	$q = "select count(*) from users where username='$username' and password=sha1('$password')";
	@$r = mysql_query($q);
	if (!$r)
	{
		echo "Sorry can't process your information at this time.<BR>";
		@mysql_close($d);
		return FALSE;
	}

	@$row = mysql_fetch_row($r);
	$k = $row[0];

	@mysql_free_result($r);
	@mysql_close($d); 
	return TRUE;
}

if (isset($_POST['submitted']) && isset($_POST['Username']))
{
	$username = trim(stripslashes($_POST['Username']));
	$password = trim(stripslashes($_POST['Password']));
	if (empty($username) || empty($password))
	{
		echo "<p align='center'>You can't leave the username or the password empty.</p>\n";
	}
	else
	{
		$k = 0;
		$flag = does_user_exist($username,$password,$k);
		if (!$flag)
		{
			exit("<p>We can't process your request at this time.</p>");
		}	
		if($k > 0)
		{	
			$_SESSION['username'] = $username;
			# The purpose of this redirect is to prevent coming back
			# to this page as logged in after logging out.
			 header("Location: ".$redirect."?$id");
		}
		else
			echo "Check the username and the password.<br>\n";
	}
}
if (isset($_SESSION['username']) && !empty($_SESSION['username']))
{
	echo "<PRE>




	</PRE>";
	echo  "<p align='center'>Welcome.<BR \>You're logged in as $username.</p><br />\n";
	echo  "<a href='members_alumni_sessions.php'><h2>Members Area</h2></a></p>\n";

}
else
	display_form();

ob_end_flush();
?>
</center></body></html>
