Help to change SESSION variables

Asked

Viewed 1,930 times

-5

I need to change the values of SESSION’s name, login and password, but I’m not getting.

I think the problem is in this part of the code,

if(!isset($_SESSION['usuario'])){ // Se a Session não for iniciada
$nome = 'Nome do Usuário'; // Carrega esse conteúdo
$login = 'usuario'; // Carrega esse conteúdo
$senha = '123'; // Carrega esse conteúdo

}else{

if(isset($_SESSION)){ // Se a Session for iniciada

because when I remove it, I can at least record the changes in SESSION’s using WAMPSERVER.

Below put the code I’m using along with the address http://buziosnegocios.com.br/ of access so that friends can verify what is happening, ie what is not happening ;).

Code:

<?php
@session_start(); // Inicia a session.

if(!isset($_SESSION['usuario'])){ // Se a Session não for iniciada
$nome = 'Nome do Usuário'; // Carrega esse conteúdo
$login = 'usuario'; // Carrega esse conteúdo
$senha = '123'; // Carrega esse conteúdo

}else{

if(isset($_SESSION)){ // Se a Session for iniciada
$nome = $_SESSION["nome"];
if($nome) {
$login = $_SESSION["login"];
if($login) {
$senha = $_SESSION["senha"];
if($senha) {
}}}}}
?>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />

<label>Nome do Usuário: </label><span><?php echo $nome ?></span><br /><br />
<label>Login do Usuário: </label><span><?php echo $login ?></span><br /><br />
<label>Senha do Usuário: </label><span><?php echo $senha ?></span><br /><br />

<form method="post">    
<label>Nome do Usuário:</label><br />
<input type="text" name="nome" value="<?php echo $nome ?>"><br /><br />
<label>Login do Usuário:</label><br />
<input type="text" name="login" value="<?php echo $login ?>"><br /><br />
<label>Senha do Usuário:</label><br />
<input type="text" name="senha" value="<?php echo $senha ?>"><br /><br />
<input type="submit" value="Atualizar" />
</form>

<?php
$nome = $_POST["nome"];
if($nome) {
$_SESSION["nome"] = $nome ;

$login = $_POST["login"];
if($login) {
$_SESSION["login"] = $login ;

$senha = $_POST["senha"];
if($senha) {
$_SESSION["senha"] = $senha ;

header('Location:index.php');
}}}
?>

I await good tips, and thank you all for your attention. Hugs.

3 answers

2


Well if I were to do, which in case it must be what you want to do I would do so.

 <?php 
@session_start(); // Inicia a session.

         if(!isset($_SESSION['usuario'])){ 
      // Se a Session não existir eu crio...
        $nome = 'Nome do Usuário'; // Carrega esse conteúdo
        $login = 'usuario'; // Carrega esse conteúdo
        $senha = '123'; // Carrega esse conteúdo

    //Armazena os dados na sessão que pode ser bidimensiona(array)
         $_SESSION['usuario']['nome']=$nome;
        $_SESSION['usuario']['login']=$login;
        $_SESSION['usuario']['senha']=$senha;

        }else if(isset($_SESSION['usuario'])){
         // Se existir sessão, eu crio aqui
        $nome = $_SESSION['usuario']["nome"];
        $login = $_SESSION['usuario']["login"];
        $senha = $_SESSION['usuario']["senha"];
    }
        ?>

There at the bottom of the code, when sending the form, it would be recommended to do so. First change the button by putting its name

<input type="submit" value="Atualizar" name="atualizar"/>

Ai in php...

      <?php 
    //pega o valor do botao
    $botao=$_POST['atualizar'];
    //verifica se o botao foi clicado
    if($botao=="Atualizar"){
       $nome = $_POST["nome"];
       $login = $_POST["login"];
       $senha = $_POST["senha"];
            if(!empty($nome) && !empty($login) && !empty($senha)) {
            $_SESSION['usuario']["nome"] = $nome ;
            $_SESSION['usuario']["login"] = $login ;
            $_SESSION['usuario']["senha"] = $senha ;

            header('Location:index.php');
            }}
        ?>

I think if I understand your problem correctly, it’ll solve it. Because there in your code Voce takes the name of a session where I do not know where you created, that in the case does not exist, because it would be the session user and not name, password... In case I haven’t understood your situation properly, comment on it so I can help you better. I did not touch those Ifs there at the end, but to give an improved...

Update: Would do so with these Ifs..., instead of the 3, would do everything in one shot. And if you have never used, this function Empty(), it checks if the variable is empty, in which case I denied it using ! Empty(), that is, if it is not empty, there it will do, there I have already checked the 3 joints, because I believe that you want to update only when the 3 are not empty... And about the textarea, I believe it would be the same logic. And about being a beginner in PHP, the beginning is just like that, when you see you will already be doing everything and fixing the mistakes that come up... Hug, any doubt comments there.

  • Hello Localhost, it worked smooth smooth, thank you very much for your guidance. But I ask you a question, I can use that same reasoning for when I use a textarea. And how can I improve these Ifs, give me a hint there because as you have already seen, I am very lay in PHP, ;). I am learning PHP using tutorials taken on Youtube, and I try to apply to learn a little bit with each tutorial. Send your idea to improve Ifs, thanks? Great hug, and once again QUARREL...

  • I edited the Ifs at the end there, take a look...

  • Hello Localhost, I’m trying to use the same logic to use with radio buttons, but I’m not getting it. Is there any way you can give me this one, but show me where I’m going wrong, or will it read differently when it comes to radio buttons? The Code I am using is Below:

  • I’ll be posting the code answering my own question, thanks?

0

Problem solved with the help of friend Miguel, as POST

Below working code:

<?php
if(isset($_POST['status_prod'])) {

if(isset($_POST['status_prod']) && ($_POST['status_prod']) == 0) { // Desabilita o Botão.
    $link_prod = null;
    $status_prod = 'Não';
}else{
    $_POST['status_prod'] == 1; // Habilita o Botão com o Link de destino.
        $link_prod = '<li><a href="'.$end.'prod_index.php" class="nav1">'.$bot_prod.'</a></li><li class="divider"></li>';
        $status_prod = 'Sim';
        }
        $_SESSION['hab_prod']['status_prod'] = $status_prod;
        $_SESSION['hab_prod']['link_prod'] = $link_prod;

        echo "<meta http-equiv='refresh' content='0; URL= menu_halitar_link.php'>
        <script language='javascript'>
        window.alert('Dados atualizados com sucesso!');
        </script>";
        }
?>
        <form method="post">
            <label>Habilitar o Link <?php echo $bot_prod ?>?</label><br><br>
            <input type='radio' name='status_prod' value='1' checked="checked"/><label>Sim</label>
            <input type='radio' name='status_prod' value='0'/><label>Não</label>
            <input type="submit" value="Atualizar">
        </form>

Hugs to all.

0

I’m editing and answering my own question with the code working.

<?php 
@session_start(); // Inicia a session.
if(!isset($_SESSION['rede_sociais'])){ 
// Se a Session não existir eu crio...
$face_status = 'Sim'; // Carrega esse conteúdo

//Armazena os dados na sessão que pode ser bidimensiona(array)
$_SESSION['rede_sociais']['face_status']=$face_status;

}else if(isset($_SESSION['rede_sociais'])){

// Se existir sessão, eu crio aqui
    $face_status = $_SESSION['rede_sociais']["face_status"];
}

<?php
//pega o valor do botao
$botao=$_POST['face'];
//verifica se o botao foi clicado
if($botao=="Atualizar"){
$face_status = $_POST["face_status"];
if(!empty($face_status)) {
$_SESSION['rede_sociais']["face_status"] = $face_status ;

echo "<meta http-equiv='refresh' content='0; URL= cabe_rede_sociais.php'>
<script language='javascript'>
    window.alert('Dados atualizados com sucesso!');
</script>";

}}
?>

<form method="post">
<label>Habilitar o Link do FaceBook?</label><br><br>
<input type='radio' name='face_status' value='Sim' checked="checked"/><label>Sim</label>
<input type="radio" name="face_status" value='Não'><label>Não</label>
<input type="submit" name="face" value="Atualizar">
</form>

I hope it helps other friends with the same problem of recording in SESSION using radio buttons (Tag).

Big hug to all.

  • Oops, I think it’s all right, except for the opening ta tag <?php in the middle of the code, there before //takes the value of the button $boot=$_POST['face']; And there where you used js to redirect... If you only used to redirect I recommend using in php so header("Location:cabe_rede_social.php"); Because then it redirects directly from php, because it disallows php sends to the browser the js code, and after processing it redirects, with php already does direct, there is a greater delay difference when using non-local server. I think that’s it, anything comments there.

  • Hello Localhost, beauty? I followed your advice and already fixed the flaws in the code above, thanks even!!! But if possible take a look at this POST (http://answall.com/questions/131598/help-com-tag-radio-para-gravar-em-session), and help me solve the problem I mentioned in it. Big Hug.

  • @Murilocabral you have answered the question you linked http://answall.com/q/131598/3117 it does not answer your question?

Browser other questions tagged

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