PHP在Session中放置变量并不起作用

时间:2017-09-29 14:50:08

标签: php session

我创建了一个php页面,在输入中接收一个编码为Json的字符串,我必须将此字符串描述并将所有Json属性放入会话中。

此页面为login.php。

为了测试这个页面,我创建了另一个名为test.php的页面。 在这个页面中,我记录了一个Json并将其发送到login.php。

问题是所有工作正常(编码,发送,解密)但变量未保存在会话中。

我试图直接打开login.php,我设置了一个固定的encripted json,在这种情况下一切正常。

一些帮助???

的login.php

<html>
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TantoSvago</title>

    <script src="../vendors/jquery/jquery-2.1.4.min.js"></script>
    <script src="../js/angular.min.js"></script>
    <script src="../js/ui-bootstrap-tpls-0.12.0.js"></script>
    <script src="../js/angular-google-maps.min.js"></script>
    <script src="https://code.angularjs.org/1.2.28/angular-route.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBze9qLOsDpAWj8938CYJSVsopwrkuWbPA&callback=initMap"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular-sanitize.js"></script>
    <script src="../js/markerclusterer.js"></script>
    <script src="../js/angular.rangeSlider.js"></script>
    <script src="../js/assets/app.js"></script> 

</head>
<body ng-app="tantoSvagoApp">

<?php
    session_start();
    include 'AES.php';

    $inputText = file_get_contents("php://input");

    //$inputText = "RFk0ZGRtZWQ0NUdrNzNHa3BtYVBCdklEYUlUMk5CUUdGMUF5V2tFSHVWRTRsUEtYZTRiL1FmVGNsY2pKZHdIb2lkOG1ra3BMODdPZUVuUmQzN3Vqd1JpamZGRmloeW1EU09xVFMzbU1Jd1Z0N1dNZzF6MitDYWlHZ3p6VUVyRXgycDYrbHU1Tm0yYVQ4amNuK0hheUNyODErSXZqMzVIQm9NdCtOQU0vVTcyMVBUQ09YQmRZTWZkM1JsbHk0aVJJaFFJdUYrR0JWZzF5WG1HUXl6QnFEa0d0V2ozNWl2YmhheGp6UkpXSVRFZDh4TXM3Q2Vyb2liQWp1UmJEZXNvYnFWNmkzc3ZzWEp4ak92MjB0ZWpjYWJGOFVoMEw0Vk8rNTI2WXhoMTRvYW89";

    $inputText = base64_decode($inputText);

    $inputKey = "466169626f20536f74662052756c657a";

    $blockSize = 256;

    $aes = new AES($inputText, $inputKey, $blockSize);

    $enc = $aes->decrypt();

    $inputJson = json_decode($enc);

    $email = $inputJson->{'email'};
    $firstName = $inputJson->{'firstName'};
    $lastName = $inputJson->{'lastName'};
    $phone = $inputJson->{'phone'};
    $credit = $inputJson->{'credit'};
    $userId = $inputJson->{'userId'};
    $supportPhone = $inputJson->{'supportPhone'};
    $supportMail = $inputJson->{'supportMail'};
    $paymentTypes = $inputJson->{'paymentTypes'};

    if (isset($email)) {
        $_SESSION['email'] = $email;

    }   

    if (isset($firstName)) {
        $_SESSION['firstName'] = $firstName;
    }   

    if (isset($lastName)) {
        $_SESSION['lastName'] = $lastName;
    }   

    if (isset($phone)) {
        $_SESSION['phone'] = $phone;
    }   

    if (isset($credit)) {
        $_SESSION['credit'] = $credit;
    }   

    if (isset($userId)) {
        $_SESSION['userId'] = $userId;
    }   

    if (isset($supportPhone)) {
        $_SESSION['supportPhone'] = $supportPhone;
    }   

    if (isset($supportMail)) {
        $_SESSION['supportMail'] = $supportMail;
    }   

    if (isset($paymentTypes)) {
        $_SESSION['paymentTypes'] = $paymentTypes;
    }else{
        $_SESSION['paymentTypes'] = 'welfare';
    }

    echo 'ok';
?>

<div ng-controller="loginController"></div>

</body>
</html>

test.php的

<?php
$inputText = '{"firstName":"Mario","lastName":"Rossi","email":"mario.rossi@gmail.com","phone":"02 342522","userId":2,"credit":30,"paymentTypes":"welfare","supportMail":"supporto@welfarebit.it","supportPhone":"0321 444999"}';

include 'AES.php';

$inputKey = "466169626f20536f74662052756c657a";

$blockSize = 256;

$aes = new AES($inputText, $inputKey, $blockSize);

$enc = $aes->encrypt();

$result = base64_encode($enc);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://welfarebitexperience.tantosvago.it/login/login.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $result );                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($result))                                                                       
);  

$result = curl_exec($ch);

curl_close($ch);

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body onload="test()">
<form id="B2BKv" name="B2BKv" action="http://welfarebitexperience.tantosvago.it/login/login.php" method="POST">
    <input type="hidden" id="MPCookieManager" name="MPCookieManager" value="<?php echo $result; ?>">
</form>
<script language="javascript">
function test() {
    document.B2BKv.submit();
}
</script>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

尝试将session_start()放在每页的html标记之前

<?php
    session_start();?>

<html>

<head>

和这个

<?php
session_start();
$inputText = '{"firstName":"Mario","lastName":"Rossi","email":"mario.rossi@gmail.com","phone":"02 342522","userId":2,"credit":30,"paymentTypes":"welfare","supportMail":"supporto@welfarebit.it","supportPhone":"0321 444999"}';

include 'AES.php';

$inputKey = "466169626f20536f74662052756c657a";

答案 1 :(得分:0)

正如over here所描述的那样,{<1}}需要在之前执行返回任何内容。

答案 2 :(得分:0)

从一开始就让我感到震惊的是调用session_start()的地方。 PHP手册指出:要使用基于cookie的会话,必须在向浏览器输出任何内容之前调用session_start()。