fivestringsurf Posted August 28, 2009 Share Posted August 28, 2009 hello there, Would like some general advice regarding re-routing users. (php5) I've been using two methods for re-directing users to a new page within a website- header() or include() What's the best way? Are there advantages to one over the other....for instance in a member system sometimes I'll use something like this to re-route non-logged in users: if (!isset($_SESSION['c_id'])) { $msg ='hey loser, you're not welcome here!'; include('../contact.php'); exit(); }else{ $c_id = $_SESSION['c_id']; } Other times I'll use something like this: if (isset($_SESSION['c_id'])) { header('location:edit_profile.php'); } Now, I like using include() because I can include variables like my $msg above, but (there's always a but isn't there!) I run into header/footer navigation bar linkage problems when using relative links within my included header and footer files .This appears to happen for pages located in different directories. I started using header() to re-route users because i wasn't getting the problems wth my included nav bar links, but this method I can't have any variables or you get that infamous header warning that everyone and their grandma has had at somepoint (i don't want to use url vars) Anyone familiar with these issues? What do you guys do? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/172212-solved-redirect-header-vs-include/ Share on other sites More sharing options...
Alex Posted August 28, 2009 Share Posted August 28, 2009 header('Location: ..'); is used for redirecting and include() is not, include is used for including other files, not re-routing at all. Quote Link to comment https://forums.phpfreaks.com/topic/172212-solved-redirect-header-vs-include/#findComment-907997 Share on other sites More sharing options...
waynew Posted August 28, 2009 Share Posted August 28, 2009 You could set the error message in a session variable if you like and echo that out after using header() Quote Link to comment https://forums.phpfreaks.com/topic/172212-solved-redirect-header-vs-include/#findComment-907998 Share on other sites More sharing options...
fivestringsurf Posted August 28, 2009 Author Share Posted August 28, 2009 so.... don't use include? use header() -ALWAYS...and set a session variable before leaving the page and grab it upon landing on the new page? Quote Link to comment https://forums.phpfreaks.com/topic/172212-solved-redirect-header-vs-include/#findComment-908002 Share on other sites More sharing options...
waynew Posted August 28, 2009 Share Posted August 28, 2009 You could do it that way. You could also keep a list of errors in an array and only pass the index of the error message through the URL and then take it out there. Quote Link to comment https://forums.phpfreaks.com/topic/172212-solved-redirect-header-vs-include/#findComment-908003 Share on other sites More sharing options...
fivestringsurf Posted August 28, 2009 Author Share Posted August 28, 2009 aha, that's why i come here....you guys are slick. really slick. I didn't even think of that. Is that what you do waynewex? I could see that adding to the whole modularity of the site too...all you messages are in say one file and just include it at the top of each page. The thing is though I've always had some trouble when including functions and variables...i seem to have to make anything i am including global to get it to work. Not sure if that is dangerous or uses excess resources? could i just create a php file with my msg list and include it at the top of each page? Quote Link to comment https://forums.phpfreaks.com/topic/172212-solved-redirect-header-vs-include/#findComment-908009 Share on other sites More sharing options...
waynew Posted August 28, 2009 Share Posted August 28, 2009 Create a config file and require it at the top of all your pages. Put session_start() at the top of the file and then load arrays from there. Quote Link to comment https://forums.phpfreaks.com/topic/172212-solved-redirect-header-vs-include/#findComment-908249 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.