我的会话在localhost上工作,但是每当我尝试在我的信息学类项目的Web服务器(000webhost)上使用它们时,似乎它们并没有在整个页面中“保存”。
当我在登录页面上执行print_r($_SESSION);
时(按下登录按钮后),该数组将返回我预先输入的完整值。
我还试图查看与数据库的连接是否存在问题,但是当我尝试从表中检索值时,它就可以正常工作。
当我尝试在“ home.php”上print_r($_SESSION);
时,数组为空。
<?php
if($_POST) {
$connection = new PDO('mysql:host=localhost;dbname=noah', 'noah','password'); //changed the connection details because it's not usefull here
if(empty($_POST['username']) OR empty($_POST['password'])) {
?>
<div class="alert error">
<p>Please fill in all the fields</p>
</div>
<script>
$(".alert").fadeIn("500");
</script>
<?php
} else {
$username = $_POST['username'];
$password = $_POST['password'];
$password = hash('sha512', $password);
$verifyExistingUser = $connection->query("SELECT name FROM user WHERE username = '$username' AND password = '$password'");
foreach ($verifyExistingUser as $key => $value) {
retrievedUser = $value['name'];
}
if (empty($retrievedUser)) {
?>
<div class="alert error">
<p>Incorrect username / password</p>
</div>
<script>
$(".alert").fadeIn("500");
</script>
<?php
} else {
try {
session_start();
$_SESSION["username"] = $username;
$_SESSION["name"] = $retrievedUser;
$_SESSION["password"] = $password;
print_r($_SESSION);
echo "retrieved name from database: " . $retrievedUser;
echo "<script type='text/javascript'>window.location.href = 'home.php';</script>";
} catch(PDOException $e) {
echo "error: " . $e->getMessage();
}
}
}
}
?>
编辑1
经过更正的完整代码(输出为Cannot modify header information - headers already sent by (output started at /storage/ssd2/777/7604777/public_html/process/loginProcess.php:2) in /storage/ssd2/777/7604777/public_html/process/loginProcess.php on line 70
)。
我知道这是PHP中的HTML代码引起的,但是我想在其中包含一些jQuery和CSS,我该如何解决?
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="assets/css/php.css">
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
if($_POST) {
$connection = new PDO('mysql:host=localhost;dbname=noah', 'noah','password');
if(empty($_POST['username']) OR empty($_POST['password'])) {
?>
<div class="alert error">
<p>Please fill in all the fields</p>
</div>
<script>
$(".alert").fadeIn("500");
</script>
<?php
} else {
$username = $_POST['username'];
$password = $_POST['password'];
$password = hash('sha512', $password);
$verifyExistingUser = $connection->query("SELECT name FROM user WHERE username = '$username' AND password = '$password'");
foreach ($verifyExistingUser as $key => $value) {
$retrievedUser = $value['name'];
}
if (empty($retrievedUser)) {
?>
<div class="alert error">
<p>Incorrect username / password</p>
</div>
<script>
$(".alert").fadeIn("500");
</script>
<?php
} else {
try {
$_SESSION["username"] = $username;
$_SESSION["name"] = $retrievedUser;
$_SESSION["password"] = $password;
print_r($_SESSION); //debugging
echo "retrieved name from database: " . $retrievedUser; //debugging
header('location: ' . 'home.php');
} catch(PDOException $e) {
echo "error: " . $e->getMessage();
}
}
}
}
?>
</body>
</html>
答案 0 :(得分:1)
建议1 请在您的php页面顶部使用session_start()。
建议2 重定向到另一个页面,有一种简单的方法,
header('location: ' . 'http://site.url.you.want');