Empty PHP $_SESSION variable

Asked

Viewed 2,775 times

0

I have a login problem. I save the user ID to a session variable as soon as it logs in, but this variable is becoming empty, someone knows why?

PHP

<?php
 session_start();
 $username = $_POST['name'];
 $password = $_POST['pwd'];

 include("conexao.php");
 mysql_select_db("mg", $conn) or print(mysql_error()); 

 $query = "SELECT id_usuario, nm_login, nm_senha, nm_nome, nm_sobrenome, nm_imagem FROM tbl_usuario WHERE nm_login='".$username."' AND nm_senha='".$password."'";

 $result = mysql_query($query,$conn) or die(mysql_error());
 $num_row = mysql_num_rows($result);
 $row=mysql_fetch_array($result);
 if( $num_row >=1 ) {
   echo 'true';
   $_SESSION['usuario']=$row['id_usuario'];
   $_SESSION['login']=$row['nm_login'];
   $_SESSION['nome']=$row['nm_nome'];
   $_SESSION['sobrenome']=$row['nm_sobrenome'];
   $_SESSION['avatar']=$row['nm_imagem'];
 }
 else{
   echo 'false';
 }
?>

On this line it appears empty, if I give F5 it loads the variable

    if(!isset($_SESSION['usuario'])){
        echo "<li><a href='#' id='login-link'>Login</a></li>";
    };

You’re making the mistake

Notice: Undefined index: usuario

Thanks

  • 1

    One thing you will hear a lot here is that your query is subject to SQL Injection and that to log in to this system is not necessary to have valid user and password. Please see: http://www.devmedia.com.br/sql-injection-em-ambientes-web/9733

  • I haven’t treated SQL Injection yet. Anyway, thank you for reminding me :)

  • isset($_SESSION['usuario']) does not shoot error like Notice: Undefined index: usuario. The error may not be on this line.

  • php’s Session is working perennially, but it seems that at some point Voce did not set the index $_SESSION['user'] or maybe it has reset, in line with echo 'true' is showing true word in your browser?

  • Olimon, this echo 'true' is only used in the return pro jQuery: if(html=='true') { Document.Form1.loading.style.visibility = "Hidden"; $("#login-form"). fadeOut("slow"); $("#background-on-popup"). fadeOut("slow"); $("#profile"). fadeOut("fast"); $("#profile"). load("profile.php"); $("#profile"). fadein("fast"); }

  • The strange thing is that if you had lost the value of Session, giving the F5 it would not be loaded with the correct value.

  • @Papacharlie Papa Charlie, the error is on this line: echo $sql1 = "SELECT nm_image FROM tbl_usuario WHERE id_usuario =". $_SESSION['user']; Apparently because $_SESSION['user'] is empty

  • I think I figured out what’s going on. When the user logs in, only a php file is loaded inside a DIV at the top, only that this top had already been loaded previously with no value in Session, understood?

Show 3 more comments

1 answer

1

This may occur if for some reason you are recreating your session every post/Ubmit/refresh.

If this happens, it will lose the values of the old Sesame.

Take a look, if session_start() code is not running with each page interaction.

Try using a Singleton in Session to avoid this problem:

if (!isset($_SESSION)){
    session_start();
}
  • All places of code that have session_start(), were already as you commented if(!isset($_SESSION)){ session_start(); } But still continue loading EMPTY

  • I did a similar bug search, in American stackoverflow, a user solved the same problem by separating the code from the logout button. http://stackoverflow.com/questions/24411588/php-session-lost-after-refreshing-page

  • Another suggestion I found is to use the Other suggestion I found is to use the session_regenerate_id() and the session_write_close()

  • never used this session_regenerate_id(), I will give a search... Vlw

Browser other questions tagged

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