我目前正在开展一个学校项目,我应该从我选择的某个应用程序访问数据库(在这种情况下是网页w / php),我遇到了一个问题。在我的函数NewUser()中,一旦创建了帐户,就会在登录窗口弹出一条消息(它是瑞典语,所以不要担心嘿嘿),但问题是: 考虑到我已经使用了会话,消息将永远存在,并且在我以某种方式重新启动会话之前永远不会被删除(在这种情况下,重新启动服务器),这不是最佳的。
例如,我想在每次重新加载页面时删除邮件,如果可能的话?
您可以提供的任何帮助都是有帮助的,我只是想知道在重新加载页面时关闭会话的最佳方法是什么?
祝你好运, 维克多
<?php
session_start();
ini_set("display_errors", true);
error_reporting( E_ALL );
include('connect.php');
function NewUser()
{
global $email;
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$pnumber = $_POST['pnumber'];
$age = $_POST['age'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
global $mysqli;
$stmt = $mysqli->prepare("INSERT INTO persons (firstname, lastname, pnumber, age, email, pwd)
VALUES (?,?,?,?,?,?)");
$stmt->bind_param('ssssss', $firstname, $lastname, $pnumber, $age, $email, $password);
$stmt->execute();
if ($mysqli)
{
$_SESSION['accountCreated'] = "Ditt konto har skapats, du kan nu logga in!";
header("Location:index.php");
exit();
}
else
{
echo ("<script type='text/javascript'>
window.alert('Oj, det blev något fel i registreringen! Försök igen');
window.location = 'index.php';
</script>");
exit();
}
}
function SignUp()
{
if(!empty($_POST['email']))
{
global $mysqli;
$query = ("SELECT * FROM persons WHERE email = ?");
if ($stmt = $mysqli->prepare($query))
{
$stmt->bind_param('s', $_POST['email']);
$stmt->execute();
$stmt->store_result();
$countRows = $stmt->num_rows;
$stmt->close();
}
if($countRows < 1)
{
$_SESSION['emailExistsError'] = "";
$_SESSION['message'] = "success";
NewUser();
}
else
{
$_SESSION['accountCreated'] = "";
$_SESSION['emailExistsError'] = "Denna mejladdressen har redan använts!";
$_SESSION['message'] = "error";
header("Location:index.php");
exit();
}
}
}
if(isset($_POST['submit']))
{
$close = false;
if(empty($_POST["firstname"]))
{
$_SESSION['fnameError'] = "Du måste fylla i ditt förnamn!";
$close = true;
} else {
$_SESSION['fnameError'] = "";
}
if(empty($_POST["lastname"]))
{
$_SESSION['lnameError'] = "Du måste fylla i ditt efternamn!";
$close = true;
} else {
$_SESSION['lnameError'] = "";
}
if(empty($_POST["email"]))
{
$_SESSION['emailError'] = "Du måste fylla i din e-mail!";
$close = true;
} else {
$_SESSION['emailError'] = "";
}
if(empty($_POST["password"]))
{
$_SESSION['passwordError'] = "Du måste fylla i ett lösenord!";
$close = true;
} else {
$_SESSION['passwordError'] = "";
}
if(empty($_POST["pnumber"]))
{
$_SESSION['pnumberError'] = "Du måste fylla i ditt personnummer!";
$close = true;
} else {
$_SESSION['pnumberError'] = "";
}
if(empty($_POST["age"]))
{
$_SESSION['ageError'] = "Du måste fylla i din ålder!";
$close = true;
} else {
$_SESSION['ageError'] = "";
}
if($close == true) {
header("Location:index.php");
exit();
} else {
SignUp();
}
}
$mysqli->close();
?>
答案 0 :(得分:4)
是的,只需使用unset:
unset($_SESSION['message']);
如果您要删除所有会话数据(我认为您不想要),您可以使用session_destroy()
或session_unset()
,具体取决于您的意图。