我正在创建一个表单以使用 PHP(显然还有 MySQL)登录。这是PHP代码:
<?php
$host = 'localhost';
$db_user = 'root';
$db_password = '';
$db_name = 'learningsql';
$username = stripslashes($_POST['username']);
$password = stripslashes($_POST['password']);
if (strlen($username) < 1 || strlen($password) < 1){
echo "Username or password incorrect";
} else {
$username = trim(filter_var($_POST['username'], FILTER_SANITIZE_STRING));
$password = trim(filter_var($_POST['password'], FILTER_SANITIZE_STRING));
$hash = hash("sha512",$username. $password);
$connection = mysqli_connect($host, $db_user, $db_password);
$db = mysqli_select_db($connection, $db_name);
$result = mysqli_query("SELECT * FROM utenti WHERE username = '$username' AND password = '$hash'");
if(mysqli_num_rows($result)==0{
die("Username or password incorrect");
} else{
$_SESSION['loggedin']=1;
$user = mysqli_fetch_array($result);
header("location:loggato.php");
}
}
?>
带有表单的 .html 页面包含会话的 PHP 代码:
<?php
session_start();
if(isset($_SESSION['loggedin']))
header('location:loggato.html');
?>
但是,我有一个问题。我现在使用登录会话,但我想使用 cookie。如何?感谢您的回答。