Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<?php
session_start();
if (!isset($_SESSION['phplogin'])
|| $_SESSION['phplogin'] !== true) {
header('Location: login.php'); //Replace that if login.php is somewhere else
exit;
}
?>
<?php session_start(); require 'approve.php'; ?>
<?php
session_start();
if(isset($_POST['login']))
{
$password = $_POST['pswd'];
if ( $password == "mypassword" ) { //Replace mypassword with your password it login
$_SESSION['phplogin'] = true;
header('Location: index.php'); //Replace index.php with what page you want to go to after succesful login
exit;
} else {
?>
<?php require_once 'activeUser.php'; ?>
<?php
/*
* CONFIG
*
*/
define ('TIMEOUT', 1); //define the login timeout in minutes set to zero for no timeout
define ('ALLOWREMEMBERME', true); //to allow users to select to avoid login through remember me function
define ('COOKIEDURATION', 100); //define the amount of time that the browser remembers the user
define ('SALT', '3434jbnhkef'); //random characters for hashing
/**
* sample sql
create table if not exists TABLENAME (
userID INT(10) AUTO_INCREMENT PRIMARY KEY ,
loginID varchar(255) UNIQUE,
pwd varchar(255)
)
*/
class activeUser{
public function __construct(){
$sitename = ''; //name of the site
//set these for username pwd based database connections
$username = null;
$password = null;
$userTable = ''; //enter the table name for the databse
//mysql dsn looks like this mysql:host=hostname;dbname=testdatabase
//see here for more details [URL unfurl="true"]http://fr2.php.net/manual/en/ref.pdo-mysql.connection.php[/URL]
$dsn = '';
try{
$this->pdo = new PDO ($dsn, $username, $password); //your dsn goes here
} catch (PDOException $e){
die ('Cannot connect: ' . $e->getMessage());
}
$this->startUp();
}
public function startUp(){
if ($this->isLogOutRequest()){
$this->logOut();
}elseif ($this->isLogOnRequest()){
if ($this->isValidUser()){
$this->logIn();
$this->checkRememberMe();
} else {
$this->logOut();
session_write_close();
exit();
}
} elseif ($this->isLoggedIn()){
$this->logIn();
} elseif ($this->isRememberedUser()){
$this->logIn();
$this->checkRememberMe();
} else{
$this->logOut();
session_write_close();
exit();
}
}
private function logIn(){
$_SESSION['activeUser']['loggedIn'] = true;
$_SESSION['activeUser']['lastAccess'] = time();
}
private function logOut(){
unset ($_SESSION['activeUser']);
$this->removeRememberMeCookie();
$this->displayLoginForm();
}
private function isLogOutRequest(){
if (isset($_GET['action']) && $_GET['action'] == 'logout'){
return true;
} else {
return false;
}
}
private function isRememberedUser(){
if (defined('ALLOWREMEMBERME') && ALLOWREMEMBERME === TRUE){
if (isset($_COOKIE['forgetmenot'])){
$data = unserialize($_COOKIE['forgetmenot']);
list ($this->userID, $this->pwd) = $data;
//just get user name based responses
$sql = "select pwd as c from {$this->userTable} where userID=?";
$params = array ($this->userID);
$s = $this->db->prepare($sql);
$s->execute($params);
$row = $s->fetchAll();
if (count($row) !== 1){
$this->errorMessages[] = "Sorry, either this is a spoof login attempt or your user account has been deleted or suspended since your last visit";
return false;
} else {
//check the password
$pwd = $row[0][pwd];
if ( $this->encrypt($pwd . $this->encrypt(SALT)) == $this->pwd){
$_SESSION['activeUser']['userID'] = $this->userID;
return true;
} else {
$this->errorMessages = "Sorry, the automatically supplied password was incorrect";
return false;
}
}
} else {
return false;
}
} else {
return false;
}
}
private function setRememberMeCookie(){
$expire = time() + (COOKIEDURATION * 24 * 60 * 60);
$cookie_pass = $this->encrypt( $this->encrypt($this->pwd) . $this->encrypt(SALT) );
$cookiedata = serialize(array($this->userID, $cookie_pass));
setcookie('forgetmenot', $cookiedata, $expire);
}
private function removeRememberMeCookie(){
if (isset($_COOKIE['forgetmenot'])){
$expire = time() - 3600;
$cookiedata = serialize (array('',''));
setcookie('forgetmenot', $cookiedata, $expire);
}
}
private function checkRememberMe(){
if (defined('ALLOWREMEMBERME') && ALLOWREMEMBERME===TRUE){
if (isset($_POST['rememberme'])){
$this->setRememberMeCookie();
} else {
$this->removeRememberMeCookie();
}
} elseif(defined('ALLOWREMEMBERME') AND ALLOWREMEMBERME=== FALSE) {
$this->removeRememberMeCookie();
}
}
private function formValidates(){
if (empty($_POST['loginID']) || empty($_POST['pwd'])){
return false;
} else {
$this->loginID = $_POST['loginID'];
$this->pwd = $_POST['pwd'];
return true;
}
}
private function isValidUser(){
if (!$this->formValidates()){
$this->errorMessages[] = "You have not supplied either a username or a password";
$this->logout();
}
if (!$this->checkNonce('logIn')){
$this->errorMessages[] = "You cannot login through using the back button or refresh";
$this->logOut();
}
$sql = "SELECT userID from {$this->userTable} where loginID=? and pwd=? ";
$params = array ( $this->loginID,
$this->encrypt($this->pwd),
);
$s = $this->pdo->prepare($sql);
$s->execute($params);
$results = $s->fetchAll();
if (count($results) === 1){
$userID = $results[0][0];
$_SESSION['activeUser']['userID'] = $userID;
return true;
} else {
$this->errorMessages[] = "Either your username or password is incorrect";
return false;
}
}
private function isLogOnRequest(){
if (isset($_POST['action']) && ($_POST['action'] === 'logIn')){
return true;
} else {
return false;
}
}
private function isLoggedIn(){
if (isset($_SESSION['activeUser']['loggedIn']) && $_SESSION['activeUser']['loggedIn'] === true){
if ($this->sessionTimedOut()){
$this->errorMessages[] = "Your login session has timed out. Please log in again";
return false;
} else {
return true;
}
} else {
return false;
}
}
private function sessionTimedOut(){
if (defined('TIMEOUT') && TIMEOUT > 0){
if ( ($_SESSION['activeUser']['lastAccess'] + ( TIMEOUT * 60) ) < time() ){
return true;
} else {
return false;
}
} else {
return false;
}
}
private function encrypt($val){
return sha1($val);
// return $val;
}
private function getErrorMessage(){
$message = '';
foreach ($this->errorMessages as $msg) {
$message .= <<<HTML
<p class="message">$msg</p>
HTML;
}
return empty($message) ? null : "<div class=\"messages\">\r\n" . $message . "</div>";
}
private function getNonce($type='default'){
if (isset($_SESSION['activeUser']['nonce'][$type])){
//
} else {
$_SESSION['activeUser']['nonce'][$type] = sha1 (uniqid('nonce', true));
}
return $_SESSION['activeUser']['nonce'][$type] ;
}
private function checkNonce($type='default'){
if (empty($_SESSION['activeUser']['nonce'][$type])){
return false;
}
if (empty($_REQUEST['activeUser']['nonce'])){
return false;
}
if ($_SESSION['activeUser']['nonce'][$type] == $_POST['nonce']){
unset ($_SESSION['activeUser']['nonce'][$type]);
return true;
} else {
return false;
}
}
private function rememberMeCheckBox(){
if (defined('ALLOWREMEMBERME') && ALLOWREMEMBERME===true) {
return <<<HTML
<p class="forgetmenot">
<label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" /> Remember Me</label>
</p>
HTML;
}
}
private function insertNonceField($type){
$value = $this->getNonce($type);
return <<<HTML
<input type="hidden" name="nonce" value="$value" />
HTML;
}
private function displayLoginForm(){
echo <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] dir="ltr" lang="en-US">
<head>
<title>{$this->sitename} - Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function focusit() {
document.getElementById('user_login').focus();
}
window.onload = focusit;
</script>
</head>
<body class="login">
<div id="login">
<h1>{$this->sitename}</h1>
{$this->getErrorMessage()}
<form name="loginform" id="loginform" action="index.php" method="post">
<p>
<label>Username<br />
<input type="text" name="loginID" id="user_login" class="input" value="" size="20" tabindex="10" /></label>
</p>
<p>
<label>Password<br />
<input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
</p>
{$this->rememberMeCheckBox()}
<p class="submit">
<input type="submit" name="wp-submit" id="wp-submit" value="Log In" tabindex="100" />
<input type="hidden" name="action" value="logIn" />
{$this->insertNonceField('logIn')}
</p>
</form>
</div>
</body>
</html>
HTML;
}
}
if (session_id() == '' ) {
session_start();
}
$activeUser = new activeUser();
echo "If you are seeing this then you are logged in";
?>
<?php
/*
* CONFIG
*
*/
define ('TIMEOUT', 1); //define the login timeout in minutes set to zero for no timeout
define ('ALLOWREMEMBERME', true); //to allow users to select to avoid login through remember me function
define ('COOKIEDURATION', 100); //define the amount of time in days that the browser remembers the user
define ('SALT', '3434jbnhkef'); //random characters for hashing
/**
* sample sql
create table if not exists users (
userID INT(10) AUTO_INCREMENT PRIMARY KEY ,
loginID varchar(255) UNIQUE,
pwd varchar(255)
)
*/
class activeUser{
private $errorMessages = array();
public function __construct(){
$this->sitename = 'MySite'; //name of the site
//set these for username pwd based database connections
$username = 'root';
$password = 'root';
$this->userTable = 'users'; //enter the table name for the databse
//mysql dsn looks like this mysql:host=hostname;dbname=testdatabase
//see here for more details [URL unfurl="true"]http://fr2.php.net/manual/en/ref.pdo-mysql.connection.php[/URL]
$dsn = 'mysql:host=localhost;dbname=test';
try{
$this->pdo = new PDO ($dsn, $username, $password); //your dsn goes here
} catch (PDOException $e){
die ('Cannot connect: ' . $e->getMessage());
}
$this->startUp();
}
public function startUp(){
if ($this->isLogOutRequest()){
$this->logOut();
}elseif ($this->isLogOnRequest()){
if ($this->isValidUser()){
$this->logIn();
$this->checkRememberMe();
} else {
$this->logOut();
session_write_close();
exit();
}
} elseif ($this->isLoggedIn()){
$this->logIn();
} elseif ($this->isRememberedUser()){
$this->logIn();
$this->checkRememberMe();
} else{
$this->logOut();
}
}
private function logIn(){
$_SESSION['activeUser']['loggedIn'] = true;
$_SESSION['activeUser']['lastAccess'] = time();
}
private function logOut(){
unset ($_SESSION['activeUser']);
$this->removeRememberMeCookie();
$this->displayLoginForm();
}
private function isLogOutRequest(){
if (isset($_GET['action']) && $_GET['action'] == 'logout'){
return true;
} else {
return false;
}
}
private function isRememberedUser(){
if (defined('ALLOWREMEMBERME') && ALLOWREMEMBERME === TRUE){
if (isset($_COOKIE['forgetmenot'])){
$data = unserialize($_COOKIE['forgetmenot']);
list ($this->userID, $this->pwd) = $data;
//just get user name based responses
$sql = "select pwd as c from {$this->userTable} where userID=?";
$params = array ($this->userID);
$s = $this->db->prepare($sql);
$s->execute($params);
$row = $s->fetchAll();
if (count($row) !== 1){
$this->errorMessages[] = "Sorry, either this is a spoof login attempt or your user account has been deleted or suspended since your last visit";
return false;
} else {
//check the password
$pwd = $row[0][pwd];
if ( $this->encrypt($pwd . $this->encrypt(SALT)) == $this->pwd){
$_SESSION['activeUser']['userID'] = $this->userID;
return true;
} else {
$this->errorMessages = "Sorry, the automatically supplied password was incorrect";
return false;
}
}
} else {
return false;
}
} else {
return false;
}
}
private function setRememberMeCookie(){
$expire = time() + (COOKIEDURATION * 24 * 60 * 60);
$cookie_pass = $this->encrypt( $this->encrypt($this->pwd) . $this->encrypt(SALT) );
$cookiedata = serialize(array($this->userID, $cookie_pass));
setcookie('forgetmenot', $cookiedata, $expire);
}
private function removeRememberMeCookie(){
if (isset($_COOKIE['forgetmenot'])){
$expire = time() - 3600;
$cookiedata = serialize (array('',''));
setcookie('forgetmenot', $cookiedata, $expire);
}
}
private function checkRememberMe(){
if (defined('ALLOWREMEMBERME') && ALLOWREMEMBERME===TRUE){
if (isset($_POST['rememberme'])){
$this->setRememberMeCookie();
} else {
$this->removeRememberMeCookie();
}
} elseif(defined('ALLOWREMEMBERME') AND ALLOWREMEMBERME=== FALSE) {
$this->removeRememberMeCookie();
}
}
private function formValidates(){
if (empty($_POST['loginID']) || empty($_POST['pwd'])){
return false;
} else {
$this->loginID = $_POST['loginID'];
$this->pwd = $_POST['pwd'];
return true;
}
}
private function isValidUser(){
if (!$this->formValidates()){
$this->errorMessages[] = "You have not supplied either a username or a password";
$this->logOut();
}
if (!$this->checkNonce('logIn')){
$this->errorMessages[] = "You cannot login through using the back button or refresh";
$this->logOut();
}
$sql = "SELECT userID from {$this->userTable} where loginID=? and pwd=? ";
$params = array ( $this->loginID,
$this->encrypt($this->pwd),
);
$s = $this->pdo->prepare($sql);
$s->execute($params);
$results = $s->fetchAll();
if (count($results) === 1){
$userID = $results[0][0];
$_SESSION['activeUser']['userID'] = $userID;
return true;
} else {
$this->errorMessages[] = "Either your username or password is incorrect";
return false;
}
}
private function isLogOnRequest(){
if (isset($_POST['action']) && ($_POST['action'] === 'logIn')){
return true;
} else {
return false;
}
}
private function isLoggedIn(){
if (isset($_SESSION['activeUser']['loggedIn']) && $_SESSION['activeUser']['loggedIn'] === true){
if ($this->sessionTimedOut()){
$this->errorMessages[] = "Your login session has timed out. Please log in again";
return false;
} else {
return true;
}
} else {
return false;
}
}
private function sessionTimedOut(){
if (defined('TIMEOUT') && TIMEOUT > 0){
if ( ($_SESSION['activeUser']['lastAccess'] + ( TIMEOUT * 60) ) < time() ){
return true;
} else {
return false;
}
} else {
return false;
}
}
private function encrypt($val){
return sha1($val);
// return $val;
}
private function getErrorMessage(){
$message = '';
foreach ($this->errorMessages as $msg) {
$message .= <<<HTML
<p class="message">$msg</p>
HTML;
}
return empty($message) ? null : "<div class=\"messages\">\r\n" . $message . "</div>";
}
private function getNonce($type='default'){
if (isset($_SESSION['activeUser']['nonce'][$type])){
//
} else {
$_SESSION['activeUser']['nonce'][$type] = sha1 (uniqid('nonce', true));
}
return $_SESSION['activeUser']['nonce'][$type] ;
}
private function checkNonce($type='default'){
if (empty($_SESSION['activeUser']['nonce'][$type])){
return false;
}
if (empty($_REQUEST['nonce'])){
return false;
}
if ($_SESSION['activeUser']['nonce'][$type] == $_POST['nonce']){
unset ($_SESSION['activeUser']['nonce'][$type]);
return true;
} else {
return false;
}
}
private function rememberMeCheckBox(){
if (defined('ALLOWREMEMBERME') && ALLOWREMEMBERME===true) {
return <<<HTML
<p class="forgetmenot">
<label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" /> Remember Me</label>
</p>
HTML;
}
}
private function insertNonceField($type){
$value = $this->getNonce($type);
return <<<HTML
<input type="hidden" name="nonce" value="$value" />
HTML;
}
private function displayLoginForm(){
echo <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] dir="ltr" lang="en-US">
<head>
<title>{$this->sitename} - Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function focusit() {
document.getElementById('user_login').focus();
}
window.onload = focusit;
</script>
</head>
<body class="login">
<div id="login">
<h1>{$this->sitename}</h1>
{$this->getErrorMessage()}
<form name="loginform" id="loginform" action="{$_SERVER['PHP_SELF']}" method="post">
<p>
<label>Username<br />
<input type="text" name="loginID" id="user_login" class="input" value="" size="20" tabindex="10" /></label>
</p>
<p>
<label>Password<br />
<input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
</p>
{$this->rememberMeCheckBox()}
<p class="submit">
<input type="submit" name="wp-submit" id="wp-submit" value="Log In" tabindex="100" />
<input type="hidden" name="action" value="logIn" />
{$this->insertNonceField('logIn')}
</p>
</form>
</div>
</body>
</html>
HTML;
exit;
}
}
if (session_id() == '' ) {
session_start();
}
$activeUser = new activeUser();
echo "If you are seeing this then you are logged in";
if (!class_exists('PDO')){
class PDO{
private $dbLink;
public function __construct($dsn, $username, $password){
$t = preg_match ('/^mysql:host=(.*?);dbname=(.*?)$/imsx', $dsn, $match);
if (!$t){
throw new PDOException('The DSN is not properly constructed');
return false;
}
$this->dbLink = mysql_connect($match[1]);
if (!$this->dbLink) {
throw new PDOException(mysql_error());
return false;
}
if (!mysql_select_db($match[2], $this->dbLink)){
throw new PDOException(mysql_error());
return false;
}
return true;
}
public function prepare ($query){
return new PDO_statement($query, $this->dbLink);
}
}
class PDO_statement{
public function __construct ($query, $dbh){
$this->result = array();
mysql_query("SET @q = '$query'" ) or die(mysql_error());
mysql_query("PREPARE STMNT FROM @q") or die(mysql_error());
}
public function execute($params = array()){
$i = 0;
$using = array();
foreach($params as $param){
mysql_query("SET @a{$i} = '$param'") or die(mysql_error());
$using[] = "@a{$i}";
$i++;
}
if ($i > 0){
$result = mysql_query("EXECUTE STMNT USING " . implode(',', $using));
} else{
$result = mysql_query("EXECUTE STMNT");
}
if (!$result){
die(mysql_error());
}
while ($row = mysql_fetch_array($result)){
$this->result[] = $row;
}
mysql_query('DEALLOCATE STMNT');
}
public function fetchAll(){
return $this->result;
}
}
}
if (!class_exists('PDOException')){
class PDOException extends Exception{
public function __construct($message , $code=0){
parent::__construct ($message, $code);
}
}
}
just add this in to the login file and it will all work fine.
public function __construct(){
$this->sitename = 'MySite'; //name of the site
//set these for username pwd based database connections
$username = 'myusername';
$password = 'canttellyoudat';
$this->userTable = 'choir_list'; //enter the table name for the databse
//mysql dsn looks like this mysql:host=hostname;dbname=testdatabase
//see here for more details [URL unfurl="true"]http://fr2.php.net/manual/en/ref.pdo-mysql.connection.php[/URL]
$dsn = 'mysql:host=88.208.944.324;dbname=mydatabase';