How to get the username / visitor and write it on the screen?

0

Do you know how to ask the user name and use it? I'm setting up a website that is also a virtual store. I want the store to be very nice to enter, Asking her name, treating her well, saying "Okay, This item went to your cart, Amanda." But I'd like to know, how do I ask the person's name, and save it on a cookie? E How can I use the whole site name with the cookie? I've already looked, but just ask the name, and do not use it. Just welcome. I would like to use the code throughout the site, saluting the user by talking to him. For this, I believe a cookie is required.

Thank you.

    
asked by anonymous 18.05.2015 / 02:58

3 answers

2

To save a name in the cookie:

setcookie("nome", "Nome da Pessoa", time()+3600, "/");

To use the cookie:

echo $_COOKIE["nome"];

You can set the time for all you want. The example is about to expire in one hour (3600 seconds).

    
18.05.2015 / 03:39
1

I do not see what problem you are having.

window.String.prototype.novaString=function(de,ate){
    try{
        return this.split(de)[1].split(ate)[0];
    }catch(e){}
    return;
}
if(!document.cookie.novaString("nome=",";")){
    document.cookie="nome="+prompt("Digite seu nome:","");
}
document.getElementById("carrinho").onclick=function(e){
    alert("Pronto, Esse item foi para seu carrinho, "+document.cookie.novaString("nome=",";")+".");
}
<input value="Adicionar ao carrinho" type="button" id="carrinho">

link

    
18.05.2015 / 04:11
1

An alternative with Javascript is to use window.sessionStorage or window.localStorage , the explanation to decide when to use one or other has already been answered here .

DEMO in JSFiddle

if(window.sessionStorage){

    if(!sessionStorage.getItem("nome")){
        // Não há um valor definido ainda, então é exibido o prompt para ser inserido o nome.
        // Se o usuário não preenchê-lo, será utilizado o valor "usuário" para referir-se a ele.
        var nome = prompt("Qual o seu nome?") || "usuário";
        sessionStorage.setItem("nome", nome);
    }

    // Há um valor definido, então a mensagem é exibida.
    alert("Olá " + sessionStorage.getItem("nome") + "!");
}

PS: For what you want to do, the solution and the link pointed to in comments by rafaels88 is the best choice.

    
18.05.2015 / 09:14