簡體   English   中英

PHP和JQuery:實時Cookie

[英]PHP & JQuery : Cookies in Real-Time

我不太確定這是否可能,這就是為什么我要在嘗試之前詢問。 這個想法是在PHP建立一個登錄文件,當憑據正確時, $_COOKIE['fid']更改為login key

該文件將通過帶有$.post函數的JQuery使用。

是否可以在JQueryPHP進行實時cookie刷新,因為這樣的想法是,如果登錄為TRUE ,則沒有response ,則PHP代碼應選擇cookie現在已SET

PHP示例:

if(isset($_COOKIE['fid'])):
   // do something
endif;

還是這不可能,因為頁面完成加載后,if語句已經返回truefalse嗎?

我只是希望能夠進行某種類型的后台刷新,然后再拾取PHP代碼,然后執行所有實時操作。

編輯:我發現此代碼進行更新,但是PHP代碼將如何知道它已更新?

$('#login').click(function(){
     _gaq.push(['_trackEvent', 'viewerChose', $.cookie('whichSide')+'side']);
});

@MarcosPérezGude在“ Comments部分中解釋了這實際上是不可能的。

處理更新的唯一方法是通過AJAX方法發送更新並使用響應,但是,這意味着響應必須使用JavaScript而不是PHP處理。

也許有一天他們將使這成為可能。

以可能的方式進行操作的示例-

jQuery的:

$("#login-btn").click(function(){
    $("#login-load-icon").css("display") = "block";
    $.post( "example.php", { user: "example", pass: "example").done(function(data){
        if(data == 1){
            location.reload();
        } else {
            $("#login-error").css("display") = "block";
            $("#login-load-icon").css("display") = "none";
        }
    });
});

example.php:

if(isset($_POST['user']) && isset($_POST['pass'])):
    if(strtoupper($_POST['user']) == strtoupper("This would a Database Query")):
        if($_POST['pass'] == "This would you matching the hashed pass with the hash in the DB"):
            setcookie("fid", "LOGIN KEY HERE", time()+3600, "/");
        endif;
    endif;
endif;
// etc...

然后,您在頁面上進入:

if(isset($_COOKIE['fid')):
        if($_COOKIE['fid'] == $Database['login_key']):
            // get all the user details and they're logged in
        else:
            // they tried to trick your system
    else:
        // load the login-btn
    endif;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM