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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)