Ghamdan Posted November 7, 2012 Share Posted November 7, 2012 I am trying to create a script that redirect the none admin users. The code below: session_start(); function redirct_none_admin($check ='user_id', $target='index.php'){ if(!isset($_SESSION[$check])) { $url = BASE_URL . $target; header("Location: $url"); exit(); } } function call: // Redirect non-administrators: redirct_none_admin('user_admin'); I could not redirect the none-admin user. Is there anything wrong with this code? How to fix it? Thank you. Link to comment Share on other sites More sharing options...
Edward Posted November 7, 2012 Share Posted November 7, 2012 session_start(); function redirect_none_admin($target='index.php'){ if(isset($_SESSION['user_id'])) { $url = BASE_URL . $target; header("Location: $url"); exit(); } } Function call: // Redirect non-administrators: redirect_none_admin(); Link to comment Share on other sites More sharing options...
Ghamdan Posted November 7, 2012 Author Share Posted November 7, 2012 You need to store the value of the user or admin in the session id, How can I do it? Link to comment Share on other sites More sharing options...
Edward Posted November 7, 2012 Share Posted November 7, 2012 I just updated the post, please reread. Link to comment Share on other sites More sharing options...
Ghamdan Posted November 7, 2012 Author Share Posted November 7, 2012 Thank you Edward. It is working for registered users but not for admin. Link to comment Share on other sites More sharing options...
Edward Posted November 7, 2012 Share Posted November 7, 2012 Thank you Edward. It is working for registered users but not for admin. Awesome! Link to comment Share on other sites More sharing options...
Ghamdan Posted November 7, 2012 Author Share Posted November 7, 2012 It redirects any user even if that user is an admin. Link to comment Share on other sites More sharing options...
Larry Posted November 7, 2012 Share Posted November 7, 2012 I would confirm what your session looks like then. Link to comment Share on other sites More sharing options...
Ghamdan Posted November 7, 2012 Author Share Posted November 7, 2012 I want to redirect only non-admin users. Admin can access the page only. This is what I am trying to do. Link to comment Share on other sites More sharing options...
Larry Posted November 7, 2012 Share Posted November 7, 2012 Yep. You've made that clear. And my suggestion is to confirm what's being stored in the session. Simply reiterating the problem here won't actually do anything to solve the problem. 1 Link to comment Share on other sites More sharing options...
Ghamdan Posted November 7, 2012 Author Share Posted November 7, 2012 Thank you. I have manged to solve the problem. Link to comment Share on other sites More sharing options...
Larry Posted November 7, 2012 Share Posted November 7, 2012 Okay, great. How about sharing what the solution was so that others might benefit from your experience? Link to comment Share on other sites More sharing options...
Ghamdan Posted November 8, 2012 Author Share Posted November 8, 2012 function redirct_none_admin($check ='user_id', $page='index.php'){ if(!isset($_SESSION[$check])) { $url = BASE_URL . $page; header("Location: $url"); exit(); } elseif($_SESSION['user_type'] != 'admin') { $url = BASE_URL . $page; header("Location: $url"); exit(); } } ======================================== It is working but I do not know if it is the right way to do it. Link to comment Share on other sites More sharing options...
Larry Posted November 8, 2012 Share Posted November 8, 2012 If you know you want to just redirect non administrators, then I would check if $_SESSION['user_type'] is set and if it equals admin, redirecting everyone else. The $check conditional isn't necessary. Link to comment Share on other sites More sharing options...
Recommended Posts