Ehsan_Nawaz Posted June 16, 2021 Share Posted June 16, 2021 <?php require("config/config.php"); if(isset($_SESSION['username'])){ $userLoggedIn = $_SESSION['username']; $user_detail = mysqli_query($con, "SELECT * FROM users WHERE username='$userLoggedIn'"); $user = mysqli_fetch_array($user_detail); echo $user['First_Name']; } ?> In the Same Page line:33 --> <?php echo $user['First_Name']?> When i execute my code i show the error Notice: Undefined variable: user in C:\xampp\htdocs\social\include\header.php on line 33 Quote Link to comment https://forums.phpfreaks.com/topic/312920-please-help-me-in-this-error/ Share on other sites More sharing options...
Barand Posted June 16, 2021 Share Posted June 16, 2021 Before you can use $_SESSION you must call "session_start()". Put it at the start of the code. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312920-please-help-me-in-this-error/#findComment-1587277 Share on other sites More sharing options...
Ehsan_Nawaz Posted June 17, 2021 Author Share Posted June 17, 2021 WHEN I START SESSION ERROR SHOW ME SESSION_START() (A SESSION HAD ALREADY BEEN STARTED) Quote Link to comment https://forums.phpfreaks.com/topic/312920-please-help-me-in-this-error/#findComment-1587299 Share on other sites More sharing options...
Barand Posted June 17, 2021 Share Posted June 17, 2021 No need to shout. It must be in your config file (but we can't possibly know that) Quote Link to comment https://forums.phpfreaks.com/topic/312920-please-help-me-in-this-error/#findComment-1587301 Share on other sites More sharing options...
cyberRobot Posted June 17, 2021 Share Posted June 17, 2021 18 hours ago, Ehsan_Nawaz said: When i execute my code i show the error Based on the code provided, $user is only being set if the SESSION variable for "username" is set. If you haven't already, I would check to see what that SESSION variable contains when you're getting the error. It's difficult to tell how you should proceed with the information provided. In the end, you'll likely need to test whether $user is set before trying to use it. How you do that depends on the purpose of the page and how your script is set up. Quote Link to comment https://forums.phpfreaks.com/topic/312920-please-help-me-in-this-error/#findComment-1587304 Share on other sites More sharing options...
Psycho Posted June 17, 2021 Share Posted June 17, 2021 Try putting this bit of debugging code in your script right before your if() statement. What does it produce when you run the script? if(!isset($_SESSION)) { echo "Session was not started yet"; } else { echo "Session data:<br>"; echo "<pre>".print_r($_SESSION, true)."</pre>"; } Quote Link to comment https://forums.phpfreaks.com/topic/312920-please-help-me-in-this-error/#findComment-1587327 Share on other sites More sharing options...
Ehsan_Nawaz Posted June 18, 2021 Author Share Posted June 18, 2021 Here is the config file code <?php ob_start(); session_start(); $timeOne = date_default_timezone_set("Europe/London"); $con = mysqli_connect("localhost","root","","social"); if(mysqli_connect_errno()){ echo"Filled to connect"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/312920-please-help-me-in-this-error/#findComment-1587357 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.