perseverancevictory

log.html

<html>
    <head><title>Home Page</title></head>
    <body>
        <form action="login.php" method="post">
            <input type="text" name="name">
            <input type="submit" value="Log in">
            </form>
    </body>
</html>

login.php

<?php
session_start();
if (isset($_POST[\'name\'])) {
    $name=$_POST[\'name\'];
    $connect=@new mysqli(\'localhost\',\'root\',\'123\',\'mytestdb\');
    if (mysqli_connect_errno()) {
        echo "Connection to database failed:".mysqli_connect_errno();
        exit;
        # code...
    }
    $query="select*from users where name=\'$name\'";

    $result=$connect->query($query);
    if ($result->num_rows) {
        $_SESSION[\'name\']=$name;
        # code...
    }

    $connect->close();
    # code...
}
if (isset($_SESSION[\'name\'])) {
    echo "You are logged in as ".$_SESSION[\'name\']."<br/>";
    echo "<a href=\'logout.php\'>Log out</a>"."<br/>";
    # code...
}else{
    if (isset($name)) {
        echo "Could not log in"."<br/>";
        # code...
    }else{
        echo "You are not logged in"."<br/>";
    }
}

logout.php

<?php
session_start();
unset($_SESSION[\'name\']);
session_destroy();

if (empty($_SESSION[\'name\'])) {
    echo "You have log out!";
    # code...
}

 

分类:

技术点:

相关文章:

  • 2021-12-04
  • 2021-05-22
  • 2021-03-31
  • 2021-11-17
  • 2021-11-25
  • 2021-11-27
  • 2021-06-17
  • 2022-02-23
猜你喜欢
  • 2022-01-21
  • 2021-04-06
  • 2018-04-16
  • 2021-10-08
  • 2021-10-09
  • 2021-11-06
相关资源
相似解决方案