Login does not redirect, header ('Location')

2

I just made a site that allows CMS so our client can login and modify the content to their liking. In the localhost and on a test server that we have works fine but at the destination final server I can not get out of the login page after filling in with the correct details.

There are 2 possible outcomes submit :

  • stay on form page
  • and if it's all right it makes redirect into the menu.

But it does not redirect and none of the validation messages also appear. I went to the browser console and the only difference I saw between the servers is in the response header where it is connection: close and in others (where it works correctly) is connection: Keep-Alive . Is this?

I've done a lot of research and can not find any clear answers to help me solve this problem. I've even tried to implement header("Connection: Keep-Alive"); in the code but the problem remains, although the response header is now connection: Keep-Alive, close . I also used var_dump on localhost array(1) { ["logged_in"]=> bool(true) } appears. On the final server array(0) { } appears the session is not being started. I do not understand why.

<?php
session_start();
var_dump($_SESSION);
header("Connection: Keep-Alive");
header('Content-type: text/html; charset=UTF-8');
include_once('../includes/connection.php');
if(isset($_SESSION['logged_in'])) { ?>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>AdminPT</title>
        <link rel ="stylesheet" href="../assets/style.css"/>
    </head>

        <body>
            <div class="container">
                CMS - PT
                <br>

                <ol>

                    <li><a href ="add.php">Adicionar Artigo</a></li>
                    <li><a href ="delete.php">Eliminar Artigo</a></li>
                    <li><a href ="logout.php">Sair</a></li>
                </ol>
            </div>
        </body>
    </html>

    <?php
}
else {
    //display login
    if(isset($_POST['username'], $_POST['password'])) {
        $username = $_POST['username'];
        $password = crypt(sha1(md5($_POST['password'])), 'st');

        if (empty($username) || empty($password)) {
            $error = "Todos os campos têm de ser preenchidos!";
        }
        else {
            $query = $pdo->prepare("SELECT * FROM users WHERE user_name = ? AND user_password = ?");

            $query->bindValue(1, $username);
            $query->bindValue(2, $password);

            $query->execute();

            $num = $query->rowCount();

            if($num == 1) {
                $_SESSION['logged_in'] = true;
            header('Location: index.php');
            exit();
            }
            else {
                $error = "Detalhes incorretos!";
            }
        }
    }

    ?>

    <html>
    <head>
        <title>AdminPT</title>
        <meta charset="UTF-8">
        <link rel ="stylesheet" href="../assets/style.css"/>
    </head>

        <body>
            <div class="container">
                CMS - PT
                <br><br>

                <?php
                if (isset($error)) { ?>
                    <small style="color:#aa0000"><?php echo $error; ?></small>

                <?php } ?>

                <br><br>

                <form action="index.php" method="post">
                    <input type ="text" name="username" placeholder="Username"/>
                    <input type="password" name="password" placeholder="Password"/>
                    <input type="submit" value="Login"/>
                </form>
            </div>
        </body>
    </html>

    <?php
}
?>
    
asked by anonymous 31.03.2014 / 13:04

3 answers

6

1 - First of all, enable all error messages. So it will probably be explicit what is going wrong on the remote server.

2 - Try moving the validations to the beginning of the code. Many servers do not accept instructions of type header:location "in the middle" of the code, but only before <html>

3 - Another possibility is to have some module disabled on the remote server, so check through phpinfo() for the differences between the local and remote server.

I believe that only action 2 solves everything, but even so I leave other suggestions here. :)

    
31.03.2014 / 14:57
2

My name is so I also did the following:

Instead of: header ("location :index.php")

I did so: echo "<a href='index.php'> voltar a página inicial</a>";

Then it will print on the screen back to the homepage, just click and make the return.

    
29.03.2015 / 22:31
2
header("Location: " . $endereco);

You can always put:

exit();

If it does not resolve, it may be that putting this at the beginning of your file:

ob_start();

and at the end of the file:

ob_end_flush(); 
    
11.08.2015 / 15:34