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
If the answer showing the correction in
while
does not work, it is only worth you check if thesession.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.– Icaro Martins
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 betrue
, redirecting the user tosoldaforte.php
. If what you say is happening happens, the user will also be redirected tosoldaforte.php
when the session does not exist. How do you know which of the two situations is occurring?– Woss
When you say records, are you referring to the bank? Sorry, I’m a beginner in PHP :p
– Anna Gabriela
@Annagabriela Yes, if registration with the user and password informed is not found,
$linhas
will be 0.– Woss
My bank records are okay, I just checked :)
– Anna Gabriela
Dear Anna, change your face
if ($linhas == ''){
forif ($linhas == 0){
, becausemysqli_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.– Guilherme Nascimento
I did it buddy, it didn’t work :/
– Anna Gabriela