php没有显示else-block

时间:2018-05-24 11:21:21

标签: php mysqli

我正在尝试了解session_start()但是当我运行该文件时,它只显示

内的内容
if (isset($_SESSION['username'])&& isset($_SESSION['password'])==$password) {
?>
    <a href="home.php">log out</a>
<?php } ?>

并且没有显示else{...},甚至在我单击注销后,它也不会在else语句中打印任何内容,只会在if语句中打印。我使用另一个文件来执行注销,但我不知道session_destroy()

的正确代码

这是下面的logout.php代码:

<?php
session_start(); 
session_destroy(); 
header("location: home.php"); 
?>

这是完整的代码:

<?php 
session_start();
include("DB/db.php");
$_SESSION['username']=$username;
$_SESSION['password']=$password;
$_SESSION['is_log_in'] = true; 
 ?><!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/css.css">
</head>
<body>
<div id="blank"></div>
    <div id="panel">
        <nav id="bar">
            <div id="submen">
                <form id="sir">
                <input type="Search" name="search" placeholder="Search.." id="search">
            </form>
                <a  href="wallpaper.php" id="menu">Walpaper</a>
                <a  href="art.php" id="menu">Art</a>
                <a  href="photo.php" id="menu">Photos</a>
                <a  href="image.php" id="menu">Image</a>
 <?php
    if (isset($_SESSION['username'])&& isset($_SESSION['password'])==$password) {?>

                <?php echo $username?>
                    <a href="logout.php">log out</a>

            </div>
        </nav>
    </div>
</table>
    <?php } else {
    ?>
                <a href="log_in.php" id="member">login</a>
                <a href="register.php" id="member">register</a>
            </div>
        </nav>
    </div>
    </table>
    <?php } ?>

    </body>
   </html>

登录脚本的更新

<?php
session_start();

include("DB/db.php"); 

if ($_GET['log']=='out'){
    session_destroy();
} 
if ($_POST['user']){
    $sql = "Select password from user where username = '".$_POST['user']."' ";
    $result = mysqli_query($koneksi, $sql);
    if (mysqli_num_rows($result)){ 
        $row = mysqli_fetch_assoc($result);

        if ($row['password'] == md5($_POST['pass'])) {

            $_SESSION['login'] = TRUE;
            $_SESSION['username'] = $user;
            $_SESSION['password'] = $pass;
        }else{

            $pesan = "Username and password mismatch";
        }
    }else{

        $pesan = "please register";
    }
}   

?><!DOCTYPE html>
<html>
<head>
    <title>Log in</title>
</head>
<body>
    <?php

    if ($_SESSION['login']) {

        echo "text";
    }else{
        ?>
        <h1>Login</h1>
        <form method="post" action="rahasia.php">
            Username: <input type="text" name="user">
            Password: <input type="password" name="pass">
            <input type="submit" name="" value="Login">
        </form>
        <form method="post" action="register.php">
            <input type="submit" name="register" value="register"> 
        </form>

        <?php
    }
    echo $pesan;
    ?>
</body>
</html>

我哪里出错了

1 个答案:

答案 0 :(得分:0)

您的$ _SESSION变量始终设置,$ password始终等于$ _SESSION ['password']。

$_SESSION['username']=$username; // null, plus notice in error_log
$_SESSION['password']=$password; // null, plus notice in error_log

除非在include("DB/db.php");中设置了这两个变量,否则这是不好的做法。你能粘贴db.php看看里面发生了什么吗?

更新。

好的,所以正在设置变量。现在这意味着:

$_SESSION['username']=$username; // a
$_SESSION['password']=$password; // 123456789

因此他们仍然会匹配。您需要重构这些行才能正常运行。您确定mysql凭据是您想要的登录用户 ?