2016-10-09 18 views
0

セッションスクリプトのユーザ名とCookieが設定されているかどうかを確認するログインスクリプトとfunctions.phpスクリプトがあります。ユーザーがログインすると、Remember Meを選択するとCookieが設定されます。しかし、問題は、スクリプトは動作しますが、クッキーは設定されないので、ユーザーはログインしていません。私はここで非常に多くのトピックを検索し、可能な限り多くのソリューションを試しましたが、同じ結果になるか、最終的に私にエラーを与えてしまいます。ログインフォームの送信後にクッキーが設定されていない

      if (isset($_POST['rem']) && $_POST['rem'] == 'on') > { 
           setcookie('MCLatestUser', $token, 31622400, > '/'); 
           session_regenerate_id(true); 
          } 

これは覚えているがチェックされた場合にクッキーを設定しなければならないコードの一部です。

Log.php(私はAjaxのログインスクリプトを使用しているので、URLはこれに設定されている):

<?php 
include_once 'db.php'; 
include_once 'functions.php'; 
error_reporting(-1); 

if(isset($_POST['email'])) { 
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING); 
    $password = $mysqli->real_escape_string($_POST['password']); 
    try { 
     $check = mysqli_query($mysqli, "SELECT * FROM users WHERE email='$email'"); 
     $res = mysqli_num_rows($check); 
     if($res > 0) { 
      while($run = mysqli_fetch_array($check, MYSQLI_ASSOC)) { 
       $blocked = $run['blocked']; 
       $deactivated = $run['deactivated']; 
       $paused = $run['paused']; 
       $verified = $run['verified']; 
       $rank = $run['rank']; 
       $token = $run['token']; 
       $pass = $run['password']; 
       $pbackup = $run['pbackup']; 
       if($verified == 'true') { 
        if($blocked == 'true') { 
         echo 'Looks like your account was blocked. If you think this is an error, please contact an admin via [email protected]'; 
        } else if($deactivated == 'true') { 
         echo 'Looks like your account has been deactivated. If you think this is an error, please contact an admin via [email protected]'; 
        } else if($paused == 'true') { 
         echo 'Looks like your account is frozen. If you think this is an error, please contact an admin via [email protected]'; 
        } else { 
         if(password_verify($password, $pass)) { 
          $timestamp = time(); 
          // Authenticated, set session variables 
          $_SESSION['username'] = $run['username']; 
          if (isset($_POST['rem']) && $_POST['rem'] == 'on') { 
           setcookie('MCLatestUser', $token, 31622400, '/'); 
           session_regenerate_id(true); 
          } 
          $sql = mysqli_query($mysqli, "UPDATE users SET Ip = '$ipaddress', login_ip = '$ipaddress', latest_login_date = '$now', login_date = '$date', login_time = '$time', login_day = '$day', login_month = '$month', login_year = '$year', status = '$timestamp' WHERE email = '$email'"); 
          if($sql) { 
           echo "Success!"; 
          } else { 
           echo "Error login in"; 
          } 
          // do stuffs 
         } else if(password_verify($password, $pbackup)) { 
          $timestamp = time(); 
          // Authenticated, set session variables 
          $_SESSION['username'] = $run['username']; 
          if (isset($_POST['rem']) && $_POST['rem'] == 'on') { 
           setcookie('MCLatestUser', $token, 31622400, '/'); 
           session_regenerate_id(true); 
          } 
          $sql = mysqli_query($mysqli, "UPDATE users SET Ip = '$ipaddress', login_ip = '$ipaddress', latest_login_date = '$now', login_date = '$date', login_time = '$time', login_day = '$day', login_month = '$month', login_year = '$year', status = '$timestamp' WHERE email = '$email'"); 
          if($sql) { 
           echo "Success!"; 
          } else { 
           echo "Error login in"; 
          } 
          // do stuffs 
         } else { 
          echo "<h4 style='font-weight:bold;font-family:arial;margin:8px'>Your password is incorrect, please try again. If you still get this error after using your backup password, please <a href='https://mclatest.com/community/reset.php?r=password'>reset</a> your password</h4>"; 
         } 
        } 
       } else { 
        echo "<h4 style='font-weight:bold;font-family:arial;margin:8px'>You need to verify your account. Please click this link to <a href='https://mclatest.com/community/confirm.php?email=".$email."&token=".$token."'>verify your account</a></h4>"; 
       } 
      } 
     } else { 
      echo 'No records of that user have been found!'; 
     } 
    } catch(PDOException $e){ 
     echo $e->getMessage(); 
    } 

} else { 
    echo "Invalid email"; 
} 

Login.php(HTMLとAjaxフォーム):

<form id="login_form" style="text-align:center" method="post"> 
           <script> 
            $(document).ready(function() { 
             $("#login").click(function(e) { 
              e.preventDefault(); 
              var email = $("#email").val(); 
              if (email = "") { 
               $("#error_msg").html("<h4>Email cannot be empty</h4>"); 
              } else { 
               var data = $("#login_form").serialize(); 
               $.ajax({ 
                type: "POST", 
                url: "../inc/log.php", 
                data: data, 
                beforeSend: function() { 
                 $("#error_msg").fadeOut(); 
                 $("#login").val('sending ...'); 
                }, 
                success: function(data) { 
                 if (data == "Success!") { 
                  // alert("Works"); //for testing purposes 
                  window.location.href = "index.php"; 
                 } else { 
                  $("#error_msg").fadeIn(1000, function() { 
                   $("#error_msg").html('<div style="border:1px solid: red; background:rgba(255,0,0,0.9;)">'+data+'!</div>'); 
                   $("#login").val('Login'); 
                  }); 
                 } 
                }, 
                error: function(data) { 
                 alert("Process Failed!"); 
                } 
               }); 
               return false; 
              } 
             }); 
            }); 
           </script> 
           <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label "> 
            <label for="input_email" class="mdl-textfield__label">Email</label> 
            <input type="email" name="email" class="mdl-textfield__input" maxlength="255" id="input_email" /> 
           </div> 
           <br> 

           <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label"> 
            <label for="input_password" class="mdl-textfield__label">Password</label> 
            <input type="password" name="password" class="mdl-textfield__input" maxlength="255" id="input_password" /> 
           </div> 
           <br> 

           <label style="width:auto !important" for="remember_me" class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" > 
            <input name="rem" type="checkbox" id="remember_me" class="mdl-checkbox__input" checked /> 
            <span class="mdl-checkbox__label">Stay logged in?</span> 
           </label> 
           <br> 

           <nav style="width:auto !important;display:-webkit-box;-webkit-box-pack:center" class="mdl-navigation"> 
            <a class="mdl-navigation__link" href="forgot.php?ftype=password">Forgot Password?</a>&nbsp; | &nbsp; 
            <a class="mdl-navigation__link" href="register.php">Register?</a> 
           </nav> 
           <br> 

           <input type="submit" id="login" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" name="login" value="Login"/> 
          </form> 

のfunctions.php(これはセッションとクッキー変数を確認するためのスクリプトの一部である):

function loggedIn() { 
    if (isset($_SESSION['username']) && !empty($_SESSION['username']) && isset($_COOKIE['MCLatestUser'])) { 
     return true; 
    } else { 
     return false; 
    } 
} 

スクリプトは機能しますが、クッキーは送信されません。私はここで私の知恵の終わりで、これを4〜5時間以上かけて作業しています。これを理解するために35個以上のクロムタブが開いています。おそらく私は細部を見落としているでしょう。私は、関数スクリプト

+0

詳細を見る前に、最初に明白になるはずです...あなたのブラウザでクッキーが有効になっていますか? –

+0

はい、デフォルトのPHPSESSIDクッキーは常に0に設定されています –

+0

タイトルに解決しないでください。代わりに回答を受け入れてください。 –

答えて

0
setcookie('MCLatestUser', $token, 31622400, '/'); 

このメソッドは、現在の時刻に基づいて行われるべき第三parameter.Itでいくつかの問題があるからを削除する場合Login Page Link

は、それは動作します。

PHP: setcookie - Manual

+0

申し訳ありませんが、何も変更されていません –

+0

それを理解することは素晴らしいことです。しかし、そのスクリプトを実行できるかどうかは疑問です。 – JohnZ

0

私はそれを考え出しました。それはブラウザの欠陥であった。私はMicrosoft EdgeとMozilla Firefoxでそれを試してみました。だから私はその問題を見て、私がしなければならなかったのはクロムのclear my cookies and site dataでした。助けてくれた人たちと、したいと思っていた人たちに感謝しました。

+0

私はそれが解決されたことを明確にするために質問を編集することをお勧めします。これは、Google検索でこのページを見つけるのに役立ちます。解決策を探す際に時間を節約するためです。 –

+0

ありがとうございます。質問に「(解決済み)」を追加しました。 –

関連する問題