简体   繁体   English

PHP会话变量未设置

[英]PHP session variable not being set

I am aware that there are several topics about this but after hours of reading I still can't figure out what's going wrong. 据我所知,大概有这一点,但阅读我仍然无法找出什么地方出了错小时后几个主题。

I'm building a survey with a login screen at index.php. 我正在使用index.php的登录屏幕进行调查。 The user is required to insert their name and submit the form. 要求用户输入姓名并提交表格。 The username should be saved and passed onto setup1.php. 用户名应保存并传递到setup1.php。

This is part of my index.php: 这是我的index.php的一部分:

<?php
    session_start();
    print session_id();
?>
<form id="login" method="POST" action="setup1.php">
<input id="participant" name="participant" type="text" size="20"/>
<input type="submit" name="start" value="Start"/>
</form>
<?php
    $name = $_POST['participant'];
    $_SESSION['username'] = $name;
?>

Start of setup1.php: setup1.php的开始:

<?php
    session_start();
    print session_id();
    print_r($_SESSION);
    echo $_SESSION['username'];
?>

My $_SESSION variable is empty, I have nothing printed on the following page setup.php. 我的$ _SESSION变量为空,在以下页面setup.php上没有任何内容。 I would appreciate if you could help. 如果您能提供帮助,我将不胜感激。

Your $_POST code is in the wrong file. 您的$_POST代码位于错误的文件中。 Your form is going to setup1.php , but you're trying to set the $_SESSION in your index.php . 您的表单将进入setup1.php ,但是您正在尝试在index.php设置$_SESSION

You need to take it out of there and put it in setup1.php : 您需要将其取出并放入setup1.php

<?php
  session_start();

  if (!isset($_POST['participant'])) {
    die('No $_POST data');
  }

  $_SESSION['username'] = $_POST['participant'];

  print session_id();
  print_r($_SESSION);
  echo $_SESSION['username'];
?>

Also, make sure that you're using $_SESSION and not %_SESSION . 另外,请确保您使用的是$_SESSION而不是%_SESSION I hope it was just a typo. 我希望这只是一个错字。

Your form hasn't been submitted when you set the $_SESSION['username'] , ie, $_POST['participant'] has no value. 设置$_SESSION['username'] ,您的表单尚未提交,即$_POST['participant']没有任何价值。

You should move the piece of code below from index.php to setup1.php 您应该将下面的代码从index.php移到setup1.php

<?php
$name = $_POST['participant'];
$_SESSION['username'] = $name;
?>

index.php index.php

<?php session_start(); ?>

<form id="login" method="POST" action="setup1.php"> <input id="participant" name="participant" type="text" size="20"/> <input type="submit" name="start" value="Start"/> </form>

setup1.php setup1.php

<?php session_start();

if(isset($_POST['participant']) && ! empty($_POST['participant'])) { $_SESSION['username'] = $_POST['participant']; echo $_SESSION['username']; } if(isset($_POST['participant']) && ! empty($_POST['participant'])) { $_SESSION['username'] = $_POST['participant']; echo $_SESSION['username']; } { $_SESSION['username'] = $_POST['participant']; echo $_SESSION['username']; } { $_SESSION['username'] = $_POST['participant']; echo $_SESSION['username']; } ?>` { $_SESSION['username'] = $_POST['participant']; echo $_SESSION['username']; } ?>`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM