My login won’t catch $_SESSION

Asked

Viewed 369 times

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 you take the user’s sesssions 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 patient.php file, 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

php patients.:

<?php
include 'init.php'; 

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

Where am I going wrong? =(

  • 2

    Dude, this !isset ($_SESSION['dbuser']) == true is very wrong, mix ! with == true is recipe for disaster and confusion of logic.

  • Hahahaha, it’s a very old code. I’ve fixed it.

  • William, I noticed something, exchange <? for <?php in init.php

  • Fixed it all: same thing.

  • session_start(): Cannot send Session cookie - headers already sent by (output Started at /home/Gui/public_html/sistemafp/init.php:1) in /home/Gui/public_html/sistemafp/init.php on line 2

  • Yes, init.php includes in other PHP scripts, which need connection to the database that comes from $_SESSION, but as I am not able to catch the sessoes, error.

Show 1 more comment

1 answer

1

Foul session_start in init.php, you can do:

<?php

session_start();


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

Assuming 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';

You’ll fail if you do:

<?php

echo 'oi';

include 'init.php';

You’ll fail too, so do it like this:

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

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

problema

And of course it does not create cookies, because the headers have already been sent.

  • In the others, I have to leave session_start(); ?

  • @Guilhermelirio only in init.php, because with include you’ll get it, as long as it comes before any echo or space/html, it only keeps in the Conn-login beyond init.

  • But how will I get $_SESSION in Conn-login.php ?

  • Your login only connects, as I said, only the login and init must have session_start, keep in 2, I did not say to take the login.

  • I tested here, and gave the same thing. Another detail: does not create a cookie when logging in.

  • PHP Warning: session_start(): Cannot send Session cookie - headers already sent by (output Started at /home/Gui/public_html/sistemafp/init.php:1) in /home/Gui/public_html/sistemafp/init.php on line 2 PHP Warning: session_start(): Cannot send Session cache Limiter - headers already sent (output Started at /home/Gui/public_html/sistemafp/init.php:1) in /home/Gui/public_html/sistemafp/init.php on line 2

  • But gave in the same "what?" , gave error "Cannot send Session cookie - headers already sent by" or other error, or no error? Guy sends the 3 files, Conn-login, init and one that is including the com include for me to see, you must be making confusion.

  • Poh dude, I already explained in the answer "Cannot send Session cookie - headers already sent by" is when there is room or echo before session_start, so include has to go before the <HTML>, I even sent you the link to understand https://answall.com/q/4251/3635, please read the answer again.

  • @Guilhermelirio does the following, sends the 3 files, Conn-login, init and one that is including the com include for me to see using http://pastebin.com, you must be confused

  • I am not using PHP along with HTML, I only use it separately. I will edit my post exactly as it is here.

  • Dude you’re messing around, the pages you use the include 'init.php' I’m sure you have HTML or echo before include, you don’t have to have HTML in the same script. Cara sends the 3 files because I have no way to imagine where you are getting confused, so I can adjust for you and send you back, but send via Stebin.com or Dropbox or any sharing system, but please do not send everything, only the 3 files I asked for.

  • https://pastebin.com/Y5Kwwc4c

  • You sent everything mixed up and you didn’t even send me what I asked for. Pay attention please, if we are not staying 3 days here, send the file you are including init.php and not init.php. @Guilhermelirio

  • https://pastebin.com/ZDGu5ypP

  • this must have some spacing somewhere, does the following read all this first: https://answall.com/q/4251/3635 and then searches in all files and see if there is no space, even if it is a simple line break.

  • There isn’t, so I’m finding it odd. Nor is cookie creating!

  • @Guilhermelirio gave the error is clear that will not create the cookie, as I said, send me via Dropbox, googledrive or any other means the original files, only the ones I mentioned already, I think I’ve ordered about 3 times.

  • I gave some time, to see if I found the error, but nothing. I can echo the $Sessions in the login, but the init.php file can’t get the sessions.

  • 1

    @Guilhermelirio sends my e-mail all the files, least password files and least database data... Email is this [email protected], this is the most I can do for you now.

  • Not for nothing, but I will not send my files. I sent you the Pastebin, EXACTLY as it is here friend. You want me to send you in separately, I’ll get you in. The topic is exactly as it is now, and is not giving any error, but is not taking the session for connection. !

  • 1

    @Guilhermelirio If you are making a mistake it is because even if you believe they are correct, they must be wrong at some point that you didn’t realize, I don’t know what else to do to help you, as I said it is clear that there is some confusion on your part, the most I can do by even crossing the "allowed" community limit was to send you my email so that I could check where you made the confusion, how you can’t send me and can’t create a "minimized" example that causes the failure (MCVE) I can’t help you, because it’s not a mistake in the language...

  • 1

    ... is a mess on your part, and unfortunately I have no way to guess, the only way is either you create a MCVE or you send me the files, I have no interest in your scripts, I am an experienced programmer and I guarantee that between taking things from others and creating mine I will have more profit creating my own based on the experience of 9 years of programming that I have in several languages such as c, c++, python, php, java, javascript, Vb.net and etc. Do as you please @Guilhermelirio, offered all the help that was possible, it’s up to you now to follow the tips, it’s up to you.

  • I believe I found the error: I cleaned some files on my server and interestingly, started to get $_SESSION on the other page. And I also modified this line in PHP.ini: Session.save_path = "/var/Cpanel/php/Sessions". I don’t know if there’s anything there. Earlier @Guilhermenascimento sent you the files.

Show 18 more comments

Browser other questions tagged

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