hain Posted May 31, 2020 Share Posted May 31, 2020 I have these two files and I need to solve the problem that I need the user data in the role column to be taken when logging in. I have written it so that the data can be extracted from the database, but it does not work for me that it is “forwarded” from process.php to admin.php . Please don’t know what the error is that the admin.php file doesn’t want to load $ _SESSION['Role'] from process.php ? Thanks to everyone for helping and here is the code: process.php <?php require_once('connect.php'); session_start(); if(isset($_POST['Login'])) { if(empty($_POST['Username']) || empty($_POST['Password'])) { header("location:index.php?Empty= Please Fill in the Blanks"); } else { $query="select * from role_test where Username='".$_POST['Username']."' and Password='".md5($_POST['Password'])."'"; $result=mysqli_query($con,$query); if(mysqli_fetch_assoc($result)) { $_SESSION['User']=$_POST['Username']; while($row = mysqli_fetch_array($result) ){ $_SESSION['Role']=$row['role']; } header("location:admin.php"); } else { header("location:index.php?Invalid= Please Enter Correct User Name and Password "); } } } else { echo 'Not Working Now Guys'; } ?> admin.php <?php session_start(); if(!(isset($_SESSION['User']))) { header("Location: index.php"); exit(0); } ?> <!DOCTYPE html> <html> <head> <title>Role</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <?php $_SESSION['Role']=$role; echo $role; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/ Share on other sites More sharing options...
gw1500se Posted May 31, 2020 Share Posted May 31, 2020 session_start is not first in process.php. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578558 Share on other sites More sharing options...
hain Posted May 31, 2020 Author Share Posted May 31, 2020 Yes, but login works without problems, only $ _SESSION ['Role'] does not want to connect to admin.php from process.php. I just need to solve that. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578560 Share on other sites More sharing options...
Barand Posted May 31, 2020 Share Posted May 31, 2020 In admin,php, where is $role being set? Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578562 Share on other sites More sharing options...
hain Posted June 1, 2020 Author Share Posted June 1, 2020 Here $_SESSION['Role']=$role; Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578565 Share on other sites More sharing options...
Barand Posted June 1, 2020 Share Posted June 1, 2020 No. That line is trying to set $_SESSION['Role'] to the value of (apparently undefined) $role. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578566 Share on other sites More sharing options...
hain Posted June 1, 2020 Author Share Posted June 1, 2020 This is exactly what I need to get the role of the user who logs in to the process.php which should be this: while ($ row = mysqli_fetch_array ($ result)) { $ _SESSION ['Role'] = $ row ['role']; } and then to set it to $ role in admin.php and list it in the echo for the user Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578567 Share on other sites More sharing options...
gw1500se Posted June 1, 2020 Share Posted June 1, 2020 Why use another variable at all? Use $_SESSION['Role'] directly. In any case do you do any error checking after the query to make sure the data is returned to set it in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578573 Share on other sites More sharing options...
hain Posted June 1, 2020 Author Share Posted June 1, 2020 Yes, I also tried echo $ _SESSION ['Role'] and it didn't work, so I really don't know what it is, I'm quite desperate after a few days of solving this problem, I'm completely stuck on it. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578575 Share on other sites More sharing options...
gw1500se Posted June 1, 2020 Share Posted June 1, 2020 What do you mean "it didn't work?" What was the error? You need to provide more detail about what you are getting for us to help you. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578577 Share on other sites More sharing options...
hain Posted June 1, 2020 Author Share Posted June 1, 2020 This is the problem why I write here, I do not know where the mistake is, so I wonder if anyone sees it here and could tell me what the mistake is or correct it and write here what it was. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578579 Share on other sites More sharing options...
gw1500se Posted June 1, 2020 Share Posted June 1, 2020 Not specifically based on what you have posted. At this point we can only tell you that the session variable is not set at the point you are trying to use it. If you don't want tell us the error you are getting or any details then there is little else we can do. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578580 Share on other sites More sharing options...
mac_gyver Posted June 1, 2020 Share Posted June 1, 2020 the OP's logic for fetching data from the SELECT query is all messed up, resulting the (unnecessary) while(){} loop logic being skipped over. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578581 Share on other sites More sharing options...
hain Posted June 1, 2020 Author Share Posted June 1, 2020 (edited) 8 minutes ago, mac_gyver said: Sorry guy, bad post! Edited June 1, 2020 by hain Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578582 Share on other sites More sharing options...
hain Posted June 1, 2020 Author Share Posted June 1, 2020 23 minutes ago, gw1500se said: Not specifically based on what you have posted. At this point we can only tell you that the session variable is not set at the point you are trying to use it. If you don't want tell us the error you are getting or any details then there is little else we can do. good thank you, but the problem is that the result in the echo is none (nothing is displayed), it will be because the variable is not set as you write above, so please advise why the variable is not transferred from prosess.php to admin.php Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578583 Share on other sites More sharing options...
gw1500se Posted June 1, 2020 Share Posted June 1, 2020 Did you move the session start to before the include as I originally suggested? The next step is to include a print of $_SESSION at the end of the script that sets that variable to make sure it is set at the end of that script and again after the session start of the script that uses it. That will determine if the variable is being reset somewhere in the first script or in the 2nd before it is used. This assumes there are no intervening scripts you didn't mention that can mess it up. Quote Link to comment https://forums.phpfreaks.com/topic/310873-how-to-session-get-information-from-the-database/#findComment-1578586 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.