0

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>";
}

?>

The logoutForm is not being displayed but the cookie is set. I am working on th XAMPP server. login is the database name and users is the table name. If the user is logged in, only logout form must be displayed and when the user is logged out only register and login form must be displayed.

1 Answer 1

1

Is it because in the php section, it will set the value to a boolean value as echo "true" will result in the javascript being

   var LoggedIn = true;

but in the javascript you are testing for a string "true" have you tried

  if(loggedIn==true)

without the quotes?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.