如何基于会话在php中发布另一行数据?

时间:2017-01-21 05:06:17

标签: php sql

我有这样的登录代码:

<?php
if(isset($_POST['login'])){
    include("koneksi.php");

    $username   = $_POST['username'];
    $password   = md5($_POST['password']);
    $level      = $_POST['level'];

    $query = mysqli_query($koneksi, "SELECT * FROM users WHERE username='$username' AND password='$password'");
    if(mysqli_num_rows($query) == 0){
        echo '<div class="alert alert-danger">Upss...!!! Login gagal.</div>';
    }else{
        $row = mysqli_fetch_assoc($query);

        if($row['level'] == 1 && $level == 1){
            $_SESSION['username']=$username;
            $_SESSION['level']='admin';
            header("Location: user.php");
        }else if($row['level'] == 2 && $level == 2){
            $_SESSION['username']=$username;
            $_SESSION['level']='dosen';
            header("Location: user.php");
        }else if($row['level'] == 3 && $level == 3){
            $_SESSION['username']=$username;
            $_SESSION['level']='mahasiswa';
            header("Location: user.php");
        }else{
            echo '<div class="alert alert-danger">Upss...!!! Login gagal.</div>';
        }
    }
}
?>

我想在user.php文件中根据会话发布用户名而不是用户名,我该怎么办?

这就是user.php:

<div class="login">
    <p>Login Succes</p>
    <p>Username: <?php echo $_SESSION['username']; ?><br>
    Level: <?php echo $_SESSION['level']; ?></p>
    <p><a href="logout.php" class="btn btn-primary" onclick="return confirm('are you sure?')">Log out</a></p>
</div>

1 个答案:

答案 0 :(得分:1)

假设用户的名称存储在名为name的数据库列中,您可以将此值保存到会话变量并将其显示给用户,如下所示:

登录代码:

<?php
if(isset($_POST['login'])){
    include("koneksi.php");

    $username   = $_POST['username'];
    $password   = md5($_POST['password']);
    $level      = $_POST['level'];

    $query = mysqli_query($koneksi, "SELECT * FROM users WHERE username='$username' AND password='$password'");
    if(mysqli_num_rows($query) == 0){
        echo '<div class="alert alert-danger">Upss...!!! Login gagal.</div>';
    }else{
        $row = mysqli_fetch_assoc($query);

        if($row['level'] == 1 && $level == 1){
            $_SESSION['username']=$username;
            $_SESSION['level']='admin';
            $_SESSION['name']=$row['name'];
            header("Location: user.php");
        }else if($row['level'] == 2 && $level == 2){
            $_SESSION['username']=$username;
            $_SESSION['level']='dosen';
            $_SESSION['name']=$row['name'];
            header("Location: user.php");
        }else if($row['level'] == 3 && $level == 3){
            $_SESSION['username']=$username;
            $_SESSION['level']='mahasiswa';
            $_SESSION['name']=$row['name'];
            header("Location: user.php");
        }else{
            echo '<div class="alert alert-danger">Upss...!!! Login gagal.</div>';
        }
    }
}
?>

显示代码:

<div class="login">
    <p>Login Succes</p>
    <p>Username: <?php echo $_SESSION['username']; ?><br>
    Name: <?php echo $_SESSION['name']; ?><br>
    Level: <?php echo $_SESSION['level']; ?></p>
    <p><a href="logout.php" class="btn btn-primary" onclick="return confirm('are you sure?')">Log out</a></p>
</div>

现在,如果用户的名称实际存储在2列中,例如first_namelast_name,那么您可以将其保存到会话中,如下所示:

$_SESSION['name']=$row['first_name'].' '.$row['last_name'];