<?php
//************************************************
//************ Change these values ***************
//************************************************
$host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$database = "aap";
$table = "Users";
$UserFieldName = "Username";
$PasswordFieldName = "Password";
//************************************************
//************************************************
//************************************************
function login($username,$password)
{
mysql_connect("$host", "$mysql_user", "$mysql_pass")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
$password=md5($password);
$sql="SELECT * FROM $table WHERE $UserFieldName='$username' AND $PasswordFieldName='$password'";
$result=mysql_query($sql);
$result = mysql_num_rows($result);
if($result!="0"){
$_SESSION["password"] = $password;
$_SESSION["username"] = $username;
echo "You are Logged In!";
}
else
{
echo "Wrong username or password. Please try again!";
}
}
if(isset($_GET["submit"]))
{
$user = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
login($_POST["username"],$_POST["password"]);
}
?>
session_start();
<?php
//************************************************
//************ Change these values ***************
//************************************************
$host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$database = "aap";
$table = "Users";
$UserFieldName = "Username";
$PasswordFieldName = "Password";
//************************************************
//************************************************
//************************************************
function login($username,$password) // function to login
{
mysql_connect("$host", "$mysql_user", "$mysql_pass")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB"); // Connect to the database
$password=md5($password); // Encrypt the password from the form
$sql="SELECT * FROM $table WHERE $UserFieldName='$username' AND $PasswordFieldName='$password'"; // SQL to check if the user exsists
$result=mysql_query($sql); // Perform the MySQL query
$result = mysql_num_rows($result); // See if it exsists
if($result!="0"){ // If it does exsist
$_SESSION["password"] = $password; // Register the password to the session
$_SESSION["username"] = $username; // Register the username to the session
echo "You are Logged In!"; // Tell the user that they are logged in
}
else // But if it doesnt exsist
{
echo "Wrong username or password. Please try again!"; // Tell them they are wrong
}
}
if(isset($_GET["submit"]))
{
$user = mysql_real_escape_string($_POST['username']); // Prevent MySQL Injection, for the username, and set the variable
$pass = mysql_real_escape_string($_POST['password']); // Prevent MySQL Injection, for the password, and set the variable
login($user, $pass); // Call the function, Login.
}
?>
<!-- Now for the form -->
<form method="post" action="login.php?submit">
Username <input type="text" name="username" id="username"/>
Password <input type="password" name="password" id="password"/>
<input type="submit" value="LOGIN"/>
</form>
<?php
session_start();
//************************************************
//************ Change these values ***************
//************************************************
$host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$database = "aap";
$table = "Users";
$UserFieldName = "Username";
$PasswordFieldName = "Password";
//************************************************
//************************************************
//************************************************
function login($username,$password) // function to login
{
mysql_connect("$host", "$mysql_user", "$mysql_pass")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB"); // Connect to the database
$password=md5($password); // Encrypt the password from the form
$sql="SELECT * FROM $table WHERE $UserFieldName='$username' AND $PasswordFieldName='$password'"; // SQL to check if the user exsists
$result=mysql_query($sql); // Perform the MySQL query
$result = mysql_num_rows($result); // See if it exsists
if($result!="0"){ // If it does exsist
$_SESSION["password"] = $password; // Register the password to the session
$_SESSION["username"] = $username; // Register the username to the session
echo "You are Logged In!"; // Tell the user that they are logged in
}
else // But if it doesnt exsist
{
echo "Wrong username or password. Please try again!"; // Tell them they are wrong
}
}
if(isset($_GET["submit"]))
{
$user = mysql_real_escape_string($_POST['username']); // Prevent MySQL Injection, for the username, and set the variable
$pass = mysql_real_escape_string($_POST['password']); // Prevent MySQL Injection, for the password, and set the variable
login($user, $pass); // Call the function, Login.
}
?>
<!-- Now for the form -->
<form method="post" action="login.php?submit">
Username <input type="text" name="username" id="username"/>
Password <input type="password" name="password" id="password"/>
<input type="submit" value="LOGIN"/>
</form>
if(isset($_GET["submit"])) // if ?submit is on the end of the url
{
//$user = mysql_real_escape_string($_POST['username']); // Prevent MySQL Injection, for the username, and set the variable
//$pass = mysql_real_escape_string($_POST['password']); // Prevent MySQL Injection, for the password, and set the variable
$user = $_POST['username']; // Prevent MySQL Injection, for the username, and set the variable
$pass = $_POST['password']; // Prevent MySQL Injection, for the password, and set the variable
login($user, $pass); // Call the function, Login.
}
<?php
/***************/
/* login.php */
/***************/
session_start();
function login($username,$password)
{
require_once("includes/DbVars.php");
mysql_connect("$host", "$mysql_user", "$mysql_pass") or die ("Query failed: " . mysql_error());
mysql_select_db("$database") or die ("Cannot select DB: " . mysql_error());
$password = md5($password); // Encrypt the password from the form
$sql = "SELECT * FROM $table WHERE $UserFieldName='$username' AND $PasswordFieldName='$password'";
$result = mysql_query($sql);
# Using "$result" in the line above AND below caused an error. Using "$dataset" cured it.
$dataset = mysql_num_rows($result);
if ($dataset != "0")
{
$row = mysql_fetch_row($result);
$_SESSION["key"] = $row[0];
$_SESSION["password"] = $password;
$_SESSION["username"] = $username;
$_SESSION["loggedin"] = true;
?>
<script type="text/javascript">
window.location = "test.php"
</script>
<?php
}
else
{
session_destroy();
echo "Wrong username or password. Please try again!";
}
} // End of function
if(isset($_GET["submit"]))
{
$user = $_POST['username'];
$pass = $_POST['password'];
login($user, $pass);
}
?>
<br>Please Login<br/>
<!-- Now for the form -->
<form method="post" action="login.php?submit">
Username <input type="text" name="username" id="username"/><br/>
Password <input type="password" name="password" id="password"/><br><br/>
<input type="submit" value="Login"/>
</form>
//////////////////////////////////////
<?php
/**************/
/* test.php */
/**************/
session_start();
if ($_SESSION["loggedin"] != true)
{
?>
<script type="text/javascript">
window.location = "login.php"
</script>
<?php
}
else
{
echo "You are Logged In, page 1!"; ?> <br/> <?php
echo "key = " . $_SESSION["key"]; ?> <br/> <?php
}
?>
<!-- Page to be accessed only by valid login -->
<form method="post" action="test2.php">
<input type="submit" value="Page 2"/>
</form>
<form method="post" action="logout.php">
<input type="submit" value="Logout"/>
</form>
//////////////////////////////////////
<?php
/***************/
/* test2.php */
/***************/
session_start();
if ($_SESSION["loggedin"] != true)
{
?>
<script type="text/javascript">
window.location = "login.php"
</script>
<?php
}
else
{
echo "You are Logged In, page 2!"; ?> <br/> <?php
echo "key = " . $_SESSION["key"]; ?> <br/> <?php
}
?>
<!-- Page to be accessed only by valid login -->
<form method="post" action="test.php">
<input type="submit" value="Page 1"/>
</form>
<form method="post" action="logout.php">
<input type="submit" value="Logout"/>
</form>
//////////////////////////////////////
<?php
/****************/
/* logout.php */
/****************/
session_start();
session_destroy();
?>
<script type="text/javascript">
window.location = "login.php"
</script>