【问题标题】:How can i redirect my user and admin to different page如何将我的用户和管理员重定向到不同的页面
【发布时间】:2019-04-05 06:29:31
【问题描述】:

如何将我的用户和管理员重定向到不同的页面,我尝试使用不同的在线方法但不起作用。如果您能帮助找出我的代码中的错误或错误,我会很高兴。谢谢

检查代码并帮助我解决我遇到的错误

在成功登录到管理员或会员页面后,我正在尝试重定向用户。我只能将所有用户重定向到特定页面(“位置:./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!';
}
?>   `

【问题讨论】:

  • 有点难以理解。你想做什么?你的代码有几个错位的else
  • 在成功登录到管理员或成员页面标题后,我正在尝试重定向用户我只能将所有用户重定向到特定页面(“位置:./admin/index.php”);但无法选择管理员用户重定向到管理页面

标签: php mysql forms session passwords


【解决方案1】:
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”);}

【讨论】:

  • 是的.....它只重定向我数据库上的所有用户,但我试图将两个不同的用户重定向到标题(“位置:./admin/index.php”) ;和成员到标题(“位置:./member/index.php”);
  • 那么你需要做更多的工作。你需要数据库保存一个值是管理员还是普通用户,然后你可以写入if语句。如果管理员设置位置,如果用户设置位置...
  • 我创建了一个名为 'status' 的值 ......admin 的值为 '1' 而其他成员的值为 '2' .
  • @Haim 你能解释一下你做了什么你认为可以解决他们的问题吗?
  • 嗯,在选择查询中添加了一个额外的列,如果密码匹配,则根据该列的结果设置另一个位置
【解决方案2】:

终于明白了......我删除了 $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 四十九

谢谢大家

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2023-04-01
    • 2016-04-20
    • 2017-04-06
    • 1970-01-01
    • 2014-05-01
    相关资源
    最近更新 更多