如何从PHP标头获取用户名[关闭]

I can’t figure out how to obtain a user's $username from the php header.

After signing in/signing up, the header contains a variable with the user's username, but I can't figure out how to get that variable back when I want to use it for the profile.php page. Everyone's suggested using $_GET['username'], but for some reason that doesn’t seem to be working. I'm sure it’s obvious, I just can’t figure it out.

Here's my header:

header('location: home.php?user='.$username);

I just want to be able to get that $username variable for my profile.php file so I can echo that username.

UPDATE Retried it based on suggestions, profile php looks as follows:

    <?php if(isset($_GET['user']))  {

    echo $username;

} else {

    echo "Error";
} ?>

still manages to produce an error

If your header is:

header('location: home.php?user='.$username);

Use this to get username:

$user = $_GET['user'];

$_GET array contain parameters from query string. Query string looks like:

param1=foo&param2=bar // etc

So when you want to get parameter from the query string use $_GET['param1'], $_GET['param2'] etc.

Update, because of comments

Your code is a bit wrong:

<?php if(isset($_GET['user']))  {

    echo $username;

} else {

    echo "Error";
} ?>

Firstly, you check if $_GET['user'] is assigned. If yes, you do echo $username;. $username var is undefined — you didn't assign anything to this variable.

You have to use:

<?php if(isset($_GET['user']))  {
    echo $_GET['user'];
} else {
    echo "Error";
} ?>

Or this:

$username = $_GET['user'];
<?php if(isset($username))  {
    echo $username;
} else {
    echo "Error";
} ?>

Your updated code is still wrong since you're checking the existence of $_GET['user']` but echoing an apparently uninitialised variable.

Try this:

<?php if(isset($_GET['user']))  {
  echo $_GET['user'];
} else {
  echo "Error";
} ?>