My Sessionhandler does not allow me to log in

Asked

Viewed 164 times

2

I’m using this sessionHandler as my sessionHandler.

The problem is that since I am using it I cannot initialize/close the session.

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>

php checks.

//se a password e o login fizerem match:

$_SESSION['id'] = $id; //retornado da query sql.
...
header('Location: index.php');
  • Would it be possible for you to isolate the problem exactly as in the other topic? I mean, create the BD with that structure and implement Handler with Mysqli native functions instead of a third party class?

  • @I inaugurate my class SessionSaveHandler is exactly the same as yours on the second attempt.

  • No, it’s not. I didn’t use a third-party class to operate Mysqli, my constructor had much more arguments, you removed the property Sessionsavehandler::$table which is used in 4 of the 6 stages of the session. That’s not counting what may have been tampered with in what you haven’t shown.

  • @Brunoaugusto is right. I will test with his example without changing anything.

  • @Brunoaugusto continues to be the same. When first loading index.php creates an empty session and then does not change when logging in and does not log in.

  • I suggest you edit the question and show me everything you’re doing. Where, when and how is Initiating Sessionhandler to record, to read and remove, because I tested here again, with and without redirects, with and without session names, and everything worked perfectly, write, read and delete.

  • I edited the question @Brunoaugusto

  • And the error logs?

  • Gives nothing. Just doesn’t fill in the field data table

  • I’m sorry, but I insist there must be something else you haven’t shown here. I even reproduced your example, with this structure of includes and got a positive return through a var_dump() additionally placed on index php. for debugging.

  • @Brunoaugusto sees my answer.

Show 7 more comments

2 answers

3

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

An example would be to use the PdoSessionHandler of Symfony, one Handler working with several databases that can be finding within the HttpFoundation.

To use it simply add the 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 database session simply call the function or class you defined earlier:

<?php

require_once 'session_tuned.php';

my_session_start();

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

var_dump($_SESSION);

Example in Github.

2


I decided to put a condition if($data=='') return false; Bruno’s Sessionhandler in case the data is 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 );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.