My SessionHandler will not allow me to login

2

I'm using this sessionHandler as my sessionHandler .

The problem is that since I'm using it I can not initialize / log off.

index.php

include_once( 'sessionHandler.php' );

$sessionHandler = new SessionSaveHandler(USER_NAME, PASSWORD, HOST, DATA_BASE,
"session", "my_session_name");

if(!isset($_SESSION['id']))
    include_once 'login.php';

login.php

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    include_once 'verifica.php';
}
?>
<form id="login-form" class="form-signin" method="post" action="">
    <label for="username">Username</label>
    <input id="username" name="username" type="text"/>
    <label for="password">Password</label>
    <input id="password" name="password" type="password"/>
</form>

verify.php

//se a password e o login fizerem match:

$_SESSION['id'] = $id; //retornado da query sql.
...
header('Location: index.php');
    
asked by anonymous 02.12.2014 / 13:30

2 answers

2

I solved by putting a if($data=='') return false; condition in Bruno's SessionHandler in case the data comes empty:

public function write( $id, $data ) {

    $query = sprintf(

        'INSERT INTO %s (id, data) VALUES (?, ?) ON DUPLICATE KEY UPDATE data = ?, last_updated=NULL',

        $this -> table
    );

    $stmt = mysqli_prepare( $this -> link, $query );

    mysqli_stmt_bind_param( $stmt, 'sss', $id, $data, $data );

    if($data=='') return false;    

    return mysqli_execute( $stmt );
}
    
11.12.2014 / 13:35
3

Instead of reinventing the wheel, why not use a ready handler?

An example would be to use PdoSessionHandler from Symfony , a handler that works with several databases that can be found within HttpFoundation .

To use it simply add symfony/http-foundation as a dependency on your project (How to use composer ).

composer require symfony/http-foundation

Create a file to change your Session Handler from the function session_set_save_handler :

<?php

require_once 'vendor/autoload.php';

use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;

function my_session_start()
{
    $pdo = new PDO('mysql:host=localhost;port=3306;dbname=test', 'root', 'root', 
                    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

    $session = new PdoSessionHandler($pdo);

    // Cria as tabelas no banco: rodar só na primeira vez
    //$session->createTable();

    // Quando utilizamos uma classe como SessionHandler, 
    //os parâmetros são um pouco diferentes
    session_set_save_handler($session, true);

    // Inicia a sessão 
    session_start();
}

To start using your session via the database simply call the function or class you defined previously:

<?php

require_once 'session_tuned.php';

my_session_start();

$_SESSION['id'] = '213';
$_SESSION['nome'] = 'garoto';

var_dump($_SESSION);

Example on GitHub .

    
11.12.2014 / 18:12