SESSION PHP LOGIN

Asked

Viewed 680 times

-2

I’m having problems creating a Session in php. It is as if she simply "did not create", when I will validate if she exists with isset it is as if she did not exist.

I don’t know what I’m missing, here comes the validation code:

<?php

include_once("conexao.php");

$usuario_adm = $_POST['usuario_adm'];
$senha_adm = $_POST['senha_adm'];

$sql = "select  * from adm where usuario='$usuario_adm' and senha='$senha_adm'";
$res = mysqli_query($conexao,$sql);
$linhas = mysqli_num_rows($res);

if ($linhas == ''){
    header("Location: soldaforte.php");
}

else {

    while ($dados=mysqli_fetch_assoc($res)){

        session_start();
        $_SESSION['adm_usu'] = $dados['nome_usu'];
        header("Location: empresa.php");
    }
}

mysqli_close($conexao);


?>
<?php

session_start();

if (!isset($_SESSION['adm_usu'])){
    header("Location: soldaforte.php");
}
?>
  • 1

    If the answer showing the correction in while does not work, it is only worth you check if the session.save_path of your PHP (normally on linux in /var/lib/php/sessions) is with the correct permissions, and if the Sesssions are being created in this directory.

  • Anna, what guarantees that the code is running as you think it is? If your query has no records, the value of $linhas will be 0; you compare with a string empty (which is quite odd) and the result of the comparison would be true, redirecting the user to soldaforte.php. If what you say is happening happens, the user will also be redirected to soldaforte.php when the session does not exist. How do you know which of the two situations is occurring?

  • When you say records, are you referring to the bank? Sorry, I’m a beginner in PHP :p

  • @Annagabriela Yes, if registration with the user and password informed is not found, $linhas will be 0.

  • My bank records are okay, I just checked :)

  • Dear Anna, change your face if ($linhas == ''){ for if ($linhas == 0){, because mysqli_num_rows always returns numbers, never returns an empty string as I answered in: https://answall.com/a/391326/3635 if something else fails it is because you may have more problems, then read the rest of the answer.

  • I did it buddy, it didn’t work :/

Show 2 more comments

1 answer

2

I think the mistake is this:

if ($linhas == ''){

Exchange for:

if ($linhas == 0){

Because mysqli_num_rows always returns int and no empty string, in the case when no results will return zero (0)


Now if the fault continues make a simple test, swap all session_start() for session_start() or die('A sessão não pode ser iniciada');, thus:

<?php

include_once("conexao.php") or die('A sessão não pode ser iniciada');

$usuario_adm = $_POST['usuario_adm'];
$senha_adm = $_POST['senha_adm'];

$sql = "select  * from adm where usuario='$usuario_adm' and senha='$senha_adm'";
$res = mysqli_query($conexao,$sql);
$linhas = mysqli_num_rows($res);

if ($linhas == ''){
    header("Location: soldaforte.php");
}

else {

    while ($dados=mysqli_fetch_assoc($res)){

        session_start();
        $_SESSION['adm_usu'] = $dados['nome_usu'];
        header("Location: empresa.php");
    }
}

mysqli_close($conexao);

?>

The other file

<?php

session_start() or die('A sessão não pode ser iniciada');

if (!isset($_SESSION['adm_usu'])){
    header("Location: soldaforte.php");
}
?>

I say this because it is likely to have some "white space above" even invisible in the text/script editor, if the message appears:

Session cannot be started

Is that in fact the session does not start and probably the errors are turned off on your server, may be the level of error_reporting or the display_errors be off

In this case if you are in localhost connect all errors in your php.ini, if you know how to edit, look for that line (this is to set the error capture level for the log):

error_reporting=

Change her value to

error_reporting=E_ALL

And then look for the line display_errors=, change the value to:

display_errors=On

I recommend you read Why use error_reporting with display_errors and display_startup_errors? to understand about each PHP flag and log configuration

I restarted your local server, either apache or Nginx, whatever it is, you need to restart it completely, if you can’t just log out your user and log back in and then start your server.

Then after this test the scripts, it is likely that session_start() will issue a Warning thus:

Warning: session_start() [Function.Session-start]: Cannot send Session cache Limiter - headers already sent (output Started at

So it really is a spacing error, you may have saved the scripts as UTF-8 with B.O.M., to resolve read this:

Now pay attention, if the error is this:

Warning: Unknown: open(..., O_RDWR) failed: Permission denied (13) in Unknown on line 0

So the problem is much more serious, the folder that gets the session files is inaccessible to PHP (or Apache/Ngnix user etc), so this can help:

But it is an alternative solution, although where saving these sessions is the choice of who configures the server, as long as it does not save in public places, such as ./www or ./public_html

Another similar error that occurred to me including in "professional" hosting was this:

Warning: Unknown(): write failed: No space left on device (28) in Unknown on line 0

  • The configured directory (in the case /tmp) is not writable.
  • Directory does not allow current user access (server may have user access - main, but directory only has root access).
  • It could be a directory that doesn’t exist.

Basically the same solution: /a/51161/3635

  • Man, thanks for trying to help me. But that’s not it yet. I switched $lines == " " for $lines == 0 and put or die on all session_start(), but my session is starting normally. It seems that only when I put the isset gives a bug :/

  • @Annagabriela bug is definitely not. In the second file, which validates, temporarily remove the line header("Location: soldaforte.php"); and do so: <?php&#xA;&#xA;session_start();&#xA;&#xA;if (!isset($_SESSION['adm_usu'])){&#xA; var_dump($_SESSION);&#xA;}&#xA;?>, tries to log in and then checks if var_dump has returned something.

Browser other questions tagged

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