PigsHidePies Posted September 12, 2006 Share Posted September 12, 2006 I'm sure this has been asked before but I couldnt find it within the search results so here it is:How do I, after successfully logging a user in, redirect her to a new page? I would want the page to be a default location (like a member home page) where all logged in users go to first and those that aren't logged in are directed toward the default login page. I would think this would be a standard operation but all the searches I've done on google have come up with very little regarding this, hopefully one of you can solve this for me, many thanks. Quote Link to comment Share on other sites More sharing options...
gijew Posted September 12, 2006 Share Posted September 12, 2006 As long as you're script doesn't output the headers you can use the header() function to redirect...header("Location:page.php"); Quote Link to comment Share on other sites More sharing options...
PigsHidePies Posted September 12, 2006 Author Share Posted September 12, 2006 Will this let me still validate a user? Im a little confused about the whole header(Locaton:) function. Does this have to go at the very top of the page and if so how can I validate a user if it redirects before the validaton process occurs? Quote Link to comment Share on other sites More sharing options...
gijew Posted September 12, 2006 Share Posted September 12, 2006 You should have an if/else conditional set up to verify the user. If the user passes your verification you set your sessions or cookie up and then use the header() function.Quick example[code]<?phpif ($_POST['userName'] && $_POST['userPassword']) { $sql = mysql_query("SELECT username, password FROM users WHERE username = '".$_POST['userName']."' AND password = '".md5($_POST['userPassword'])."'"); if (mysql_num_rows($sql) == 0) { $errMsg[] = 'That user does not exist'; } else { // User exists, password matches $_SESSION['yourVars'] = 'Whatever'; header("Location:your_redirect_page.php"); }} else { $errMsg[] = 'Username and password are required';}?>[/code]That's fairly basic there but it will give you the general idea. As a heads up, ensure that when you are using the header() function that you do not output anything to the browser first. By that I mean you should not be outputting anything to the browser when the script succeeds or else you will get an error. Play around and see what you can figure out = ) Quote Link to comment Share on other sites More sharing options...
PigsHidePies Posted September 12, 2006 Author Share Posted September 12, 2006 It works, brilliant. Thanks a lot mate. Quote Link to comment 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.