store $username and $file variables in the $_SESSION after login | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

store $username and $file variables in the $_SESSION after login

From **index.php** I get the values of the username and password fileds with $_POST **index.php** if(isset($_POST["username"]) && isset($_POST["password"])){ $username = mysql_real_escape_string(strtolower($_POST['username'])); $password = mysql_real_escape_string($_POST['password']); $_SESSION['username'] = $username; $_SESSION['password'] = $password; checkUser($_SESSION['username'], $_SESSION['password']); } Then I store these $username and $password variables inside the $_SESSION and call a function checkUser($_SESSION['username'], $_SESSION['password'])); which sends two parameters. The checkUser() function executes inside **lib.php** **lib.php** session_start(); function checkUser($username, $password){ include "connection.php"; $result = mysqli_query($conn, "SELECT * FROM `data` WHERE `username` = '$username' AND `password` = '$password'") or die("No result".mysqli_error()); $row = mysqli_fetch_array($result); $logic = false; if (($row['username'] == $username) && ($row['password'] == $password)) { $logic = true; echo "HI,".$username; ?> <a href='logout.php'>Log Out</a> <?php $file = $row['file']; echo "<img src='images/users/".$file."' >"; } else{ echo "Failed to login. Username or password is incorrect. Try again."; } } This part is for showing the name of the user and the image according to it. > logout.php works **logout.php** unset($_SESSION["username"]); unset($_SESSION["password"]); unset($_SESSION["file"]); header("Location: index.php"); session_destroy(); **The problem is when I navigate from one page to another, the $_SESSION variable becomes empty. Something is wrong with session. Please help me.**

16th May 2017, 11:42 AM
Ani Naslyan
Ani Naslyan - avatar
3 Answers
+ 1
Have you included the sessions in every page?
16th May 2017, 11:53 AM
Sabrina Aviles
0
Yes
16th May 2017, 2:11 PM
Ani Naslyan
Ani Naslyan - avatar
0
may be you have included logout.php file to your each file .
16th May 2017, 8:24 PM
Shafayat Hossain
Shafayat Hossain - avatar