Cookie is not being created

Asked

Viewed 60 times

-1

Guys, I have the following script:

<script>
    $(document).ready(function() {
        window.setInterval(function() {
            $(".anuncio iframe").iframeTracker({
                blurCallback: function() {
                    console.log("blurCallback!");
                    window.setTimeout(function() {
                        <?php setcookie("Changed","Ok", time() + 172800,  $path = "/"); ?>
                        liberar();
                   }, 2000);
                }
            }); 
        }, 5000);
    });
</script>

The "release" function of the script comes to be executed, but for some reason the cookie is not being created. When I place the cookie right above the head ai creates, but if I leave it inside this script it does not create, as if the script skipped the creation of the cookie and went straight to execute the "release" function. Could help me understand why the cookie is not being created?

  • A PHP code is executed on the server only once. Putting it there in setTimeout has no effect within Javascript. If you look at the source code of the page you will see that where you put <?php setcookie("Changed","Ok", time() + 172800, $path = "/"); ?> is empty.

  • But like, is it my intention that you create just one time? I just don’t understand why the script works, calls the "release" function, but skips the cookie creation.

  • So it doesn’t make sense to put the code right there in the middle of Javascript.

  • So where should I put it? Could you give me a light? I’m trying to create the cookie the moment an iframe click occurs. The iframe check works, it activates the "release" function, only the cookie that is not actually creating.

  • Communication between Javascript and PHP is done by AJAX.

2 answers

0

Good afternoon dear, I have a suggestion.

first create the following php file

 //arquivo setCookie.php
 <?php
 setcookie("Changed","Ok", time() + 172800,  $path = "/");
 echo 1;

now with jquery relate the desired event to the element

 <script>
 $(document).on("click", ".anuncio iframe", function(){
      setCookie();
 });
 </script>

This excerpt makes the event "click" ( or any event) and the element ". iframe" announcement are related at any time, regardless of whether the element has already been loaded or not. Then calling the setcookie() function described in the sequence

function setCookie(){
        $.ajax({
                url:"setCookie.php",
                success:function(data){
                        if(data == 1){
                        console.log("ok")
                        }
                }
        });
}

And voilá! to check if it worked open the inspector (F12) and see in the console the ok, see also in Aplication cookies, hugs

If you prefer copy and paste:

 <script>
 $(document).on("click", ".anuncio iframe", function(){
       $.ajax({
                url:"setCookie.php",
                success:function(data){
                        if(data == 1){
                        console.log("ok")
                        }
                }
        });
 });
 </script>

or

<script>
function setCookie(){
        $.ajax({
                url:"setCookie.php",
                success:function(data){
                        if(data == 1){
                        console.log("ok")
                        }
                }
        });
}
$(document).on("click", ".anuncio iframe", function(){
     setCookie();
});
</script>

Note that where it reads "click", it can be replaced by any event handler as listed in the link https://developer.mozilla.org/en-US/docs/Web/Events

  • Show, man. Thank you so much.

0

It is simpler to replace the <?php setcookie("Changed","Ok", time() + 172800, $path = "/"); ?> by a native function of the javascript language:

document.cookie = "Changed=ok";

With this you will create the cookie. If you want to add more parameters to the cookie go to w3Schools

  • Thanks, man. I’m gonna try this alternative

  • The suggestion came true?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.