会话开始不'开始'

时间:2016-10-03 09:44:34

标签: php mysql session login

这个问题真的让我很难过。我有4个文件,一个用于DB配置,另一个用于登录页面,第三个用于登录处理,lsat一个用于重定向成功登录后)  这是我的db-connect.php:

<?php

        $servername = "localhost";
        $username = "dbroot";
        $password = "dbpassword";
        $dbname = "dbname";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }

        ?>

我的user-login.php中的表单:

<form action="includes/login-process.php" id="login" class="formoid-metro-black" style="background-color:transparent;font-size:14px;font-family:'Open Sans','Helvetica Neue','Helvetica',Arial,Verdana,sans-serif;color:#FFFFFF;max-width:400px;min-width:150px; float:right;" method="post" enctype="multipart/form-data"><div class="title"><h2>Log In</h2></div>
            <div class="element-input"><label class="title">Username<span class="required">*</span></label><input class="large" type="text" name="username" required="required"/></div>
            <div class="element-password"><label class="title">Password<span class="required">*</span></label><input class="large" type="password" name="password" value="" required="required"/></div>
            <div class="submit"><input type="submit" name="submit" value="Log In"/></div></form><script type="text/javascript" src="forms/sign-up-form_files/formoid1/formoid-metro-black.js"></script>

login-process.php:

<?php include ("db-connect.php");

if(isset($_POST['submit'])){

  $username = mysqli_real_escape_string($conn, $_POST['username']);
  $password = mysqli_real_escape_string($conn, $_POST['password']);

  $encrypt = md5($password);

  $userquery = "SELECT * FROM user WHERE `user-username`='$username' AND `user-password`='$password'";

  $run = mysqli_query($conn,$userquery); 

  if(mysqli_num_rows($run)>0){

    $_SESSION['username']=$username;
    ?>
    <script>alert('Login successful.')</script>
    <script>window.open('../user-profile.php','_self')</script>
    <?php
  }
  else {
    ?>
    <script>alert('Username or Password Incorrect.','_self')</script>
    <script>window.open('../user-login.php','_self')</script>
    <?php

  }
}


?>

在我的登录页面中,在我成功登录后,通过脚本警报成功证明,但是,当user-profile.php打开时(我将session_start()置于顶部),它直接运行条件在if语句中,忽略了我已经成功登录的事实。 这是我的user-profile.php:

    <!DOCTYPE html>
      <?php  
      session_start();
      if(!isset($_SESSION['username'])){
        ?> 
        <script> alert('Please Log-in first.','_self')</script>
        <?php include_once('user-login.php'); 
      }
      else {
        $first_name= $_SESSION['fname'];
        $first_name= ucfirst($first_name);
        ?>
   //rest of my code

上面的代码应该运行else语句中的内容,但我会继续先获取登录信息。 (只需忽略$ firstname代码,我想在下面输出它)。你们有什么想法吗?如果是这样,pleeeease帮助我。谢谢!

2 个答案:

答案 0 :(得分:1)

在login-process.php中,在将值设置为会话变量之前,必须先session_start()

答案 1 :(得分:1)

您正在直接访问会话而不声明会话。用户session_start()在访问会话变量之前,您可以在访问完成后使用session_close()