Skip to content

Instantly share code, notes, and snippets.

@Agurken
Created May 15, 2010 15:49
Show Gist options
  • Save Agurken/402255 to your computer and use it in GitHub Desktop.
Save Agurken/402255 to your computer and use it in GitHub Desktop.
<?php
// Part of the Login template for the SONI projekt
//
// Check.php
// Page for checking for errors
// Copyright Niclas Mølby 2010
// Include the needed files
// Including the Database.php file for database requests
include("Database.php");
// Including the Error.php file for error settings
include("Error.php");
// The Check class
class Check {
// Function that find any errors in login perform
function TryLogin($username, $password){
global $Errors, $Database;
// Set the varibel that counts the errors
$Error = 0;
// Test if username is empty
if(strlen($username) < 1 && $username == ""){
$Errors->setError("Username", "The field is empty");
$Error++;
}
// Test if username exist
if($Database->CheckUsername($username) == 0 && strlen($username) > 0){
$Errors->setError("Username", "The username don't exist");
$Error++;
}
// Test if password is empty
if(strlen($password) < 1 && $password == ""){
$Errors->setError("Password", "The field is empty");
$Error++;
}
// Test if password is right
if(strlen($username) > 0 && strlen($password) > 0 && $Database->WrongPass($username, $password) == 0){
$Errors->setError("Password", "Wrong password!");
$Error++;
}
// If there are errors, set the amount
if($Error > 0){
$Errors->ErrorAmount($Error);
}
return $Error;
}
};
// Intialize a new Check class
$Check = new Check;
?>
<?php
// Part of the Login template for the SONI projekt
//
// Database.php
// Page for almost all database request.
// Copyright Niclas Mølby 2010
// Database class
class Database{
// Function that handle the connection for the database
function Connect(){
$Connect = mysql_connect("localhost", "root", "");
mysql_select_db("Blog", $Connect);
}
// Check if requested name is taken
function CheckUsername($name){
// Connect to database
$this->Connect();
// Request for any users with that name
$Query = mysql_query("SELECT * FROM user WHERE username='$name'");
// Return the result
return mysql_num_rows($Query);
}
// Test if username and password fits together
function WrongPass($username, $password){
// Connect to database
$this->Connect();
// Request for any users with that name and pass
$Query = mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password'");
// Return the result
return mysql_num_rows($Query);
}
}
// Intialize a new Database class request
$Database = new Database;
?>
<?php
// Start the session
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SONI login</title>
</head>
<body>
<?php
// If logged in, show it
if($_SESSION['logged_in']){
echo "<h2>You are now logged in!</h2>";
echo "Welcome: ".$_SESSION['username']."<br /><br />";
echo "<a href=\"Logout.php\">Log out!</a>";
}
// Otherwise show a login form
else {
?>
<h1>Login</h1>
<form action="Process.php" method="post">
<table>
<tr>
<td colspan="3">
<span style="color:#F00; font-size:14px;"><?php echo $_SESSION['ErrNum']; ?></span>
</td>
</tr>
<tr>
<td>
Username:</td> <td><input type="text" name="Username" /></td><td><span style="color:#F00; font-size:12px;"><?php echo $_SESSION['Username']; ?></span></td></tr>
<tr>
<td>
Password:</td> <td><input type="password" name="Password" /></td><td><span style="color:#F00; font-size:12px;"><?php echo $_SESSION['Password']; ?></span></td></tr>
<tr>
<td>
<input type="hidden" name="Login" value="1" />
<input type="submit" value="Login!" />
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
<?php
unset($_SESSION['Username']);
unset($_SESSION['Password']);
unset($_SESSION['ErrNum']);
?>
<?php
// Part of the Login template for the SONI projekt
//
// Logout.php
// Page for logging out, and destroy the required sessions
// Copyright Niclas Mølby 2010
// Start the session
session_start();
// Destroy required sessions
unset($_SESSION['logged_in']);
unset($_SESSION['username']);
// Get back to login page
header("Location: index.php");
?>
<?php
// Part of the Login template for the SONI projekt
//
// Process.php
// Page for processing all request such as add comment, login and so on.
// Copyright Niclas Mølby 2010
// Include the needed files
// Including the Check.php file for error checking
include("Include/Check.php");
// The Process class
class Process {
// Function that handle comment add requests
function Login($username, $password){
global $Check;
// Testing if login contain errors
$Try = $Check->TryLogin($username, $password);
// If no errors perform login
if($Try == 0){
$_SESSION['logged_in'] = 1;
$_SESSION['username'] = $username;
}
// Return to the right page
header("Location: index.php");
}
};
// Intialize a new Process
$Process = new Process;
// Action variabel for know what to do
$Action = $_POST['Login'];
// If action is login start the process
if(isset($Action)){
$Process->Login($_POST['Username'], $_POST['Password']);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment