How can I export the variables values from a php file (on the screen) to another file in another directory?

Here it is my problem:

I have

$userName = $_POST[userName];

// I have received this value from the form. Now I want it on the screen:

echo $userName;

// on the screen appears:

Jose

//In the other file in the other directory I have the same variable:

echo $username

// Without having to process the form again in the second file, I want $userName (in the second file in another direcotry) be attributed with the value "Jose" that was defined by the form in the first file .

I don't want to use the include because it doesn't change the filename of the first file I am processing. I want to create a new file .

If I use fwrite, it will write just the code, without the values --or does exist a way to write the variable valuables in the other file? If so, how?

Can somebody help me? Thanks

Update:

Hi People,

Theelitecoder, thank you for your answer.

I feel that I am in the right path, however it didn't work yet.

What I did:

I attributed the variable $userName to the $SESSION['username'] as Theelitecoder suggested.

Then, in the other script, I attributed the $SESSION['username'] to the variable that i had in the script,

$userName:

$userName = $_SESSION['username'];

But, for some reason the variables was not transferred because the output of script didn't change.

Meaining, the username didn't appear in the 2nd script.

Does theelitecoder or somebody else can see what I am doing wrong that it is not working?

Update 2:

Oh, a last question, do I have to start a session in the second script too? Wouldn't it create a new session different of the one is going on and I would never recover the data? Thanksl

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    There are several ways to accomplish this, but the safest and most efficient way to accomplish this is by using sessions.

    Here is a code example:

    $userName = $_POST[userName];

    // I have received this value from the form. Now I want it on the screen:

    echo $userName;

    $SESSION['username'] = $userName;

    _____________________________________________

    Now you may want to access the username variable in another script. You can output it by modifying the echo example shown above with this:

    echo $SESSION['username'];

Still have questions? Get your answers by asking now.