Ir ao conteúdo
  • Cadastre-se

PHP Não estou conseguindo usar Session...


Posts recomendados

Estou usando a Session, porém quando faço logout esta dando que a variavel user nao existe.

 

Codigo

 


 

<!DOCTYPE html>
<html lang="en">
 <head>
   <title>AuthMe Integration Sample</title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 </head>
 <body>
<?php
    if(!isset($_SESSION)) 
    { 
        session_start(); 
    } 
error_reporting(E_ALL);

require 'AuthMeController.php';

// Change this to the file of the hash encryption you need, e.g. Bcrypt.php or Sha256.php
require 'Sha256.php';
// The class name must correspond to the file you have in require above! e.g. require 'Sha256.php'; and new Sha256();
$authme_controller = new Sha256();

$action = get_from_post_or_empty('action');
$user = get_from_post_or_empty('username');
$pass = get_from_post_or_empty('password');
$email = get_from_post_or_empty('email');

$was_successful = false;
if ($action && $user && $pass) {
    if ($action === 'Log in') {
        $_SESSION['user'] = $user;
        $was_successful = process_login($user, $pass, $authme_controller);
    } else if ($action === 'Register') {
        $was_successful = process_register($user, $pass, $email, $authme_controller);
    }
}

function get_from_post_or_empty($index_name) {
    return trim(
        filter_input(INPUT_POST, $index_name, FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR | FILTER_FLAG_STRIP_LOW)
            ?: '');
}


// Login logic
function process_login($user, $pass, AuthMeController $controller) {
    if ($controller->checkPassword($user, $pass)) {
        printf('<h1>Hello, %s!</h1>', htmlspecialchars($user));
        echo 'Successful login. Nice to have you back!'
            . '<br /><a href="index.php">Back to form</a>';
        return true;
    } else {
        echo '<h1>Error</h1> Invalid username or password.';
    }
    return false;
}

// Register logic
function process_register($user, $pass, $email, AuthMeController $controller) {
    if ($controller->isUserRegistered($user)) {
        echo '<h1>Error</h1> This user already exists.';
    } else if (!is_email_valid($email)) {
        echo '<h1>Error</h1> The supplied email is invalid.';
    } else {
        // Note that we don't validate the password or username at all in this demo...
        $register_success = $controller->register($user, $pass, $email);
        if ($register_success) {
            printf('<h1>Welcome, %s!</h1>Thanks for registering', htmlspecialchars($user));
            echo '<br /><a href="index.php">Back to form</a>';
            return true;
        } else {
            echo '<h1>Error</h1>Unfortunately, there was an error during the registration.';
        }
    }
    return false;
}

function is_email_valid($email) {
    return trim($email) === ''
        ? true // accept no email
        : filter_var($email, FILTER_VALIDATE_EMAIL);
}

?>


<h1>Login sample</h1>
This is a demo form for AuthMe website integration. Enter your AuthMe login details
into the following form to test it.
<form method="post">
 <table>
   <tr><td>Name</td><td><input type="text" value="<?php echo htmlspecialchars($user) ?>" name="username" /></td></tr>
   <tr><td>Email</td><td><input type="text" value="<?php echo htmlspecialchars($email) ?>" name="email" /></td></tr>
   <tr><td>Pass</td><td><input type="password" value="<?php echo htmlspecialchars($pass) ?>" name="password" /></td></tr>
   <tr>
     <td><input type="submit" name="action" value="Log in" /></td>
     <td><input type="submit" name="action" value="Register" /></td>
   </tr>
 </table>
</form>

$_SESSION['user']:
[<?php echo $_SESSION['user'] ?>]


<a href="logout.php">sair</a>
 </body>
</html>

 

Link para o comentário
Compartilhar em outros sites

  • 2 semanas depois...

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!