用户在php中刷新页面

时间:2017-01-06 16:25:07

标签: php login-script

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>

模态内容提供弹出窗口。 刷新页面后,注销按钮消失,并且有登录按钮。但我希望网站具有与刷新之前相同的状态。只有1个可以登录表单或注销表单。

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>

2 个答案:

答案 0 :(得分:0)

删除此处的条件:

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

拥有session_start()的主要原因是设置会话。在此之前,$_SESSION将为空,会话永远不会被设置。并确保session_start()始终位于顶部,首先,因为它与header()函数类似。

答案 1 :(得分:0)

您必须在开始使用会话的每个页面开始会话。

更改

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

session_start();    

Make session_start();是您使用$ _SESSION的每个页面上调用的第一件事。另外,请确保<?php

之前没有空格或制表符