0

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)

Jim Vercoelen
  • 1,048
  • 2
  • 14
  • 40
  • pls try a `echo $_SESSION['username'];` before the `if` and see what it gives you. – DocRattie Jul 07 '15 at 14:13
  • 3
    Your second page is messed up: It has a php closing tag, followed by another session_start(). Are you sure you are posting the right code? The code you posted could never work... – Erwin Moller Jul 07 '15 at 14:13
  • Your "third" code is **NOT** working fine. It's **IMPOSSIBLE** for it to work fine. `

    ` counts as output, therefore `session_start()` will fail with [headers already sent](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php)

    – Marc B Jul 07 '15 at 14:22
  • @JimVercoelen: if it is working, then the only possible explanation is that you've got output buffering enabled... – Marc B Jul 07 '15 at 14:29
  • "I added this code to the first page" - you mean the first page that contains php code - which you called "second page" in your post? – VolkerK Jul 07 '15 at 14:31
  • The site you've linked to doesn't seem to implement what you've posted here. After entering the name `g` and submitting the form `start.php?username=g` is requested. On that page the name is shown but the `start` button links to `vraag1.php?name=`; the GET parameter isn't passed there. – VolkerK Jul 07 '15 at 14:36
  • "On that page the name is shown" – VolkerK Jul 07 '15 at 14:42

1 Answers1

1

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().

Mojtaba
  • 4,852
  • 5
  • 21
  • 38
  • Ah thanks, i removed the second session_start on the same page, didnt know that. My post wasn't that good, the php tags are there now sorry for that one – Jim Vercoelen Jul 07 '15 at 14:35