Php 简单会话示例不起作用

Php 简单会话示例不起作用,php,session,if-statement,Php,Session,If Statement,我写了这个小会话脚本,我发现它不起作用 <?php session_start(); $username = $_SESSION["username"]; ?> <html><head> <title>Simple Session Example</title> </head> <body> <? if(isset($_SESSION['username'])) { $

我写了这个小会话脚本,我发现它不起作用

<?php
  session_start();
  $username = $_SESSION["username"];
?>

  <html><head>
  <title>Simple Session Example</title>
  </head>
  <body>

<?

  if(isset($_SESSION['username'])) {
  $username = $_POST["username"];
  echo "<h3>Hello $username</h3>";
  }

  else {
  echo "<h3>OUT!</h3>"; 
  echo "<form action='index.php' method='POST'>";
  echo "<input type='text' name='username'>";
  echo "<input type='submit' name='submit' value='Submit'></form>";
  }

  ?>
我不知道哪一部分错了。看起来对m很好,但肯定是出了什么问题。

像这样的事情

    <?php
      session_start();
      if(isset($_SESSION['username']))
      $username = $_SESSION["username"];
    ?>

      <html><head>
      <title>Simple Session Example</title>
      </head>
      <body>

    <?

      if(isset($_POST['username'])) {
      $username = $_POST["username"];
      $_SESSION["username"] = $_POST["username"];
      echo "<h3>Hello $username</h3>";
      }

else if($username) {
      echo "<h3>Hello $username</h3>";
      }

      else {
      echo "<h3>OUT!</h3>"; 
      echo "<form action='index.php' method='POST'>";
      echo "<input type='text' name='username'>";
      echo "<input type='submit' name='submit' value='Submit'></form>";
      }

      ?>
像这样的

    <?php
      session_start();
      if(isset($_SESSION['username']))
      $username = $_SESSION["username"];
    ?>

      <html><head>
      <title>Simple Session Example</title>
      </head>
      <body>

    <?

      if(isset($_POST['username'])) {
      $username = $_POST["username"];
      $_SESSION["username"] = $_POST["username"];
      echo "<h3>Hello $username</h3>";
      }

else if($username) {
      echo "<h3>Hello $username</h3>";
      }

      else {
      echo "<h3>OUT!</h3>"; 
      echo "<form action='index.php' method='POST'>";
      echo "<input type='text' name='username'>";
      echo "<input type='submit' name='submit' value='Submit'></form>";
      }

      ?>

你有一个逻辑错误。检查会话值是否存在,但尝试使用post变量。它们不相关。我删除了post变量,但仍然不起作用。您有一个逻辑错误。检查会话值是否存在,但尝试使用post变量。它们不相关。我删除了post变量,但仍然不起作用。