使用angular 5时未传递PHP会话变量

时间:2018-05-08 05:56:29

标签: php angular

我正在使用PHP后端开发Angular 5应用程序,用户必须登录才能访问其他组件。我正在使用Wamp,以下是我的login.php页面:

<?php
if(session_status() == PHP_SESSION_NONE){
    error_log("Not started");
    session_start();
}
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Origin: *");

require_once ('./DBConnect.php');
$conn = mysqli_connect($servername, $username, $password, $db ,$port);

$regUserData = json_decode(file_get_contents("php://input"),true);
$response = array();
$email = $regUserData['email'];
$pass = $regUserData['password'];
$pass = md5($pass);

if($email ===''||$pass ===''){
    $response['success'] = FALSE;
    $response['msg'] = 'Please fill all fields';
}
else{
    $query1 = "select * from user where Email = '".$email."'";
    $result1 = $conn->query($query1);
    if(mysqli_num_rows($result1)===0){
        $response['success'] = FALSE;
        $response['msg'] = 'User does not exist';
    }
    else{
        $query2 = "select * from user where Email = '".$email."' and Password = '".$pass."'";
        $result2 = $conn->query($query2);
        $row1 = mysqli_fetch_assoc($result2);
        if(mysqli_num_rows($result2)===0){
            $response['success'] = FALSE;
            $response['msg'] = 'Email or password incorrect';
        }
        else{
            $_SESSION['Id'] = $row1['Id'];
            $response['success'] = true;
        }
    }
}
echo json_encode($response);

使用了2个头语句,因为wamp位于与我的Angular应用程序不同的端口上。这个login.php页面工作正常但是当我尝试将会话变量传递给另一个php页面时,我在error_log文件中得到错误,该错误是&#39; Id&#39;变量未定义。这是我的第二个php页面:

<?php
session_start();
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Origin: *");

require_once ('./DBConnect.php');
$conn = mysqli_connect($servername, $username, $password, $db ,$port);
error_log($_SESSION['Id']); //The error is on this line

$query = "select * from Service";
$result = $conn -> query($query);
$arraySingle = array();
$arrayAll = array();
while($row = mysqli_fetch_assoc($result)){
    $arraySingle = array(
        'Name' => $row['Name'],
        'Desc' => $row['Description']
    );
    $arrayAll[] = $arraySingle;
}
echo json_encode($arrayAll);

我得到的错误是:

  

PHP注意:未定义的索引:第8行的C:\ wamp64 \ www \ ProjetErgonomie \ GetServices.php中的ID

有人可以帮助我解决我的问题吗?谢谢!

0 个答案:

没有答案