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)

7
  • pls try a echo $_SESSION['username']; before the if and see what it gives you. Commented Jul 7, 2015 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... Commented Jul 7, 2015 at 14:13
  • Your "third" code is NOT working fine. It's IMPOSSIBLE for it to work fine. <p> counts as output, therefore session_start() will fail with headers already sent Commented Jul 7, 2015 at 14:22
  • @JimVercoelen: if it is working, then the only possible explanation is that you've got output buffering enabled... Commented Jul 7, 2015 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? Commented Jul 7, 2015 at 14:31

1 Answer 1

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

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

1 Comment

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

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.