PHP Session Problem

When I try to use the Session variable in PHP \$_Session[“VariableName”], it does not work”. The session never get registered.

How to check? You can use the following code to see if the Session is null or not.

<?php
isset($_SESSION["VariableName"]) //If this return false, that mean this session is null
?>

if(isset($_SESSION["VariableName"])){
echo "Session value:" . $_SESSION["VariableName"];
$_SESSION["VariableName"] = "Assigned";
}else{
echo "Session is null";
}
?>

if(isset($_SESSION["VariableName"])){
echo "Session value:" . $_SESSION["VariableName"];
$_SESSION["VariableName"] = "Assigned";
}else{
echo "Session is null";
}
?>

If the result is “Session is null”, once you refresh the page again, it should display “Session value: Assigned”.

If not, that mean the Session feature has not been turn on properly on you server.

Solution 1:
If you are allowed to modify the php.ini file, you can open up the “php.ini” file and Search for the keyword “session.auto_start
To ensure
session.auto_start= 1;
After you edited it, you can restart the web sever and try again.

<p>
  <b>Solution 2:</b><br />You can use the follow code to enable the session feature:</span><br /><span style="font-family:Courier New;"><span style="color:rgb(0,0,0);"><span style="color:rgb(0,0,187);">session_start</span><span style="color:rgb(0,119,0);">();</span></span></span>
</p>

<p>
  <span class="Apple-style-span" style="color:#000000;">Hope you find this useful!</span></span></span></div>

```

Avatar
Chi Yau
Software Developer

Intererest to share knowledge / experience

comments powered by Disqus