显示$ _SESSION中的用户名和用户ID

时间:2013-11-15 18:56:05

标签: php mysql session

我一直无法回复我的$_SESSION['user_name']。我注意到如果我删除“!”来自表单中的if语句,如果确定登录或未确定,则在第二次登录后工作:

if(!isset($_SESSION['LOGIN_STATUS'])

然而,尽管这对我来说没有任何意义,但它也禁用了注销功能,因此我尽量避免这种情况。我也想知道在单独的表单中填写用户名是否存在问题。

我也无法使用$_SESSION['user_id']返回任何值。我尝试了很多东西,包括使用PDO :: FETCH_NUM检查拼写并验证我的表格凭据,但仍然没有。

截至目前,验证适用于用户名和密码,但在当前代码中回显$_SESSION['user_name']$_SESSION['user_id']时未返回任何内容:

PHP:

<?php
session_start();
require_once('php/config.php');

if(isset($_POST['submit'])){
  $username = $_POST['user_name'];
  $password = $_POST['pwd'];

  $stmt = $con->prepare("SELECT * FROM tapp_login WHERE username = ? AND password = ? LIMIT 1");
  $stmt->bind_param('ss', $username, $password);
  $stmt->execute();
  $stmt->bind_result($username, $password);
  $stmt->store_result();
  if($stmt->num_rows == 1){  //To check if the row exists
    $row = $stmt->fetch(PDO::FETCH_NUM); //fetching the contents of the row
    $_SESSION['LOGIN_STATUS']=true;
    $_SESSION['user_id'] = $row['user_id'];
    $_SESSION['user_name'] = $username;              
    echo 'true';  
  }
  else {
    echo 'false';
  }
  $stmt->close();
}
else{
}
$con->close();
?>

FORM:

<body>
<fieldset id="login_form_wrap" class="login_form_header">
  <legend><a href='projects.php'>CUSTOMER LOGIN</a></legend>
  <?php session_start();
  $_SESSION['timeout'] = time();?>
  <div id="profile"> 
  <!--If not logged in-->  
  <?php if(!isset($_SESSION['LOGIN_STATUS']) || ($_SESSION['timeout'] + 600 < time())){?>
    <div id="login_a">
      <a id="login_profile" href="#">click to login</a>
    </div>
  <!--Else if logged in-->
  <?php }else {;?>
  <a href='logout_session.php' id='logout'>Logout</a>
  <!--header('location: dashboard.php');-->
  <?php } ?>
  </div>
  <form action="login_validation.php" id="login_form" method="POST">
    <div id="login_b">
      <div class="welcome"></div>
      <span class="col1">USER NAME:</span>
      <input type="text" id="user_name" name="user_name" class="input" required />
      <span class="col1">PASSWORD:</span>
      <input type="password" id="password" name="password" class="input" required />
      <span class="col1"></span>
      <input type="submit" name"submit" id="login_validation" value="SUBMIT" />
      <input type="reset" id="cancel_hide" value="CANCEL" />
      <div id="add_err"><br></div>
    </div>
  </form>
  <form action="project_validation.php" id="projects_form" method="POST">
  <div id="projects">

    <!-------------------------- Does not work ------------------------->
    <div class="welcome">Welcome <?php echo $_SESSION['user_name'];?><br>
    User ID: <?php echo $_SESSION['user_id'];?></div>  


    <span class="col1">PROJECTS:</span>
    <select id="Projects" name="Projects" class="input">
    <option id="project" name="project" value="" selected="selected" disabled='disabled'>Choose a Project...</option>

    ...

    </select>  
  </div>
  <span class="col1"></span>          
  <input type="submit" name="select" id="project_validation" value="SELECT" />
  <div id="add_err"><br></div> 
  </form>
</fieldset>
</body>

如果你想知道“......”只是代表了更多不必要的形式。

1 个答案:

答案 0 :(得分:0)

来自PHP官方文档:

  

要使用基于cookie的会话,必须在向浏览器输出任何内容之前调用session_start()。

在表单页面中,在将内容发送到输出后调用session_start。你必须在写HTML之前调用session_start。