会话变量显示未定义

时间:2016-03-11 12:00:23

标签: javascript php angularjs session ionic-framework

我使用离子制作了登录页面,似乎运行良好。成功登录后,也会显示$ _SESSION变量。然后在另一个php文件中,当我尝试打印相同的会话变量时,它显示未定义或未设置。此外,会话在刷新后到期,并显示再次登录的警报

Here is my login php code

    <?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->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);    
    }
}
?>

这是我要显示会话变量的其他页面的代码

    <?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);
}
?>

此页面不会将会话数据返回给控制器。它显示为空白 以下是控制器和离子代码的代码

登录页面的控制器

.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";
            }
        });
    }
})

我想要显示详细信息的其他页面的控制器

.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);
        }
    });

})

登录后,当我通过输入localhost:8100 / #profile进入个人资料页面时,它显示为空白..它不显示{{contents.name}}字段

简介页面的IONIC CODE

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

以前的会话适用于我的其他项目。无法弄清楚为什么它不适合这个。是因为Authentication-Allow-Cross标题??

请帮忙。

1 个答案:

答案 0 :(得分:0)

使用Javascript localStorage属性。它将数据存储在您的设备中,直到您将其删除。因此,将一些数据保留在设备中非常容易。您可以从代码中的任何位置访问它(如果存在)。

// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");  

有关更多信息,请访问https://www.w3schools.com/jsref/prop_win_localstorage.asp