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
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
– Caffé
I haven’t treated SQL Injection yet. Anyway, thank you for reminding me :)
– Ricardo
isset($_SESSION['usuario'])
does not shoot error like Notice: Undefined index: usuario. The error may not be on this line.– Papa Charlie
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 F.
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"); }
– Ricardo
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.
– Ricardo
@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
– Ricardo
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?
– Ricardo