简体   繁体   中英

user gets logged out on page refresh in php

WebPage.php

<?php
    session_start();    
    include 'functions.php';
?>
<html>
<head>
    <title>Ecommerce</title>
    <link rel="stylesheet" href="Styling.css">

</head>
<body onLoad="CheckLogin()">

<script>

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

      var logout_Form = document.getElementById("logout_Form"); 
      var login_Form = document.getElementById("login_Form"); 

      if(loggedIn == "true"){
          login_Form.style.display="none";
          logout_Form.style.display = "show";
      }else{
          logout_Form.style.display="none";
          login_Form.style.display = "show";
      }
  }

  function LogOut(){
      <?php  
        session_destroy();

      ?>
      window.open('WebPage.php', '_self');
  }

  function Login()
  {
    var modal = document.getElementById('loginModal');

    var btn = document.getElementById("login");

    var span = document.getElementsByClassName("close")[0];

    modal.style.display = "block";


    span.onclick = function() {
        modal.style.display = "none";
    }

    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
  }

  function SignUp()
  {
        var modal = document.getElementById('signModal');

        var btn = document.getElementById("signUp");

        var span = document.getElementsByClassName("close2")[0];

        modal.style.display = "block";

        span.onclick = function() {
            modal.style.display = "none";
        }

        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
  }
</script>

<div style = "background:cyan">
<font color="green"><h1><center>WELCOME</center></h1></font>
    <div>

<!--Logo and Search Box-->  
        <form method="GET" action = "results.php">
            <a href="WebPage.php"><img src = "ball.png" alt = "Logo of Website" height = "100px" width = "100px"></a>
            <input type = "text" name = "user_query" placeholder="Search" size="50px" style="overflow:hidden">
            <button type="Submit" name="search">Search</button>
        </form>

<!--LOGIN-->    
    <div id="login_Form">   
    <button id = "login" onclick="Login()">Login</button>
    <button id="signUp" onclick="SignUp()">Sign Up</button> 

    <div id="loginModal" class="loginModal">

    <!-- Modal content -->
    <div class="login-content">
            <span class="close">&times;</span>
            <p><h2>Login</h2>
        <form name="login" method="POST" id="login" action="loginCheckUser.php">
            <label><b>E-Mail ID: </b></label>
            <input type="email" name="email" required>
            <br><br>
            <label><b>Password: </b></label>
            <input type="password" name="pass" required>
            <br><br>
            <button type="submit" id="submit" name="submit">Login</button>
            <a href="loginCheckSell.php"><input type="button" id="sellerLogin" value="Login as Seller"></a>
            <br><br>
            <input type="checkbox" checked>Remember Me
        </form></p>
        </div>

    </div>

    <div id="signModal" class="signModal">

        <!-- Modal content -->
        <div class="signUp-content">
            <span class="close2">&times;</span>
            <p><h2>Sign Up</h2>
        <form name="signup" action="register.php" method="POST" id="signup">
            <label><b>Name: </b></label>
            <input type="text" name="name" required>
            <br><br>
            <label><b>E-Mail ID: </b></label>
            <input type="email" name="email" required>
            <br><br>
            <label><b>Password: </b></label>
            <input type="password" name="pass" required>
            <br><br>
            <label><b>Confirm Password: </b></label>
            <input type="password" name="con_pass" required>
            <br><br>
            <label><b>Mobile No: </b></label>
            <input type="tel" id="phoneno" name="phone" required>
            <br><br>
            <button type="submit" id="submit" name="submit">Register</button>
            <a href="regSell.php"><button type="submit" id="subSell" name="subSell">Register as Seller</button></a>
        </form></p>
        </div>


    </div>
    </div>

    <div id="logout_Form">
        <div class="dropdown">
            <button class="dropbtn"> <?php 
                        echo "Hi ";
                        if(isset($_SESSION['email'])){
                            echo getName($_SESSION['email']);
                        }
            ?></button>
            <div class="dropdown-content">
                <a href="myAccount.php?account=a&personal=p">Account</a>
                <a href="myAccount.php?orders">Orders</a>

            </div>
        </div>
        <button name="logout" id="logout" onclick="LogOut()">Log Out</button>
    </div>

    </div>


    </div>  
<!--Categories-->   
    <div>
        <ul id="searchMenu">
            <li>
                <a href="#" title="Electronics">Electronics</a></li>
            &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
            <li>
                <a href="#" title="Appliances">Appliances</a></li>
            &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 
            <li>
                <a href="#" title = "Men">Men</a></li>
            &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
            <li>
                <a href="#" title="Women">Women</a></li>

        </ul>
    </div>

    <div id="content_area">

    <?php cart();?>
        <div id="shopping_cart">
            <span style = "float: right; font-size: 18px; padding: 5px, line-height:40px;">
                Welcome <b style="color:yellow">Shopping Cart: </b>Total Items: <?php total_items();?> - Total Price: <?php total_price();?><a href="cart.php">Go to Cart</a>

            </span>
        </div>

        <?php
            $ip = getIp();
        ?>
        <div id="products_box">
            <?php

                get_pro();
                getCatPro();
                getBrandPro();
            ?>
        </div>
    </div>
<!--Company Info-->
    <div>
        <ul id="CompInfo" >
            <li style="background-color:red"><a href="WebPage.php">Home</a></li>
            <li style="background-color:red"><a href="#">About Us</a></li>
            <li style="background-color:red"><a href="#">Contact</a></li>

        </ul>
    </div>

</body>
</html>

The modal contents gives a pop up window. As soon as I refresh the page the logout buttons disappears and there are the login buttons. But I want the site to have the same state as it was before refreshing. Only 1 can be there either login form or logout form.

loginCheckUser.php

<?php  session_start();?>
<html><head>

</head>
<body>

<script>
        var lat =0 ;
        var lon =0 ;

        function getLocation() {
            navigator.geolocation.getCurrentPosition(showPosition);
        }

        function showPosition(position) {
            document.getElementById("lat").value = position.coords.latitude; 
            document.getElementById("lon").value =  position.coords.longitude;
            document.getElementById("check").value= "true";
            document.getElementById("form").submit();
        }


</script>
<form id="form" method="post">
<input type="hidden" id="lat" name="lat">
<input type="hidden" id="lon" name="lon">
<input type="hidden" id="check" value="">

</form>

<?php

include 'functions.php';

$email =$_POST['email'];
$pass =$_POST['pass'];

if($email && $pass){

$connect = mysqli_connect('localhost', 'root', '','test') or die("Couldn't connect to database");

$ip = getIp();
$query = mysqli_query($connect, "SELECT * FROM users WHERE email = '$email'") or die("NO query");

$numrows = mysqli_num_rows($query);

if($numrows >=1){

    while($row = mysqli_fetch_assoc($query)){
        $dbemail = $row['email'];
        $dbpass = $row['pass'];
    }

    if($email == $dbemail && md5($pass) == $dbpass){
        echo '<script>
                getLocation();

              </script>';

        $latitude = $_POST['lat'];
        $longitude = $_POST['lon'];

        $check =$_POST['check'];


        if($check=='true'){
            $sql1 = "UPDATE users SET lat  = '$latitude', lon  = '$longitude' where email = '$email'";

            mysqli_query($connect, $sql1);
        }

        $name="";
        $q = mysqli_query($connect, "SELECT name FROM users where email = '$email'") or die("cannot get");
        while($row = mysqli_fetch_assoc($q)){
            $name = $row['name'];
        }

        $_SESSION['email'] = $email;

        header("location: WebPage.php");

    }else{
        echo "<script type='text/javascript'>alert('Your password is incorrect');</script>";
        header("location: WebPage.php");
    }
}

else{
    echo "<script type='text/javascript'>alert('The user Doesn't exist');</script>";
    header("location: WebPage.php");
}

}

?>  </body></html>

Remove the condition here:

if (!isset($_SESSION)) {
    session_start();    
}

The main reason to have session_start() is for setting the session. Before that, the $_SESSION will be empty and the session never gets set. And make sure, session_start() is always at the top, first, as it is similar to header() function.

You have to start session at beggining of every page you are going to use session on.

Change

if (!isset($_SESSION)) {
    session_start();    
}

to

session_start();    

Make session_start(); is the very first thing called on every page where you are using $_SESSION. Also, make sure no spaces or tabs are before <?php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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