NaderH Posted March 22, 2023 Share Posted March 22, 2023 (edited) Hello I have this error Quote warning: trying to access array offset on value of type bool in sql.php on line 195 it appears between all pages while I am navigating from one page to another, this error appears and disappears immediately, but it appears and remains present when viewing sales reports page. /*--------------------------------------------------------------*/ /* Function for checking which user level has access to the page /*--------------------------------------------------------------*/ function page_require_level($require_level){ global $session; $current_user = current_user(); $login_level = find_by_groupLevel($current_user['user_level']); //if user not login if (!$session->isUserLoggedIn(true)): $session->msg('d','Please Sign in'); redirect('index.php', false); //if Group status Deactive elseif($login_level['group_status'] === '0'): //Line 195 $session->msg('d','User Banned'); redirect('home.php',false); //checking logged in User level and Require level is Less than or equal to elseif($current_user['user_level'] <= (int)$require_level): return true; else: $session->msg("d", "Error"); redirect('home.php', false); endif; } Edited March 22, 2023 by NaderH Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/ Share on other sites More sharing options...
mac_gyver Posted March 22, 2023 Share Posted March 22, 2023 the error means that $login_level is a boolean (true or false) value, but the code expects it to be an array with a group_status element. either something has changed to cause the find_by_groupLevel() function to return an unexpected value OR that part of the code (which is using an exact comparison with a string consisting of '0') never actually worked, wasn't fully tested, and was probably always producing this error, but the error wasn't being reported/displayed. what is the code for the find_by_groupLevel() function and what is the expected value it should return for both a non-banned and a banned user? Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/#findComment-1606719 Share on other sites More sharing options...
NaderH Posted March 22, 2023 Author Share Posted March 22, 2023 2 hours ago, mac_gyver said: the error means that $login_level is a boolean (true or false) value, but the code expects it to be an array with a group_status element. either something has changed to cause the find_by_groupLevel() function to return an unexpected value OR that part of the code (which is using an exact comparison with a string consisting of '0') never actually worked, wasn't fully tested, and was probably always producing this error, but the error wasn't being reported/displayed. what is the code for the find_by_groupLevel() function and what is the expected value it should return for both a non-banned and a banned user? It is to determine if the one who logging in is an admin or a seller or a user to display the home page that should appear to him according to his group(permission) level. Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/#findComment-1606722 Share on other sites More sharing options...
mac_gyver Posted March 22, 2023 Share Posted March 22, 2023 programming is an exact science. when someone points out a problem with some code and asks what is that code, it means you need to post exactly the code that was mentioned. if someone asks what is the expected value for something, it means to post exactly what that value is. Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/#findComment-1606724 Share on other sites More sharing options...
NaderH Posted March 22, 2023 Author Share Posted March 22, 2023 (edited) 20 minutes ago, mac_gyver said: programming is an exact science. when someone points out a problem with some code and asks what is that code, it means you need to post exactly the code that was mentioned. if someone asks what is the expected value for something, it means to post exactly what that value is. well, the expected value it should return is 0 or 1 to check if the user is allowed to log in or is banned, but for some reason it doesn't work, I mean if you banned the user he will be able to login. consider that I am still a beginner. Edited March 22, 2023 by NaderH Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/#findComment-1606727 Share on other sites More sharing options...
NaderH Posted March 22, 2023 Author Share Posted March 22, 2023 (edited) 30 minutes ago, mac_gyver said: programming is an exact science. when someone points out a problem with some code and asks what is that code, it means you need to post exactly the code that was mentioned. if someone asks what is the expected value for something, it means to post exactly what that value is. Well, I changed that lint to elseif($login_level['group_status'] != '1') instead of elseif($login_level['group_status'] === '0') and it caused that all users can login but can't view any page. Edited March 22, 2023 by NaderH Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/#findComment-1606728 Share on other sites More sharing options...
ginerjm Posted March 22, 2023 Share Posted March 22, 2023 (edited) I think your tests are giving you incorrect results. See the comments I've added to your code below. function page_require_level($require_level) { global $session; $current_user = current_user(); $login_level = find_by_groupLevel($current_user['user_level']); //if user not login if (!$session->isUserLoggedIn(true)) { $session->msg('d','Please Sign in'); redirect('index.php', false); } //if Group status Deactive elseif($login_level['group_status'] === '0') // is login_level a string??? { $session->msg('d','User Banned'); redirect('home.php',false); } elseif($current_user['user_level'] <= (int)$require_level) // or is it an integer???? return true; else { $session->msg("d", "Error"); redirect('home.php', false); } } Edited March 22, 2023 by ginerjm mistake in copying OP code Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/#findComment-1606731 Share on other sites More sharing options...
ginerjm Posted March 22, 2023 Share Posted March 22, 2023 I"m re-posting my version of your code again with some new comments. function page_require_level($require_level) { global $session; $current_user = current_user(); $login_level = find_by_groupLevel($current_user['user_level']); // SHOW THE RESULT OF ABOVE CALL //if user not login // SHOW THE VALUE OF $SESSION BEFORE USING IT // EX: ECHO "session is </pre>",print_r($session,true),"</pre>"; if (!$session->isUserLoggedIn(true)) { $session->msg('d','Please Sign in'); redirect('index.php', false); } //if Group status Deactive elseif($login_level['group_status'] === '0') // is login_level a string??? { $session->msg('d','User Banned'); redirect('home.php',false); } elseif($current_user['user_level'] <= (int)$require_level) // or is it an integer???? return true; else { $session->msg("d", "Error"); redirect('home.php', false); } } is your use of redirect() the same as using the php header() command? If so you should be doing an exit() right after running it. Try doing my suggestions to debug your process and see what the vars you are using actually contain. Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/#findComment-1606733 Share on other sites More sharing options...
NaderH Posted March 25, 2023 Author Share Posted March 25, 2023 Thanks a lot everybody I fixed it, it was because the PHP version. Quote Link to comment https://forums.phpfreaks.com/topic/316029-warning-trying-to-access-array-offset-on-value-of-type-bool-in-sqlphp/#findComment-1606808 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.