Login does not redirect, header('Location')

Asked

Viewed 7,781 times

2

I just made a website that enables CMS so that our client can login and modify the content to his liking. On the localhost and on a test server that we have works very well, but on the final target server I can’t leave the login page after filling in the correct details.

There are 2 possible outcomes Submit:

  • stay on the form page
  • and if it’s all right he does the redirect to the menu.

But it doesn’t do the redirect and none of the validation messages appear either. I went to the browser console and the only difference I saw between the Servers is in the response header where this is connection: close and in others (where it works correctly) is connection: Keep-Alive. Is this?

I’ve done a lot of research and I can’t find any clear answer to help me solve this problem.
I have even tried to implement header("Connection: Keep-Alive"); in the code but the problem remains, although in the response header it is now connection: Keep-Alive, close. I also used the var_dump on localhost appears array(1) { ["logged_in"]=> bool(true) }. On the final server appears array(0) { } 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
}
?>
  • 2

    Have you tried using the full path in "window.Location"? Although in your case I would use PHP itself, with `header( 'Location: /index.php' ) - However note that the correct in the Location header is to use the full path as well. Nothing to do with the question, but you sticking 3 hashes into each other in the password is only weakening the protection.

  • Thanks, already redirected with php I also used now var_dump in localhost appears "array(1) { ["logged_in"]=> bool(true) }" in the final server appears "array(0) { } ", ie Session is not being started, but I do not understand why. As for the hashs I just copied a method from youtube that sounded good, but thanks for the advice

  • 1

    Whenever you have a moment, take a peek at this question to learn more about passwords and hashes: http://answall.com/questions/2402/comort-hash-de-passwords-safe

  • Thank you very much yes I will see already recorded in bookmarks

  • Have you tried to see if the Phps version is the same? Take a peek at the server error log, there might be some hints there.

  • Server error log? How do I see it? Excuse my ignorance

  • I have been investigating and yet sent a cookie with the id Session... I do not understand pk does not redirect, although the result of the Var_dump is "array(0) { }"

  • Is output_buffering enabled in php.ini? (http://www.php.net/manual/en/outcontrol.configuration.php)

Show 3 more comments

4 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 to move validations to code start. Many servers do not accept instructions like header:location "in the middle" of the code, and yes only before the <html>

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

I believe that only Action 2 solves everything, but I still leave other suggestions. :)

  • Obgado I tried now to implement the option 2 but it is not working I believe that I must be doing something wrong, Mr. can implement this idea in my code sff?

2

header("Location: " . $endereco);

You can always put:

exit();

If it doesn’t resolve, it may be that putting this at the beginning of your file:

ob_start();

and at the end of the archive:

ob_end_flush(); 

2

Mine is like this too I did the following:

In place 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 home page, just click and make the return.

-5

was having a problem of the following type, my file was inside a directory and the page I wanted to redirect was in a repository before .

when I put it that way

header("location:../app_agenda/novo_cadastro.php?cadastro=1");

made a mistake!!!!

I had to do it this way to make it work :)

header("location:../../app_agenda/novo_cadastro.php?cadastro=1");

Browser other questions tagged

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