[英]PHP session not working in Aurelia
I'm using Aurelia with PHP as backend. 我将Aurelia与PHP一起用作后端。 Here is my view with its model: 这是我对其模型的看法:
home.html home.html的
<template>
<require from="datepicker.js"></require>
<form submit.delegate="submit()">
<input value.bind="name"></input>
<input value.bind="age"></input>
<button type="submit" name="submit">Submit
</button>
</form>
</template>
home.js home.js
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';
@inject(HttpClient)
export class Home {
name;
age;
constructor(http) {
this.http = http;
}
submit() {
this.jsonobj = {
'name': this.name,
'age': this.age
};
if (this.jsonobj.name && this.jsonobj.age) {
this.http.fetch('dist/components/ses.php', {
method: 'post',
body: JSON.stringify(this.jsonobj)
})
.then(response => response.json())
.then(data =>
{
console.log(data);
});
}
}
}
And here is the PHP script: 这是PHP脚本:
ses.php ses.php
<?php
session_start();
$word = 'lol';
if(!isset($_SESSION['username'])){
$_SESSION['username'] = 'kil';
}
$input = file_get_contents('php://input');
$input_json_array = json_decode($input);
echo json_encode(array('item' => $_SESSION['username']));
?>
I expect that after the first call to the script, $_SESSION['username'] will be set to 'kil'. 我希望在第一次调用脚本之后,$ _ SESSION ['username']将被设置为'kil'。 So on next ajax post !isset($_SESSION['username'] wont evaluate to true but it does which means PHP session isn't working. 因此,在下一个ajax帖子中!isset($ _ SESSION ['username']不会评估为true,但这确实意味着PHP会话无法正常工作。
By default fetch
(the web standard that aurelia-fetch-client is built on) does not send cookies. 默认情况下, fetch
(建立aurelia-fetch-client的Web标准)不会发送cookie。 You'll need to use credentials: 'include'
in your request init object. 您需要在请求init对象中使用credentials: 'include'
。
Two great resources for fetch: 可获取的两个重要资源:
The codez: 编码:
this.http.fetch('dist/components/ses.php', {
credentials: 'include', // <-------------------------------------
method: 'post',
body: JSON.stringify(this.jsonobj)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.