Session Variable showing undefined
<p>I have made the login page using ionic and it seems to work well. The $_SESSION variable also gets displayed after successfull login. Then in another php file, when I try to print the same session variable, it shows undefined or not set. Also, the session expires after refresh and it shows an alert to login again</p>

<pre><code>Here is my login php code

    &lt;?php
ob_start();
session_start(); 
$errmsg_arr = array();
$errflag = false;

if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }

$errors = array();
$data = array();

// Getting posted data and decodeing json
$_POST = json_decode(file_get_contents('php://input'), true);

require_once 'db_functions.php';
$db = new db_functions();


if(empty($_POST['doc_key']))
{
  $data['errors'] = 'Please enter all the credentials';
  echo json_encode($data);
}
else if(empty($_POST['password']))
{
  $data['errors'] = 'Please enter all the credentials';
  echo json_encode($data);
}
else
{    
    $doc_key = $_POST['doc_key'];
    $password = $_POST['password'];
    $user = $db-&gt;getDoctorByEmailAndPassword($doc_key, $password);
    if( $user == true)
    {
        //session_regenerate_id();
        $_SESSION['name'] = $user["name"];
        $_SESSION['contact'] = $user["contact"];
        $_SESSION['email'] = $user["email"];
        $_SESSION['license_no'] = $user["license_no"];
        $_SESSION['type'] = $user["type"];
        $_SESSION['gender'] = $user["gender"];
        $_SESSION['location'] = $user["location"];
        $_SESSION['fees'] = $user["fees"];
        $_SESSION['experience'] = $user["experience"];
        $_SESSION['doc_key'] = $user["doc_key"];
        //session_write_close();
        $data['message'] = $_SESSION['name'];// "User logged in successfully";
        echo json_encode($data);
    }

    else
    {
      $data['errors'] = 'Login Credentials are invalid';
      echo json_encode($data);    
    }
}
?&gt;
</code></pre>

<p>This is the code of other page where I want to display the session variable</p>

<pre><code>    &lt;?php
ob_start();
session_start();
if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }
$data = array();
$type_user = "";
if(isset($_SESSION['user_name']) || isset($_SESSION['license_no']))
{   
    if($type_user == "doctor")
    {
        $data["name"] = $_SESSION["name"];
        $data["email"] = $_SESSION["email"];
        $data["contact"] = $_SESSION["contact"];
        $data["license_no"] = $_SESSION["license_no"];
        $data["doc_key"] = $_SESSION["doc_key"];
        $data["gender"] = $_SESSION["gender"];
        $data["type"] = $_SESSION["type"];
        $data["location"] = $_SESSION["location"];
        $data["fees"] = $_SESSION["fees"];
        $data["experience"] = $_SESSION["experience"];
        echo json_encode($data);
    }
    else
    {
        $data["name"] = $_SESSION["name"];
        $data["email"] = $_SESSION["email"];
        $data["contact"] = $_SESSION["contact"];
        $data["gender"] = $_SESSION["gender"];
        $data["user_name"] = $_SESSION["user_name"];
        echo json_encode($data);
    }

}
else
{
    $data["errors"] = "Please login first to see this";
    echo json_encode($data);
}
?&gt;
</code></pre>

<p>This page does not return the session data to controller. It shows blank
Here are the codes of controllers and ionic code</p>

<p>Controller for login page</p>

<pre><code>.controller('doctorloginCtrl', function($scope,$http,$window) {
    $scope.doctor = {};
    $scope.loginDoc = function(){
        $http({
        method: 'POST',
        url: 'http://localhost/drmedic/login_doctor.php',
        data: $scope.doctor,
        headers: {'ContentType': 'application/x-www-form-urlencoded'}
        })
        .success(function(data){
            if(data.errors)
            {
                alert(JSON.stringify(data.errors));
            }
            else
            {
                alert(JSON.stringify(data.message));
                $window.location.href = "#/home";
            }
        });
    }
})
</code></pre>

<p>Controller for the other page where I want to show the details</p>

<pre><code>.controller('profileCtrl', function($scope,$http,$ionicSideMenuDelegate,$window) {
    $ionicSideMenuDelegate.toggleLeft();
    $http({method: 'GET', url: 'http://localhost/drmedic/retrieve_login_details.php'}).success(function(data) {
        if(data.errors)
        {
            alert(data.errors);
            $window.location.href = "#/select-role";
        }
        else
        {       
        $scope.contents = data;
        console.log($scope.contents);
        }
    });

})
</code></pre>

<p>After login, when I go to the profile page by typing localhost:8100/#profile, it shows blank.. It does not display the {{contents.name}} field</p>

<p>IONIC CODE for Profile page</p>

<pre><code>&lt;ion-view title="Profile"&gt;
    &lt;ion-content overflow-scroll="true" padding="true" scroll="false" class="has-header"&gt;
        Hi {{contents.name}}
    &lt;/ion-content&gt;
&lt;/ion-view&gt;
</code></pre>

<p>The sessions used to work properly for my other project. Can't figure it out why it isn't working for this one. Is it because of the Authentication-Allow-Cross headers??</p>

<p>Please help.     </p>