Problems with PHP session - Continue a session

2

I'm setting up a client site and I need to do login control. The user can log in normally and without error, but sending it to the main page when he is logged in is as if he had not logged on. Here's my code:

  

login.php

<form class="form-horizontal" action="conf/logar.php" method="POST">
    <fieldset>
        <!-- Text input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="user">Usuario</label>
            <div class="col-md-2">
                <input id="user" name="user" placeholder="login" class="form-control input-md" required="" type="text">
            </div>
        </div>
        <!-- Password input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="senha">Senha</label>
            <div class="col-md-2">
                <input id="senha" name="senha" placeholder="senha" class="form-control input-md" required="" type="password">
            </div>
        </div>
        <!-- Button -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="log">Login</label>
            <div class="col-md-4">
                <button id="log" name="log" class="btn btn-success">Login</button>
            </div>
        </div>
    </fieldset>
</form>
  

logar.php

include("conexao.php");

$user = $_POST["user"];
$senha = $_POST["senha"];

$userBusca = mysql_query("SELECT * FROM usuario WHERE usuario_login = '".$user.
    "' AND usuario_senha = '".$senha.
    "' ") or die(mysql_error("Erro ao fazer login"));

if (mysql_num_rows($userBusca) == 1) {
    session_start(); //Inicia a sessão
    $_SESSION["usuario_nome"] = $_POS["user"];
    $_SESSION["usuario_senha"] = $_POST["senha"];
    header("Location:../index_logado.php");

} else {
    "<script>
    alert('Usuário não encontrado! Informe os dados corretamente');
    window.location.href = '../login.php'; < /script>";
}

I created a page that controls whether the user is logged in or not.

  

restrict.php

@session_start();

if(isset($_SESSION["usuario_nome"])){

}else{
    header("Location:login.php");
}

But by adding 'restrict.php' to 'index_logged.php' it's as if the user has not logged in and I can not retrieve session data. Thanks for the help. Vlws!

    
asked by anonymous 11.08.2015 / 22:57

2 answers

1

Dude, I have high expectations of finding your problem:

You added the user name in the session like this:  $ _SESSION ["user_name"] = $ _ POS ["user"];

A T is missing; So $ _SESSION ["user_name"] returns NULL;

    
03.09.2015 / 04:58
0

There is an arroba before session_start() , I'll risk saying it's there because it even generated warnings or some other unwanted output. If it was this, the unwanted exit was not for nothing, it indicates a situation that deserves attention.

If the session_start() indicates that the headers have already been sent, obviously there will be problems in running PHP and the at will only prevent the alert for possible problems from being displayed, but they will occur when trying to alert or not.

session_start() should be called before any server output to the browser, if in doubt, best put at the beginning of the code execution.

As a first step I recommend taking the at sign and seeing what appears. If it is normal, take the redirect from the page that authenticates ( logar.php ) and check for output. From what I have seen, there can be no way out. Remember that the browser must have cookies enabled or the url should contain something like PHPSESSION=xxxxxxxx .

Warnings, alerts and errors are not bad, it is usually bad to ignore them. This only refers to the arroba, with it it is not known if there is any problem.

    
12.08.2015 / 01:51