S. Marquez
S. Marquez

Reputation: 11

PHP - Session variable unset while doing header()

I've been doing a deep research in the web, looking for a solution for this error, but I couldn't fix it yet. Let me introduce you the situation:

SCRIPT1.php:

if(isset($_SESSION['LoadFile'])){
    echo "<span>";
    $MsgId = 91;
    $Msg = GetMsg($language, $MsgId);
    echo $Msg . " ";
    echo "<input name=\"Fleet\" id=\"Fleet\" type=\"file\" />";
    echo "</span>";
}else{
echo "<span>";
    $MsgId = 169;
    $Msg = GetMsg($language, $MsgId);
    echo $Msg . " ";
    $MsgId = 22;
    $Msg = GetMsg($language, $MsgId);
    echo "<a href=\"".$linkpath."inc_load_file.php\">".$Msg."</a>";
echo "</span>";
}

The inc_load_file.php contents:

<?php
$_SESSION["LoadFile"] = "Load, please";
$_SESSION["ShowLoadingOpt"] = "Show";
//echo $_SESSION["LoadFile"] <---This works correctly, the variable is being properly created.
header("Location: ".$_SESSION["PrevLoc"]);
exit;
?>

Note that if I do print_r($_SESSION) in the begging of the Script1, it shows the following result:

Array ( [UID] => 1 [UName] => Santi [USurname] => Márquez [ULvl] => 1 [ULang] => en_Gb [Logged] => 1 [UEmail] => [email protected] [User] => smarquez [UCreationDate] => 2015-10-14 [UAccessTimes] => 162 [UALDesc] => Administrator [CountryCodeKPIReportsGenerator] => 2 )

My webpage should show a message like:

"File loaded. If you want to load a new file click here."

If you click in the link, the inc_load_file.php is executed. It creates the session variables and goes back to the script1.php. After doing it, the webpage should show a file input, but actually, it's still showing the link. The session variable is not set.

Can you please help me to check why the session variables created in inc_load_file.php are not being properly returned after executing the header() function?

Thank you very much in advance for your help.

Upvotes: 0

Views: 672

Answers (2)

S. Marquez
S. Marquez

Reputation: 11

Thank you for your answers :) I'm currently using wamp and the session is starting automatically. It's correctly started, that's why I'm able to save all this data in the $_SESSION array:

[UID] => 1
[UName] => Santi 
[USurname] => Márquez 
[ULvl] => 1 
[ULang] => en_Gb 
[Logged] => 1 
[UEmail] => [email protected] 
[User] => smarquez 
[UCreationDate] => 2015-10-14 
[UAccessTimes] => 162 
[UALDesc] => Administrator 
[CountryCodeKPIReportsGenerator] => 2

So, the problem is not the session creation. It's properly created, but $_SESSION["LoadFile"] and $_SESSION["ShowLoadingOpt"] are being unset after the Header().

By the other hand, $_SESSION["PrevLoc"] contents the previous page where the user has been. I deleted some variables from the print_r result due a security reason. Anyways, I tried using $_SERVER['HTTP_REFERER'] too but it didn't work neither.

Thank you very much again for all your support :)

Upvotes: 1

BeetleJuice
BeetleJuice

Reputation: 40916

  1. Add session_start() at the beginning of your script, before you try to access $_SESSION and before you output anything to the browser.
  2. Where is $_SESSION["PrevLoc"] set? You use it in the Location header, but we don't see you set its value.

Upvotes: 1

Related Questions