简体   繁体   中英

php redirect to a different page trouble

I am having difficulty with header redirecting me to index.html from my login page. I'm not sure header is even the way to go but it is all that I could find on page redirection.

This code is just a simple login page which checks the username and password against a MySQL database.

The problem I am having is inside the if statement:

 if(mysqli_num_rows($res) == 1){
        header("Location: index.php");
        exit();
 }

full code for login.php:

<?php 
        //Connects to MySQL database using sql_connect.php
        require "sql_connect.php";
     ?>

    <?php 
        if(isset($_POST['username'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $sql = "SELECT * FROM login_test WHERE username='".$username."' AND password='".$password."' LIMIT 1";
            $res = mysqli_query($connection, $sql);
            if(mysqli_num_rows($res) == 1){
                header("Location: index.php");
                exit();
            } else {
                echo "Invalid login information!";
                exit();
            }
        }
     ?>

    <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>login</title>
            <link rel="stylesheet" href="../style.css">
        </head>
        <body>
            <form name="login" method="post" action="login.php">
                <h3>Login</h3>
                <p>Username: </p><input type="text"name="username">
                <p>Password: </p><input type="password" name="password">
                <br/>
                <input type="submit"name="submit"value="log in">
            </form>
            <script src="../script.js" type="text/javascript"></script>
        </body>
    </html>

While I'm sure the rest of my code is far from perfect I am just really looking at how to redirect to a new page if the user is authenticated in the database? Is there a better option than header in my case or am I just implementing it wrong?

I used php header before, but have bad experience.
Normally I use javascript

 echo '<script type="text/javascript">' . "\n";
if(isset($from))
 {
    echo 'window.location="..";';
 }
 else
 {
    echo 'window.location="..";';
 }
 echo '</script>';

header() function didn't work well may cause by your "mysqli_num_rows($res) == 1" result is false.

And There is no space between "Location" and your "index.php" like: header("Location:index.php");

Or you can create a php function and use JavaScript, its a good way to avoid that.

function redirect($url, $prompt) {
    $js_string = "<script>";
    $js_string .= $prompt ? "alert('". $prompt ."');" : "";
    $js_string .= "window.location = '{$url}';";
    $js_string .= "</script>";
    exit($js_string);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM