login form that calls itself
Posted: Tue Nov 21, 2006 4:11 pm
I'm trying to make the login form appear in each of these cases:
- user want to log in
- user access page when not logged in
- user inputs wrong name and passsword
A login object is created on each page, the constructor does the checks.
Unfortunatley, it doesn't work well when user inputs wrong name/pass.
I'd like to have the same form displayed in every case ...
Code:
you will see that the form with 'invalid name/pass' appears once at every two refreshes 
Please, help me to make this work.
Thank you in advance.
- user want to log in
- user access page when not logged in
- user inputs wrong name and passsword
A login object is created on each page, the constructor does the checks.
Unfortunatley, it doesn't work well when user inputs wrong name/pass.
I'd like to have the same form displayed in every case ...
Code:
Code: Select all
<?php
session_start();
class Test
{
var $bad;
function Test($islogin=false)
{
$bad = false;
if (isset($_SESSION['user']) && isset($_SESSION['pass'])) {
if ($this->checkuser()) echo 'Already logged <br>';
else {
unset($_SESSION);
session_destroy();
session_start();
$this->bad = true;
$this->login();
}
}
else $this->login();
}
function paintform()
{
echo "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "\">\n";
echo "Nume <input type=\"text\" name=\"txtUser\"><br>\n";
echo "Parola <input type=\"password\" name=\"passPass\">\n";
echo "<hr>";
echo "<input type=\"submit\">\n";
echo "</form>\n";
}
function login()
{
if(!$this->bad && isset($_POST['txtUser']) && isset($_POST['passPass'])) {
$_SESSION['user'] = $_POST['txtUser'];
$_SESSION['pass'] = $_POST['passPass'];
}
else {
if ($this->bad) echo "invalid user/pass <br>\n";
$this->bad = false;
$this->paintform();
exit;
}
}
function checkuser() {
if ($_SESSION['user'] != 'test') return false;
if ($_SESSION['pass'] != 'test') return false;
return true;
}
}
$obj =& new Test;
echo ' Success <br>';
?>Please, help me to make this work.
Thank you in advance.