0

Sorry for long title.

I have been trying for hours to get this thing to log out... no success.

I am using the form action $_SERVER('PHP_SELF') to send the info placed in the registration form.

All infor is validated and sent to database with no problem.

Once logged in the registration form goes and a logout button appears... Click on that and nothing happens. Code so far:

$db = new Database();
$newUser = new registrationSession($db);

$output = $username = $url = '';

if($_POST)
{
        $validatedFields = new ValidatorSet();

            //VALLIDATION STUFF DELETED

        if($validatedFields->getErrors())
        {
            $username = $validatedFields->getItem('username')->getSanitisedValue();
            $url = $validatedFields->getItem('URL')->getSanitisedValue();

            $output = '<ul>';

            foreach($validatedFields->getErrors() as $key=>$error)
            {
                $output .= '<li>' . $key . ': ' . $error . '</li>';
            }
            $output .= '</ul>';
        }
        else
        {
            $newUser->register($_POST['username'], $_POST['password'], $_POST['email'], $_POST['url'], $_POST['dob']);
        }
    if ($_POST['submit'] == 'logout')
    {
        $newUser->logout();
    }
}
?>

    //HTML NORMAL START STUFF DELETED

<body>
<h1>Assignment Task 3 - Registration Form</h1>
<p>To go to the members secure page you must be logged in, then click the below link:</p>
<p><a href="securePage.php">Go to secure page</a></p>
<p> If you are not a member please fill in the form below</p>
<section>

<?php
if(!$newUser->isLoggedIn())
{
?>

    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <legend>Enter your registration details</legend>
            <label for="username">Username: </label>
            <input type="text" maxlength="20" required name="username" id="username">
            <label for="password">Password: </label>
            <input type="password" required name="password" id="password">
            <label for="email">Email: </label>
            <input type="email" required name="email" id="email">
            <label for="url">Webpage URL: </label>
            <input type="url" name="url" id="url">
            <label for="dob">Date of birth (DD/MM/YYYY): </label>
            <input type="date" required name="dob" id="dob">
        </fieldset>
        <button type="submit" name="submit" value="register" formnovalidate>Submit Details</button>
    <?php
    }
    else
    {
    ?>
        <button type="submit" name="submit" value="logout">Logout</button>
        </form>
    <?php
    }
    ?>

Code for the register(), loggedIn() and logout():

class registrationSession extends DatabaseSession {

        public function register($username, $pass, $email, $url, $dob)
        {
            echo $username;
            echo $pass;
            echo $email;
            echo $url;
            echo $dob;


            $parameters['fields'] = array('username', 'password', 'email', 'url', 'dob');
            $parameters['table'] = 'userDetails';
            $parameters['records'] = array(array($username, $pass, $email, $url, $dob));

            $result = $this->dbConn->insert($parameters);

            if($result)
            {
                session_regenerate_id();
                $_SESSION["username"] = $username;
            }
            else
            {
                throw new Exception("Credentials not found");
            }
        }

    public function logout()
    {
        $_SESSION = array();
        if(ini_get("session.use_cookies"))
        {
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000,
                $params["path"], $params["domain"],
                $params["secure"], $params["httponly"]);
        }
        session_destroy();
        header("location: /Registration.php");
    }

    public function isLoggedIn()
    {
        return isset($_SESSION['username']);
    }
}

Really am stummped, would be grateful for any pointers

2
Contributors
1
Reply
23
Views
3 Years
Discussion Span
Last Post by cereal
0

Move the form opening and closing tags outside the IF / ELSE statements, currently:

  • when the user is not logged the form is not closed;
  • when the user is logged the opening tag is missing and for this reason is not submitted.

You could also use a completly different form and use method GET: because you're not going to create or update any data, just logging out.

Edited by cereal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.