My login does not get $ _SESSION [closed]

0

I have the following codes:

conn-login.php :

<?php
session_start();
header('Access-Control-Allow-Origin: *');

//Conexão MYSQLI - LOGIN GLOBAL
$mysqli = new mysqli('localhost', 'login', 'senha', 'bancodedados');

?>

login.php :

<?php
include 'conn-login.php';

header('Content-Type: application/json');

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $email_l = $_POST['email'];
    $senha_l = md5($_POST['senha']);

    $sql = "SELECT u.id, u.id_farmacia, u.usuario, u.email, u.senha, u.accesskey, f.db_database, f.db_username, f.db_password FROM usuarios u INNER JOIN farmacias f ON u.id_farmacia = f.id WHERE (email =? AND senha =?) LIMIT 1";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('ss', $email_l, $senha_l);
    $stmt->execute();
    $stmt->bind_result($id, $id_farmacia, $usuario, $email, $senha, $accesskey, $database, $dbuser, $dbpass);
    $stmt->store_result();

    if($stmt->num_rows <= 0){

        $var = Array(
            'status' => 'ERRO',
            'msg' => 'Usuário não cadastrado e/ou senha incorreta!'
        );

    } else {

        while ($ln = $stmt->fetch()){

            $_SESSION['database'] = $database;
            $_SESSION['dbuser'] = $dbuser;
            $_SESSION['dbpass'] = $dbpass;

            $var = Array(
                'status' => 'OK',
                'accesskey' => $accesskey
            );
        };

    }
}    
echo json_encode($var);
exit;
$stmt->close(); 

?>

and then init.php, where it takes the user's sessions to connect to a new database (logged in user database).

init.php

<?

//Conexão MYSQLI
$db = $_SESSION['database'];
$dbuser = $_SESSION['dbuser'];
$dbpass = $_SESSION['dbpass'];

var_dump($_SESSION);

$mysqli = new mysqli('localhost', $dbuser, $dbpass, $db);

$mysqli->query("SET NAMES 'utf8'");
$mysqli->query('SET character_set_connection=utf8');
$mysqli->query('SET character_set_client=utf8');
$mysqli->query('SET character_set_results=utf8');

//Data Padrão do Brasil (GMT -3)
date_default_timezone_set('America/Sao_Paulo');

?>

When I use INIT.PHP, VAR_DUMP shows NULL.

On the host, when I use the file patients.php, where I retrieve data from all patients:

[21-Sep-2017 16:45:49 UTC] PHP Warning:  mysqli::mysqli(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in /home/gui/public_html/sistemafp/init.php on line 9
[21-Sep-2017 16:45:49 UTC] PHP Warning:  mysqli::query(): Couldn't fetch mysqli in /home/gui/public_html/sistemafp/init.php on line 11
[21-Sep-2017 16:45:49 UTC] PHP Warning:  mysqli::query(): Couldn't fetch mysqli in /home/gui/public_html/sistemafp/init.php on line 12
[21-Sep-2017 16:45:49 UTC] PHP Warning:  mysqli::query(): Couldn't fetch mysqli in /home/gui/public_html/sistemafp/init.php on line 13
[21-Sep-2017 16:45:49 UTC] PHP Warning:  mysqli::query(): Couldn't fetch mysqli in /home/gui/public_html/sistemafp/init.php on line 14
[21-Sep-2017 13:45:49 America/Sao_Paulo] PHP Warning:  mysqli::prepare(): Couldn't fetch mysqli in /home/gui/public_html/sistemafp/pac.php on line 5
[21-Sep-2017 13:45:49 America/Sao_Paulo] PHP Fatal error:  Call to a member function execute() on null in /home/gui/public_html/sistemafp/pac.php on line 6

patients.php:

<?php
include 'init.php'; 

if(isset($_POST['id_paciente'])){
//restante do meu código
}

Where am I going wrong? = (

    
asked by anonymous 19.09.2017 / 03:56

1 answer

1

Missing session_start in init.php, you can do:

<?php

session_start();


if((!isset ($_SESSION['dbuser'])) and (!isset ($_SESSION['dbpass'])))
{

Assuming that init.php is included in another file include it at the top before any space, as explained in this question:

That is, if you do something like:

<html>
<?php

include 'init.php';

It will fail if you do:

<?php

echo 'oi';

include 'init.php';

It will also fail, so do so:

<?php
include 'init.php';
?>
<html>

If there is a space or line break that is before session_start the error will occur, for example:

And of course you do not create cookies, because the headers have already been sent.

    
19.09.2017 / 04:32