-2

guys I'm pretty new for php i'm trying to make a login page.

How can I redirect page if statement is true?

I tried iwth header("location: nextpage.php"); but it doesn't work

<html>
    <head>
        <title>Uploader</title>
    </head>
    <body>

        <form action="index.php" method="POST" enctype="application/x-www-form-urlencoded"> 
        <input id="login" class="username" type="text" name="username" autofocus placeholder="Username" maxlength="30"/>
        <input id="login" class="password" type="password" name="password" placeholder="Password" maxlength="15"/>
        <input id="login" class="submit" type="submit" value="Login" />
        <p>Last update <span id="lastupdate"><?php echo date('d-m-Y');?></span></p>

        <?php
        mysql_connect("localhost", "root","******") or die(mysql_error());
        mysql_select_db("login") or die(mysql_error());

        if(isset($_POST['username']) && isset($_POST['password'])){
            $db_user = mysql_query("SELECT username FROM login_tb");
            $db_pass = mysql_query("SELECT password FROM login_tb");
            $ctrl_user = mysql_result($db_user,0);
            $ctrl_pass = mysql_result($db_pass,0);

            $username = $_POST['username'];
            $password = $_POST['password'];
            echo $username . $password;

            if(($username || $password) == NULL){
                echo "you have entered wrong username or password! <br/> please contact site admin.";
            }else{
                if($username == $ctrl_user && $password == $ctrl_pass){
                header("location: ./nextpage.php");
                }
            }
        }
        ?>
    </body>
</html>

please anyone can help me? thanks

takeItEasy
  • 3,981
  • 2
  • 15
  • 10

3 Answers3

1

A header is just that, something which appears at the beginning of the data transfer. You are sending data (most of the web page) before trying to send the header.

Move your php code to the top of the file, and then just echo the "wrong user/pass" in another php block in the correct location

<?php
    mysql_connect("localhost", "root","******") or die(mysql_error());
    mysql_select_db("login") or die(mysql_error());

    if(isset($_POST['username']) && isset($_POST['password'])){
        $db_user = mysql_query("SELECT username FROM login_tb");
        $db_pass = mysql_query("SELECT password FROM login_tb");
        $ctrl_user = mysql_result($db_user,0);
        $ctrl_pass = mysql_result($db_pass,0);

        $username = $_POST['username'];
        $password = $_POST['password'];

        if(($username || $password) != NULL && $username == $ctrl_user && $password == $ctrl_pass){
            header("location: ./nextpage.php");
        }
    }
?>
<html>
    <head>
        <title>Uploader</title>
    </head>
    <body>

        <form action="index.php" method="POST" enctype="application/x-www-form-urlencoded"> 
        <input id="login" class="username" type="text" name="username" autofocus placeholder="Username" maxlength="30"/>
        <input id="login" class="password" type="password" name="password" placeholder="Password" maxlength="15"/>
        <input id="login" class="submit" type="submit" value="Login" />
        <p>Last update <span id="lastupdate"><?php echo date('d-m-Y');?></span></p>
        <?php
            echo $username . $password;
            if(($username || $password) == NULL){
                echo "you have entered wrong username or password! <br/> please contact site admin.";
            }
        ?>
    </body>
</html>
Andrew Brock
  • 1,374
  • 8
  • 13
  • DUDE, you just left his nonsense code as is – Your Common Sense Apr 29 '12 at 12:29
  • @YourCommonSense He is trying to learn, and while I agree that his code is not the highest of quality, and has security holes bigger than the Chicxulub crater, I gave the solution to his issue without going into a lecture on security or good coding practices. – Andrew Brock Apr 29 '12 at 12:34
  • OMG, it is not "not the highest of quality"! It just doesn't make any sense and will never work. It is not the matter of "security" or "practices". This code will never work. I can't believe you can't see thwt. – Your Common Sense Apr 29 '12 at 12:37
1

There are multiple ways to redirect pages, I like to do it as follows:

<?php
header('Location: /blah.php');
die('<meta http-equiv="refresh" content="0; url=http:/blah.php" />\n
<p>Please visit <a href="/blah.php">Blah.php</a>.</p>');
?>

That way you have a fallback if the header fails.

Also please allow me to point out that you should sanitise your data inputs to prevent SQL injection/XSS. Here:

$user = htmlspecialchars(mysql_real_escape_string($_POST['user']));
mjsa
  • 4,221
  • 1
  • 25
  • 35
-1

It seems output buffering problem, you can use ob_start() and ob_end_fluh()

And following;

<? ob_start(); ?>
<html>
    <head>
        <title>Uploader</title>
    </head>
    <body>

        <form action="index.php" method="POST" enctype="application/x-www-form-urlencoded"> 
        <input id="login" class="username" type="text" name="username" autofocus placeholder="Username" maxlength="30"/>
        <input id="login" class="password" type="password" name="password" placeholder="Password" maxlength="15"/>
        <input id="login" class="submit" type="submit" value="Login" />
        <p>Last update <span id="lastupdate"><?php echo date('d-m-Y');?></span></p>

        <?php
        mysql_connect("localhost", "root","******") or die(mysql_error());
        mysql_select_db("login") or die(mysql_error());

        if(isset($_POST['username']) && isset($_POST['password'])){
            $db_user = mysql_query("SELECT username FROM login_tb");
            $db_pass = mysql_query("SELECT password FROM login_tb");
            $ctrl_user = mysql_result($db_user,0);
            $ctrl_pass = mysql_result($db_pass,0);

            $username = $_POST['username'];
            $password = $_POST['password'];
            echo $username . $password;

            if(($username || $password) == NULL){
                echo "you have entered wrong username or password! <br/> please contact site admin.";
            }else{
                if($username == $ctrl_user && $password == $ctrl_pass){
                header("Location: ./nextpage.php");
                }
            }
        }
        ?>
    </body>
</html>
<? ob_end_flush(); ?>
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73