0

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. The user is required to insert their name and submit the form. The username should be saved and passed onto setup1.php.

This is part of my 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:

<?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. I would appreciate if you could help.

3
  • 3
    use $_SESSION not %_SESSION Commented Apr 7, 2015 at 13:47
  • No need to session_start twice (or is this intentionally). Commented Apr 7, 2015 at 13:51
  • Sorry, this was a typo, I just changed it. I learned that I'd have to include session_start(); at the beginning of each file. Commented Apr 7, 2015 at 14:00

3 Answers 3

5

Your $_POST code is in the wrong file. Your form is going to setup1.php, but you're trying to set the $_SESSION in your index.php.

You need to take it out of there and put it in 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. I hope it was just a typo.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for picking that up, @satishrajak. I'm hoping it was just a type on their end. I just copy + pasted it.
die("No $_POST data"); - this will generate Notice: Array to string conversion - use single quotes not to evaluate this $_POST array
Was meant to put single quotes! @n-dru
Sorry, it was a typo!
0

Your form hasn't been submitted when you set the $_SESSION['username'], i.e., $_POST['participant'] has no value.

You should move the piece of code below from index.php to setup1.php

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

Comments

0

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

<?php session_start();

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

1 Comment

Please don't use %_SESSION

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.