簡體   English   中英

我如何將我的用戶和管理員重定向到其他頁面

[英]How can i redirect my user and admin to different page

我如何將我的用戶和管理員重定向到其他頁面,我嘗試使用其他在線方法但不起作用。 我希望您能幫忙找出我代碼中的錯誤或錯誤。 謝謝

檢查代碼並幫助我解決錯誤

成功登錄到管理員或成員頁面后,我正在嘗試將用戶重定向。 我只能將所有用戶重定向到特定頁面(“位置:./ admin / index.php”); 但無法將管理員用戶設置為重定向到管理員頁面

        session_start();
    // Change this to your connection info.
    $DB_HOST = 'localhost';
    $DB_USER = 'root';
    $DB_PASS = '';
    $DB_NAME = 'schoolexamdatabase';


    // Try and connect using the info above.
        $con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
        if ( mysqli_connect_errno() ) {
            // If there is an error with the connection, stop the script and display the error.
            die ('Failed to connect to MySQL: ' . mysqli_connect_error());
        }
// Now we check if the data was submitted, isset will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
    // Could not get the data that should have been sent.
    die ('Username and/or password does not exist!');
}
// Prepare our SQL 
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute(); 
    $stmt->store_result(); 


    // Store the result so we can check if the account exists in the database.
    if ($stmt->num_rows > 0) {
        $stmt->bind_result($id, $password);
        $stmt->fetch();      
        // Account exists, now we verify the password.
        if (password_verify($_POST['password'], $password)) {
            // Verification success! User has loggedin!
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $_POST['username'];
            $_SESSION['id'] = $id;

        } else {

                      header ("Location: ./admin/index.php");
        }

    } else {
        echo 'Incorrect username and/or password!';
    }
    $stmt->close();
} else {
    echo 'Could not prepare statement!';
}
?>   `

終於明白了……我刪除了$ status的if語句,並在登錄驗證的末尾粘貼了。 檢查我以前的代碼。

以前

session_start();
    // Change this to your connection info.
    $DB_HOST = 'localhost';
    $DB_USER = 'root';
    $DB_PASS = '';
    $DB_NAME = 'schoolexamdatabase';


    // Try and connect using the info above.
        $con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
        if ( mysqli_connect_errno() ) {
            // If there is an error with the connection, stop the script and display the error.
            die ('Failed to connect to MySQL: ' . mysqli_connect_error());
        }
// Now we check if the data was submitted, isset will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
    // Could not get the data that should have been sent.
    die ('Username and/or password does not exist!');
}
// Prepare our SQL 
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute(); 
    $stmt->store_result(); 


    // Store the result so we can check if the account exists in the database.
    if ($stmt->num_rows > 0) {
        $stmt->bind_result($id, $password);
        $stmt->fetch();      
        // Account exists, now we verify the password.
        if (password_verify($_POST['password'], $password)) {
            // Verification success! User has loggedin!
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $_POST['username'];
            $_SESSION['id'] = $id;

        } else {

                      header ("Location: ./admin/index.php");
        }

    } else {
        echo 'Incorrect username and/or password!';
    }
    $stmt->close();
} else {
    echo 'Could not prepare statement!';
}
?>   

更新:像魔術一樣工作

感謝所有世衛組織的貢獻@ Haim @Funk Forty Niner

感謝大伙們

<?php
    session_start();
    // Change this to your connection info.
    $DB_HOST = 'localhost';
    $DB_USER = 'root';
    $DB_PASS = '';
    $DB_NAME = 'schoolexamdatabase';


    // Try and connect using the info above.
    $con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
    if ( mysqli_connect_errno() ) {
        // If there is an error with the connection, stop the script and display the error.
        die ('Failed to connect to MySQL: ' . mysqli_connect_error());
    }


    // Now we check if the data was submitted, isset will check if the data exists.
    if ( !isset($_POST['username'], $_POST['password']) ) {
        // Could not get the data that should have been sent.
        die ('Username and/or password does not exist!');
    }


    // Prepare our SQL 
    if ($stmt = $con->prepare('SELECT id, password, status FROM accounts WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute(); 
    $stmt->store_result(); 


    // Store the result so we can check if the account exists in the database.
    if ($stmt->num_rows > 0) {
        $stmt->bind_result($id, $password, $status);
        $stmt->fetch();      
        // Account exists, now we verify the password.
        if (password_verify($_POST['password'], $password)) {
            // Verification success! User has loggedin!
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $_POST['username'];
            $_SESSION['id'] = $id;




                //echo 'Welcome ' . $_SESSION['name'] . '!';
            } else {
                echo 'Incorrect username and/or password!';
            }
        } else {
            echo 'Incorrect username and/or password!';
        }
        $stmt->close();
    } else {
        echo 'Could not prepare statement!';
    }
    if ($status == 1) {

                      header ('Location: ./admin/index.php');
        }

    elseif ($status == 2) { 


            header('location: ./members/index.php');
    }
    ?>
if (password_verify($_POST['password'], $password)) {
        // Verification success! User has loggedin!
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $_POST['username'];
        $_SESSION['id'] = $id;

    // this else doesn’t belong here
    //} else {

                  header ("Location: ./admin/index.php");
    }

更新

if ($stmt = $con->prepare('SELECT id, password, status FROM accounts WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bind_param('s', $_POST['username']);
$stmt->execute(); 
$stmt->store_result(); 


// Store the result so we can check if the account exists in the database.
if ($stmt->num_rows > 0) {
    $stmt->bind_result($id, $password, $status);
    $stmt->fetch();      
    // Account exists, now we verify the password.
    if (password_verify($_POST['password'], $password)) {
        // Verification success! User has loggedin!
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $_POST['username'];
        $_SESSION['id'] = $id;

    //} else {
if ($status == 1) {

                  header ("Location: ./admin/index.php");
    }

elseif ($status == 2) { header(“location: ./members/index.php”);}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM