duanmorong9597 2016-01-13 07:58 采纳率: 100%
浏览 1670
已采纳

如何在登录网站后显示用户名?

我正在设计一个网站与登录页面,登录部分和从 db 获取的内容工作正常, 但是我想登录后在主页上显示用户名。我用的 session start ()没有效果,用户名还是没有显示。我查过类似的问题,但都不是我想要的答案。

下面是代码:

 $email = mysqli_real_escape_string($con,$_POST['user']);
$pass = mysqli_real_escape_string($con,$_POST['pass']);
$sel_user = "select * from admins where email='$email' AND password='$pass'";
$run_user = mysqli_query($con, $sel_user);
$check_user = mysqli_num_rows($run_user);
if($check_user>0)
{
$_SESSION['email']=$email
echo "<script>window.open('index2.html','_self')</script>";
}
else {
echo "<script>alert('Email or password is not correct, try again!')</script>";
}

index2.html :

<?php
       session_start();
?>
    <div class="inline-block">
                                <h5 class="no-margin"> Welcome </h5>
                                <h4 class="no-margin"> <?php echo $_SESSION['email']; ?> </h4>
                                <a class="btn user-options sb_toggle">
                                    <i class="fa fa-cog"></i>
                                </a>
                            </div>
  • 写回答

1条回答 默认 最新

  • doutan1637 2016-01-13 08:04
    关注

    Try below code:

    First code:

    session_start();
    $email = mysqli_real_escape_string($con,$_POST['user']);
    $pass = mysqli_real_escape_string($con,$_POST['pass']);
    $sel_user = "select * from admins where email='$email' AND password='$pass'";
    $run_user = mysqli_query($con, $sel_user);
    $check_user = mysqli_num_rows($run_user);
    if($check_user>0)
    {
        $_SESSION['email']=$email
        echo "<script>window.open('index2.html','_self')</script>";
    }
    else {
        echo "<script>alert('Email or password is not correct, try again!')</script>";
    }
    

    index2.php :

    <?php session_start(); ?>
    <div class="inline-block">
        <h5 class="no-margin"> Welcome </h5>
        <h4 class="no-margin"> <?php echo $_SESSION['email']; ?> </h4>
        <a class="btn user-options sb_toggle">
            <i class="fa fa-cog"></i>
        </a>
    </div>
    

    It should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器