0

My header location doesn't works, in my wampserver it works, when I put it in my server, it doesn't, any Ideas?

If I put a echo instead of the header, it works fine.

<html>
    <head>
        <meta charset="utf-8">
        <title>Login</title>
        <link rel="stylesheet" href="css/login.css" />
    </head>
    <body>
    <?php
        require('db.php');

        session_start();

        // If form submitted, insert values into the database.
        if (isset($_POST['username'])){
            $username = $_POST['username'];
            $password = $_POST['password'];

            $username = stripslashes($username);
            $username = mysqli_real_escape_string($connection, $username);

            $password = stripslashes($password);
            $password = mysqli_real_escape_string($connection, $password);

            $query = "SELECT * FROM `users` WHERE username='$username' and password='$password'";
            $result = mysqli_query($connection, $query) or die(mysql_error());
            $rows = mysqli_num_rows( $result);
            if($rows==1){
                $_SESSION['username'] = $username;
                header("Location: welkom.php");
            }else{
                echo "<div id='cancel'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='index.php'>try again</a></div>";
            }
        }else{   
    ?>
    <div id="login">
        <div id="triangle"></div>
        <h1>Log in</h1>
        <form action="" method="POST" name="login">
            <input type="text" name="username" placeholder="Username" required />
            <input type="password" name="password" placeholder="Password" required />
            <input name="submit" type="submit" value="login" />
        </form>
    </div>
    <?php } ?>
    </body>
</html>
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
  • 1
    Are you sure `$rows == 1` on your server? – Pierre-Loup Pagniez Jan 12 '16 at 09:48
  • If anything, make sure there is **no output whatsoever** before or after the location header and put a `die()` after it.... Your `sessions_start()` should also not work, because you already have output before the statement... – Raphioly-San Jan 12 '16 at 09:55

4 Answers4

1

If anything, make sure there is no output whatsoever before or after the location header and put a die() after it.... Your sessions_start() should also not work, because you already have output before the statement...

So your session start and your location header should preceed the HTML.

Take a look at what I changed:

<?php
        require('db.php');

        session_start();

        // If form submitted, insert values into the database.
        if (isset($_POST['username'])){
            $username = $_POST['username'];
            $password = $_POST['password'];

            $username = stripslashes($username);
            $username = mysqli_real_escape_string($connection, $username);

            $password = stripslashes($password);
            $password = mysqli_real_escape_string($connection, $password);

            $query = "SELECT * FROM `users` WHERE username='$username' and password='$password'";
            $result = mysqli_query($connection, $query) or die(mysql_error());
            $rows = mysqli_num_rows( $result);
            if($rows==1){
                $_SESSION['username'] = $username;
                header("Location: welkom.php");
                die();
            }
<html>
    <head>
        <meta charset="utf-8">
        <title>Login</title>
        <link rel="stylesheet" href="css/login.css" />
    </head>
    <body>
    <?php
        if($rows!=1){
                echo "<div id='cancel'><h3>Username/password is incorrect.</h3><br />Click here to <a href='index.php'>try again</a></div>";

        }else{   
    ?>
    <div id="login">
        <div id="triangle"></div>
        <h1>Log in</h1>
        <form action="" method="POST" name="login">
            <input type="text" name="username" placeholder="Username" required />
            <input type="password" name="password" placeholder="Password" required />
            <input name="submit" type="submit" value="login" />
        </form>
    </div>
    <?php } ?>
    </body>
</html>
Raphioly-San
  • 403
  • 3
  • 10
0

You can't use header("Location: welkom.php"); where you are because headers have already been sent.

Either load all of the HTML in the page before that into a variable and then echo it once your code has come to that condition, or turn on Output Buffering.

Alternatively, re-order your code to check this condition and redirect before you send anything to the browser.

Geoff Atkins
  • 1,693
  • 1
  • 17
  • 23
0

Remove this :

header("Location: welkom.php");

And try this :

echo '<script>window.location.href = "welkom.php";</script>' ;

user2460074
  • 1,252
  • 3
  • 11
  • 28
-1

Use full url in header location from i.e. header("Location: httP://yoursite.com/welkom.php"); that will work.

Ajay Makwana
  • 2,330
  • 1
  • 17
  • 32