
在我的web应用程序的登录页面中,当用户输入正确的电子邮件和密码时,任何会话都将打开。在console>application中,我没有PHPSESSID。但是,当我将PHP文件与简单的HTML代码一起使用时,它可以正常工作,并且我有一个PHPSESSID。我不明白有什么问题,有人能帮我吗?
ReactJS文件:
import React from 'react';
import { Redirect} from 'react-router';
import './connexion.css';
import axios from 'axios';
class Login extends React.Component {
constructor(props) {
super(props);
this.state = {
password: null,
email: null,
loggedIn: false,
error: '',
};
}
change = async e => {
await this.setState({
[e.target.id]: e.target.value,
});
}
handleSubmitLogIn = e => {
const { email, password } = this.state;
if (password && email) {
console.log(this.state);
let formData = new FormData();
formData.append("email",this.state.email);
formData.append("password",this.state.password);
const url = "http://localhost:8888/API/Connexion/login.php";
axios.post(url, formData)
.then(function(response) {
this.setState({loggedIn: true});
console.log("success");
}.bind(this))
.catch((error) => {
if(error.response && error.response.status === 403){
this.setState({
error: error.response.data,
});
}
});
} else {
this.setState({
error: 'please fill in everything',
});
}
setTimeout(() => {
this.setState({
error: '',
});
}, 2000);
e.preventDefault();
}
render() {
const { error } = this.state;
const {loggedIn } = this.state;
if(loggedIn){
//redirect to the home page
return <Redirect to="/accueil"/>;
}
return (
<div>
<form onSubmit={this.handleSubmitLogIn}>
<div className="text-center">
<h2 className="dark-grey-text mb-5">
Log In
</h2>
</div>
<label htmlFor="email" className="grey-text">
email
<input
type="email"
id="email"
name="email"
className="form-control"
onChange={this.change}
/>
</label>
<br />
<label htmlFor="password" className="grey-text">
password
<input
type="password"
id="password"
name="password"
className="form-control"
onChange={this.change}
/>
</label>
<div className="text-center mb-3">
<button type="submit" className="btn btn-primary" onClick={this.handleSubmitLogIn}>validate</button>
{
error && (
<p className="error">{error}</p>
)
}
</div>
</form>
</div>
);
}
}
export default Login;PHP文件login.php:
<?php
session_start();
$con=mysqli_connect('localhost', 'root', 'root', 'mybibli');
if(!$con) {
die('erreur de connexion avec la base de donnée'.mysql_error());
}
$email = $_POST['email'];
$password = $_POST['password'];
$email = strip_tags(mysqli_real_escape_string($con, trim($email)));
$password = strip_tags(mysqli_real_escape_string($con, trim($password)));
$query = "SELECT * FROM user where email='".$email."'";
$hi = mysqli_query($con, $query);
if(mysqli_num_rows($hi) > 0) {
$row = mysqli_fetch_array($hi);
$password_hash = $row['password'];
if(password_verify($password, $password_hash)) {
$_SESSION['User']=$_POST['email'];
http_response_code(200);
} else {
echo"invalid password";
http_response_code(403);
}
} else{
echo"No user account linked to this email address";
http_response_code(403);
}
?>发布于 2020-05-09 00:14:49
仔细阅读这里的建议,Make Axios send cookies in its requests automatically。我不想重复这一点,但是您必须将withCredentials: true添加到您的axios请求中,并且在您的情况下,您必须向您的响应Access-Control-Allow-Origin: http://localhost:8888添加头部。这就是你所需要的。
https://stackoverflow.com/questions/61682882
复制相似问题