Session variable "balance" does not update

Asked

Viewed 666 times

1

I have a problem that’s never happened to me before. My balance session variable does not update on the .php. page.It is updated in the database after an UPDATE. But on the page is only updated if I log out and log back in. What will be the problem?

These are my session pages

//init.php
<?php   
session_start(); 
mysql_connect('localhost','root','usbw');   
mysql_select_db('cern_cn');     
date_default_timezone_set('Europe/Lisbon'); 

?>

//login.php
<?php   
include ('init.php');
include ('con_cern_db.php');    
session_regenerate_id();

//CONSULTA DO UTILIZADOR    
$consulta="Select * from login where username='" . $_POST['username'] . "' and password='" . $_POST['password'] . "' AND saldo AND id";     
$resultado=mysql_query($consulta);  

if (mysql_num_rows($resultado)>0) //SE O EMAIL E A PASSWORD COINCIDIREM     
{       
    //COLOCA NA VARIAVEL LINHA OS DADOS DA CONSULTA         
    $linha=mysql_fetch_array($resultado); 
    //COLOCA O EMAIL EM SESSAO 

    $_SESSION['username']=$linha['username'];
    $username=$_SESSION['username'];

    $_SESSION['modo']=$linha['modo'];

    $_SESSION['saldo']=$linha['saldo'];
    $saldo=$_SESSION['saldo'];

    $_SESSION['id']=$linha['id'];
    $id=$_SESSION['id'];

    //REDIRECCIONA A PAGINA PARA A PAGINA SECRETA       
    header("location: index.php");      
}   
else //CASO NAO COINCIDAM   
{   
    //REDIRECCIONA PARA A PAGINA INICIAL REPORTANDO O ERRO
    header("location: index.php?erro=1");  
}        
?>

That’s what I call her

<?php            

if (isset($_SESSION['username'])) //SE EXISTIR AUTENTICAÇÃO            
 {              

    echo ' Olá ' . $_SESSION['username'] . '.<br/>';        

    echo '  ' . $_SESSION['saldo'] . '.Créditos<br/>';          
    //echo ' Modo ' . $_SESSION['modo'] . '.<br/>';     
   echo " ";

    //--------------------------//              
    //TODO O CODIGO PRIVADO AQUI//              
    //--------------------------//                               

}             
else //CASO NÃO ESTEJA AUTENTICADO             
{               
    echo 'Esta é uma àrea reservada, só utilizadores podem ter acesso.';             
 }      
?> 

//logout.php
<?php   
include ('init.php');   
session_destroy();  
header("location: index.php"); 
?>
  • 1

    You should probably be having the same session print out the id and see if it’s changing when you log out after login. When I am with this "freezing" of Session_regenerate_id I use the login page so it always forces to generate a new id for the session. Another qnd you logout the ideal tbm and clear the session with unset or Destroy.

  • Friend @Thallesdaniel, thank you for the reply. I really noticed that the session id after logout was always the same. Now I use session_regenerate_id(); . The problem continues. The field is updated in the database and my balance Session does not update. My logout has session_destroy();. What might be?

  • in the restricted page do so: $_SESSION['balance']='test changing balance'; E then print_r($_SESSION); and see if it changed. The problem may be depending on before Session of an echo in your select to see if exactly what’s going on.

  • when you are inside the protected page change the Session with other values and an F5 to see if you are changing

  • @Thallesdaniel the problem continues. I changed the balance to 10. then I upgraded to 15, I did not. Just logging out and logging in it updates

  • ta doing local?

  • yes, I am with Usbwebserver http://localhost:81/

  • can be tested on the web

  • you already gave a var_dump or a print on your query to see if it’s coming right?

  • I will test location on another computer and on the web tomorrow. the problem can only be local. If in the meantime you get another solution says.

  • had a problem with Session local tbm so I asked. Try restarting the server service.

  • You can create a function that searches in the database if the balance is the same as it is set in Session, if it is, leave it as true, if not, you put $_SESSION[balance] = $Row['balance'] updating to Session.

  • @Thallesdaniel tested with another local server, easyPHP, and the problem is the same

  • I don’t get it worked or n?

  • no, the problem remains the same

  • There is something wrong with your 1.verique $line['balance'] application before assigning it in the session. 2.destroy Session with Session array() Stroy and unset and remember to always do this by placing Session start at the top of the page. Test the web for the result because I did a test on mine yesterday and changed the time. Every page that uses Session needs to start with Session start and not seen on your login screen. And on the logout page the correct I know is so Session start and then Session Destroy believe that why it is not destroying.

  • A doubt, why are you using session_regenerate_id?

  • has several reasons, duplicate Session, problems to change the Session, generate a new etc. http://php.net/manual/en/function.session-regenerate-id.php

Show 13 more comments

1 answer

2


##########login.php##############
<?php 
include('init.php');
session_start(); 

//CONSULTA DO UTILIZADOR    
$consulta="Select * from login where username='" . $_POST['username'] . "' and password='" . $_POST['password'] . "' AND saldo AND id";     
$resultado=mysql_query($consulta);  

    if (mysql_num_rows($resultado)>0) //SE O EMAIL E A PASSWORD COINCIDIREM     
    {       
    //COLOCA NA VARIAVEL LINHA OS DADOS DA CONSULTA         
    $linha=mysql_fetch_array($resultado); 
    //COLOCA O EMAIL EM SESSAO 

    $_SESSION['username']=$linha['username'];
    $username=$_SESSION['username'];

    $_SESSION['modo']=$linha['modo'];

    $_SESSION['saldo']=$linha['saldo'];
    $saldo=$_SESSION['saldo'];

    $_SESSION['id']=$linha['id'];
    $id=$_SESSION['id'];

    //REDIRECCIONA A PAGINA PARA A PAGINA SECRETA       
    header("location: index.php");      
    }   
    else //CASO NAO COINCIDAM   
    {   
    //REDIRECCIONA PARA A PAGINA INICIAL REPORTANDO O ERRO
    session_destroy();
    header("location: index.php?erro=1");  
    }        
?>


##########privada.php############

<?php            
session_start(); 

    if (isset($_SESSION['username'])) //SE EXISTIR AUTENTICAÇÃO            
    {              

    echo ' Olá ' . $_SESSION['username'] . '.<br/>';        
    $_SESSION['saldo'] = '300,00'; #exemplo de receber novo saldo.

    echo '  ' . $_SESSION['saldo'] . '.Créditos<br/>';          
    //echo ' Modo ' . $_SESSION['modo'] . '.<br/>';     
    echo " ";

    //--------------------------//              
    //TODO O CODIGO PRIVADO AQUI//              
    //--------------------------//                               

    }             
    else //CASO NÃO ESTEJA AUTENTICADO             
    {               
    echo 'Esta é uma àrea reservada, só utilizadores podem ter acesso.'; 

    $_SESSION = array();
    session_regenerate_id();
    session_unset();
    session_destroy(); #apenas com esse já resolve tudo mas teste com os outros pra analisar o comportamento
    header("location: login.php"); #voltar para a tela de login 
    }      
?> 



#########logout.php##############

//logout.php
<?php   
session_start(); 
    $_SESSION = array();
    session_regenerate_id();
    session_unset();
    session_destroy(); #se der certo pode remover os anteriores
    header("location: index.php"); 
?>

Fiz um projeto onde tem um var_dump da session e a  alteração de um pagina pra outra

http://sistemasti.16mb.com/david_concha/index.php

Segue código solicitado:



#########index.php###############
<?php
session_start();

$_SESSION['saldo']='450,00';
    echo 'var_dump($_SESSION) vai retornar o resultado abaxixo</br></br>';
    echo '<pre>'.var_dump($_SESSION).'</pre></br>';
    echo '<pre> Id da session: '.session_id().'</pre></br>';
?>
<a href="index2.php">alterar session na index2</a>



###########index2.php##################
<?php
session_start();

$_SESSION['saldo']='300,00';
    echo 'var_dump($_SESSION) vai retornar o resultado abaxixo</br></br>';
    echo '<pre>'.var_dump($_SESSION).'</pre></br>';
    echo '<pre> Id da session: '.session_id().'</pre></br>';
?>
<a href="index.php">voltar session com valor anterior na index</a>
  • is that you can send me the index and index 2 page code with "change Session in index2" "back Session with previous index value". On your service I can change to Sesssions without problem

  • @Davidconcha added what you requested but in summary I only put a new value on the page $_SESSION['balance']='300,00'; that’s all.

  • the way you have it works perfectly. if I assign a session value before I call it if it changes the value it changes the time on the page

  • Isn’t that what you want? vc only can not change when the page is displayed to the client only while loading from one to the other, because the client only html and not the script you made.

  • I wanted him to take the new updated value in the comic and show it. it always shows the old value. if I assign a value like you did it trade on time

  • then you make a query in the bank on page 2 and put to Sesssion to receive this simple new value.

Show 2 more comments

Browser other questions tagged

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