Preload with Cookie or Session

Asked

Viewed 88 times

0

Hey, guys, what’s up? I am using a code Preload is working very well, but the problem is that every time you access the site loads the Preload it bothers a lot.. would like you to only press 1 time for access, for example if continuing browsing will no longer load, but if you re-access will show the Preload again.

1 answer

0


You can set a variable in $_SESSION and check if it was set at the time of showing the Preload, in the case of PHP would be something like this:

<?php
session_start();

if (empty($_SESSION['preload'])) {
   $_SESSION['preload'] = true;
  //código para mostrar o preload
}
?>

With JS you can do using cookie for example:

function setCookie(cname, cvalue, days) {
    var d = new Date();
    var expires;
    if (days) {
        d.setTime(d.getTime() + (days*24*60*60*1000));
        expires = "expires="+ d.toUTCString();
    }else {        
        expires = "";
    }
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return "";
}

function checkPreload() {
    var preload = getCookie("preload");
    if (preload == "") {
        console.log("mostrar preload");
        setCookie("preload", 1, 1);
    } else {
        console.log("já mostrou o preload");
    }
}

checkPreload();
  • I don’t know much about PHP, how I would mount the PHP code in my html, once I use a div, another thing with a cookie would be ideal to save the cookie until the browser remains.

  • Right, you could be at the event onunload delete the cookie

  • I couldn’t even assemble, could you give me an example of what the code would be like?

  • You can set the cookie without the expiration date, in which case it will be removed at the end of the browser session when it is closed. I changed the function to allow you to do this, then you use setCookie('preload', 1)

  • 1

    Fernando without wanting to abuse you can give me a contact so I can resolve this issue?

  • @Renato which your email?

  • [email protected] @fernandoandrade

Show 2 more comments

Browser other questions tagged

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