简体   繁体   English

在PHP登录页面中使用Cookie

[英]Use cookie in a PHP login page

I'm create a form to login with PHP (and, obviously, MySQL). 我正在创建一个表单以使用PHP(显然还有MySQL)登录。 This is the PHP code: 这是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");

    }
}
?>

The .html page with form contains PHP code for session: 带有表单的.html页面包含用于会话的PHP代码:

<?php
session_start();
if(isset($_SESSION['loggedin']))
    header('location:loggato.html');
?>

But, I have a problem. 但是,我有一个问题。 I use at the moment the login's sessions, but I wanna use cookie. 我现在使用登录会话,但是我想使用cookie。 How? 怎么样? Thanks for the answers. 感谢您的回答。

The PHP session stores a cookie to enable the session. PHP会话存储cookie以启用该会话。 With the function session_start() you start the session (duhh) and the session will set a cookie called PHPSESSID. 使用函数session_start()可以启动会话(duhh),该会话将设置一个名为PHPSESSID的cookie。 Check it in your browser with some dev tool. 使用一些开发工具在浏览器中检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM