登录的PHP会话身份验证

时间:2017-11-27 10:42:49

标签: php html session

我的网站导航栏应该在登录前后相应地更改会话。但是,会话不起作用,因为它显示登录和注销菜单,这里是我的php登录代码:

<?php
session_start();
include 'dbh.php';

$login_email = $_POST ['login_email'];
$login_pwd = $_POST ['login_pwd'];

$sql_login = "select * from userdata where email='$login_email' and 
pwd='$login_pwd'";

$check_user= mysqli_query($conn, $sql_login);

if($row = mysqli_fetch_assoc($check_user)){
    $_SESSION['id']= $row['id'];
    $_SESSION['authenticated']=true;

    header("Location: testing.html");
}else{
    echo "Your email or password is incorrect"; 
}

mysqli_close($conn);

HTML(包括登录和注册下拉菜单):

<?php
if(isset($_SESSION['authenticated']))
{
    echo '
    <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" href="#"><b>Admin</b><span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a href="logout.php">Logout</a></li>
      </ul>
    </li>';
}else{
    echo '
    <li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown"><b>LOGIN</b><span class="caret"></span></a>
        <ul id="login-dp" class="dropdown-menu">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#Login" data-toggle="tab">Login</a></li>
                <li><a href="#Registration" data-toggle="tab">Registration</a></li>
                <div class = "tab-content">
                <div class="tab-pane active" id="Login">
                    <b><em>Listen to the voice of Soul</em></b>
                    <form class="form" role="form" method="post" action="login.php" accept-charset="UTF-8"id="login-nav">
                        <div class="form-group">
                            <label class="sr-only" for="email1">Email address</label>
                            <input type="email" class="form-control"id="email1" name="login_email" placeholder="Email address" required>
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="pwd1">Password</label>
                            <input type="password" class="form-control" id="pwd1" name="login_pwd" placeholder="Password" required>
                            <div class="help-block text-right"><a href="">Forget the password?</a></div>
                        </div>  
                        <div class="form-group">
                            <input type="submit" name="loginBtn" class="btn btn-primary btn-block">Sign in</button>
                        </div>
                        <div class="checkbox">
                            <label><input type="checkbox">Keep me logged-in</label>
                        </div>
                    </form>
                </div>
                <div class="tab-pane" id="Registration">
                    <b><em>Listen to the voice of Soul</em></b>
                    <form class="form" role="form" method="POST" action="signup.php" accept-charset="UTF-8"id="signup-nav">
                        <div class="form-group">
                            <label class="sr-only" for="email2">Email address</label>
                            <input type="email" class="form-control"id="email2" name="signup_email" placeholder="Email address" required>
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="password2">Password</label>
                            <input type="password" class="form-control" id="pwd2" name="signup_pwd" placeholder="Password" required>
                        </div>  
                        <div class="form-group">
                            <label class="sr-only" for="password3">Confirm Password</label>
                            <input type="password" class="form-control" id="pwdcon" name="signup_pwdcon" placeholder="Confirm Password" required>
                        </div>  
                        <div class="form-group">
                            <input type="submit" name="signUpBtn" value="Sign Up" class="btn btn-primary btn-block"></button>
                        </div>
                    </form>
                </div>  
                </div>
            </ul>
        </ul>
    </li>';
}
?>

我是php和html的新手,是因为错误的echo标签?

2 个答案:

答案 0 :(得分:0)

如果它同时输出ifelse条件,可能不会将其解释为php。

尝试使用php文件("Location: testing.php"

,而不是重定向到HTML文件

答案 1 :(得分:0)

请务必将session_start()添加到HTML页面的顶部,并确保if语句使用正确的比较类型:

if ($_SESSION['authenticated'] == true){

另外注意,你的sql语句很容易被sql注入,你应该使用php prepared statements或者至少清理变量$_POST中的数据