登录注销脚本php

时间:2017-01-08 10:37:27

标签: php login

loginTest.php

<?php
    session_start();
    $con = mysqli_connect('localhost', 'root', '', 'login');


?>

<script>
function CheckLogin(){
    var loggedIn = <?php 
        if(isset($_COOKIE['name'])){
            echo "true";
        }else{
            echo "false";
        }

    ?>;

    var loginForm = document.getElementById("loginForm");
    var logoutForm = document.getElementById("logoutForm");

    if(loggedIn=="true"){
        loginForm.style.display = "none";
        logoutForm.style.display = "block";
    }else{
        loginForm.style.display = "block";
        logoutForm.style.display = "none";
    }
}



</script>

<html>
    <body onload="CheckLogin()">
    <div id="loginForm">
        <form method="POST">
            <input type="email" name="email">
            <input type="password" name="pass">
            <button type="submit" name="login">LOGIN</button>
        </form>

    <div id="regForm">
        <form method="POST">
            <input type="email" name="email">
            <input type="password" name="pass">
            <input type="text" name="name">
            <button type="submit" name="register">Register</button>
        </form>
    </div>  </div>

    <div id="logoutForm">
        <p>Hi <?php echo $_COOKIE['name'];?></p>
        <a href="loginTest.php?logout"> <button type="submit" name="logout">LogOut</button></a>

    </div>
    </body>
</html>


<?php
if(isset($_GET['logout'])){
    if(isset($_COOKIE['name'])){
        session_destroy();
        setcookie('name', $name, time()-30);
    }
    echo "<script>window.open('loginTest.php', '_self')</script>";
}


if(isset($_POST['login'])){
    $email = $_POST['email'];
    $pass = $_POST['pass'];

    $check = mysqli_query($con , "Select * from users where email = '$email'");
    if(mysqli_num_rows($check)>=1){
        $name="";
        while($row = mysqli_fetch_array($check)){
            $dbemail = $row['email'];
            $dbpass = $row['pass'];
            $name = $row['name'];
        }

        if($email==$dbemail && md5($pass)==$dbpass){
            $pass=md5($pass);
            setcookie('name', $name);
        }else{
            echo "<script>alert('Wrong Inputs Given')</script>";

        }
    }else{
        echo "<script>alert('Does Not exist')</script>";

    }
    echo "<script>window.open('loginTest.php', '_self')</script>";

}
if(isset($_POST['register'])){
    $email = $_POST['email'];
    $pass = $_POST['pass'];
    $name= $_POST['name'];
    $check = mysqli_query($con , "Select * from login where email = '$email'");
    if(mysqli_num_rows($check) >=1){
        echo "<script>alert('User Already Exists')</script>";
    }else{
        $pass = md5($pass);
        mysqli_query($con,  "INSERT INTO users (email, pass, name) VALUES('$email', '$pass', '$name')" ) or die('cannot insert');

            setcookie('name', $name);
            echo "<script>alert('Successful Registration')</script>";

    }
    echo "<script>window.open('loginTest.php', '_self')</script>";
}

?>

未显示logoutForm但已设置cookie。我在XAMPP服务器上工作。 login是数据库名称,users是表名。 如果用户已登录,则只能显示注销表单,当用户注销时,只能显示注册和登录表单。

1 个答案:

答案 0 :(得分:1)

是因为在php部分,它会将值设置为布尔值,因为echo“true”将导致javascript正在

   var LoggedIn = true;

但是在javascript中你正在测试字符串“true”你试过吗

  if(loggedIn==true)

没有引号?