2017-01-06 5 views
1

WebPage.phpユーザー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つしか存在できません。

<?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> 
+6

'session_start()'が一番上になければHTMLが出力されません。 –

+0

jsとphpを極端に混ぜてはいけません。ログイン/ログ機能はPHPでもできます。あなたのコードは混在して読みにくいです。 –

+0

@Alex Wiebogen。トラブルをおかけして申し訳ありません。しかし、私はhtmlで緯度と経度を取得する方法を知らない。だから、私はjsとphpの両方を入れる必要があったのです。 –

答えて

0

loginCheckUser.phpは、ここに条件を削除します。

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

session_start()を持っている主な理由は、セッションを設定するためのものです。その前に、$_SESSIONは空になり、セッションは決して設定されません。そして、session_start()が常にheader()の機能に似ているので、常に先頭にあることを確認してください。

+0

申し訳ありませんが、問題は解決しません。 –

+0

@AyushBansalフルコードを表示してください。 –

+0

これは最初のフルページです。 loginCheckUser.phpファイルも欲しいですか? –

0

セッションを開始するには、ページごとにの先頭にセッションを開始する必要があります。

session_start();  

変更

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

session_start();あなたは$ _SESSIONを使用しているすべてのページに呼ばれる非常に最初のものであることを確認してください。また、空白やタブがないことを確認してから、<?php

+0

申し訳ありません。出力に変化はありません。まだ同じことをやっている。それはサーバーの問題でしょうか?私はXAMPPサーバーに取り組んでいます。 –

+0

session_start()をインクルードしましたか?このような? '<?php session_start(); ?> eコマース <リンクのrel = "スタイルシート" のhref = "Styling.css"> <体のonLoad = "CheckLogin()"> <?phpの (もし!ISSET($ _SESSION)){ session_start(); ' –

+0

@ Ondrej Vrabel。私はsession_start()のコードを更新しました。 –

関連する問題