Как я могу перенаправить моего пользователя и администратора на другую страницу - PullRequest
0 голосов
/ 01 ноября 2018

Как я могу перенаправить моего пользователя и администратора на другую страницу, я пробовал использовать другой онлайн-метод, но не работает. Я хотел бы, если вы можете помочь найти ошибки или ошибки в моем коде. спасибо

проверьте код и помогите мне решить ошибку, я получаю

Я пытаюсь перенаправить пользователя после успешного входа на страницу администратора или пользователя. я могу только перенаправить всех пользователей на определенную страницу («Местоположение: ./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!';
}
?>   `

Ответы [ 2 ]

0 голосов
/ 01 ноября 2018

Наконец-то получил ...... я удаляю оператор if для $ status и вставляю его в конце подтверждения входа в систему. проверьте мой предыдущий код.

Назад

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!';
}
?>   

ОБНОВЛЕНИЕ: РАБОТА, КАК МАГИЯ

СПАСИБО ВСЕМ, КТО ВКЛАДИТСЯ @ Хаим @ ​​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');
    }
    ?>
0 голосов
/ 01 ноября 2018
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”);}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...