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();
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题