Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
JavaScript code that's supposed to return the user to a login page if a cookie doesn't exist, doesn't work

So I have multiple pages connected that should check if a cookie exists when first visited, as the cookie wont exist it should send the person to the login page where after put in their username, password and time limit for the cookie, the script creates a cookie and redirects them back to the index page. Now everything works besides the redirecting part, because I'm guessing its not fetching the cookie right so it keeps sending you to the login page as soon as you get redirected to where you were supposed to.

function upisi_cookie() {
var danas = new Date();
var istice = new Date();
var username = document.prijava.username.value;
var password = document.prijava.password.value;
var trajanje = parseInt(document.prijava.trajanje.value);

istice.setTime(danas.getTime() + (trajanje * 1000));
let expires = "expires=" + istice.toUTCString();
document.cookie = "User=" + username + ";" + expires + ";path=/";}

Above is the cookie creation code. And this is for fetching the cookie and checking weather it exists or not (and redirection if it does exist)

function getCookie(User) {
    var dc = document.cookie;
    var prefix = User + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else {
        begin += 2;
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
    }
    return decodeURI(dc.substring(begin + prefix.length, end));
}
window.onload = function ControlCookies() {
    var myCookie = getCookie(User);
    stranicaCurrent = document.getElementsByTagName("title");
    if (myCookie == null && stranicaCurrent != "prijava") {
        window.location.href = "---login page location---"; //sends the user to a login page if there are no cookies
    }
    else {
    }
}

 
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda