dota220141003 2018-08-16 17:15
浏览 92
已采纳

使用登录脚本进行会话时出错

I'm receiving the error Error In Session when I run my login script:

session_start();
if (isset($_POST['login'])) {
    $logusername=$_POST['username'];
    $logpassword=sha1(md5($_POST['password']));
    $redirectLoginSuccess = "dashboard.php";

    $result=mysqli_query($con, "SELECT * FROM users WHERE username='$logusername' AND password='$logpassword'")or die('Error In Session');
    $row=mysqli_fetch_array($result);

    if($result>0){
        $access  = mysqli_fetch_assoc($result,0,'access');
        $userID = mysqli_fetch_assoc($result,0,'id');
        $username = mysqli_fetch_assoc($result,0,'username');
        $name = mysqli_fetch_assoc($result,0,'name');

        //declare two session variables and assign them
        $_SESSION['username'] = $username;
        $_SESSION['userID'] = $userID;
        $_SESSION['name'] = $name;
        $_SESSION['access'] = $access;
    }

        header("Location: " . $redirectLoginSuccess );
}

I receive this error everytime I run the script not sure exactly where the error is. enter image description here

  • 写回答

1条回答 默认 最新

  • dongzhui9936 2018-08-16 18:45
    关注

    The code below works but for some reason, I am not able to echo the session variable name. Is it possible that the session variable not being stored?

    if ( isset( $_POST[ 'login' ] ) ) {
        $errMsg = '';
        // Get data from FORM
        $username = $_POST[ 'username' ];
        $password = sha1($_POST[ 'password' ]);
        if ( $username == '' )
            $errMsg = 'Enter username';
        if ( $password == '' )
            $errMsg = 'Enter password';
        if ( $errMsg == '' ) {
            try {
                $stmt = $connect->prepare( 'SELECT id, name, username, password, access FROM users WHERE username = :username AND password = :password' );
                $stmt->execute( array(
                    ':username' => $username,
                    ':password' => $password
                ) );
                $data = $stmt->fetch( PDO::FETCH_ASSOC );
                if ( $data == false ) {
                    $errMsg = "User $username not found.";
                } else {
                    if ( $password == $data[ 'password' ] ) {
                        $_SESSION[ 'name' ] = $data[ 'fullname' ];
                        $_SESSION[ 'username' ] = $data[ 'username' ];
                        $_SESSION[ 'password' ] = $data[ 'password' ];
                        $_SESSION[ 'access' ] = $data[ 'access' ];
                        header( 'Location: dashboard.php' );
                        exit;
                    } else
                        $errMsg = 'Password not match.';
                }
            } catch ( PDOException $e ) {
                $errMsg = $e->getMessage();
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组