简体   繁体   English

PHP和JQuery:实时Cookie

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

I am not too sure if this is possible which is why I am asking before I attempt it. 我不太确定这是否可能,这就是为什么我要在尝试之前询问。 The idea is to build a login file in PHP which when the credentials are correct, the $_COOKIE['fid'] is changed to a login key . 这个想法是在PHP建立一个登录文件,当凭据正确时, $_COOKIE['fid']更改为login key

This file is going to be used using JQuery with a function $.post . 该文件将通过带有$.post函数的JQuery使用。

Is it possible to do a real-time cookie refresh in JQuery or PHP because the idea is that there is no response if the login is TRUE , the PHP code should pick-up that the cookie is now SET 是否可以在JQueryPHP进行实时cookie刷新,因为这样的想法是,如果登录为TRUE ,则没有response ,则PHP代码应选择cookie现在已SET

PHP example: PHP示例:

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

Or is this not possible because as soon as the page finishes loading, the if statements have already been returned true or false ? 还是这不可能,因为页面完成加载后,if语句已经返回truefalse吗?

I'd just love to be able to make some kind of background refresh which then the PHP code picks up and then does something so its all real-time. 我只是希望能够进行某种类型的后台刷新,然后再拾取PHP代码,然后执行所有实时操作。

Edit: I found this code to update it, but how would the PHP code know its been updated? 编辑:我发现此代码进行更新,但是PHP代码将如何知道它已更新?

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

@MarcosPérezGude has explained in the Comments section that this isn't actually possible. @MarcosPérezGude在“ Comments部分中解释了这实际上是不可能的。

The only way to work with the update would to send it via AJAX methods and work with a response, however, this means the response has to be handled in JavaScript and not PHP . 处理更新的唯一方法是通过AJAX方法发送更新并使用响应,但是,这意味着响应必须使用JavaScript而不是PHP处理。

Maybe one day they will make this possible haha. 也许有一天他们将使这成为可能。

Example of doing it the way that is possible - 以可能的方式进行操作的示例-

JQuery: 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: 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...

Then on the page you're in: 然后,您在页面上进入:

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