duankeng1911 2015-07-07 14:08
浏览 13
已采纳

PHP值不从其他页面检索

I have this page I am working on which is a sort of a game. From page to page I am passing the user name like this

index:

<form method="GET" action="start.php">
    <label> Name: </label>
    <input type="text" name="username"><br>
    <input type="submit">
</form>

Where I retrieve the name on the second page like this:

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

-

  <?php
  session_start();
  if(isset($_SESSION['username'])){
     echo "Welcome: " . $_SESSION['username'];
  } 
  else{                 
     echo "Name is unknown";
  }
 ?>

And also on the third:

    <p>
      <?php 
        session_start();
        echo $_SESSION['username']
      ?>
    </p>

And this code is working just fine. Now I was making an if statement which, when ever you don't enter a name, you won't continue to the next page. I added this code to the first page and this is working for the first page.

<?php
  session_start();
  $_SESSION['username'] = $_GET['username'];
  if($_SESSION['username'] != "")
  {
    header("Location: start.php");
  }
?>

So after adding this, you won't go further unless you do enter a name. But by doing this for a reason I don't know yet and couldn't find, the $_SESSION['username'] = $_GET['username']; isn't working and the names are not passed through

This is the link if you like to play: http://i333180.iris.fhict.nl/site (not finished yet)

  • 写回答

1条回答 默认 最新

  • douji4948 2015-07-07 14:27
    关注

    These are your problems:

    1- You don't need to use more than one session_start() in a file. 2- you have closed php tag in the second code and then countinued the PHP coding. 3- before session_start(), it must not send any header or echo anything. In the 3rd code, you have echoed

    tag before session_start(). 4- same as session_start(), you must not send any header or echo anything before calling header function. Be sure in the 4th page, you do not have anything echoed before header().

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

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)