Jump to content

Archived

This topic is now archived and is closed to further replies.

PigsHidePies

Redirect upon login

Recommended Posts

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.

Share this post


Link to post
Share on other sites
As long as you're script doesn't output the headers you can use the header() function to redirect...

header("Location:page.php");

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]
<?php
if ($_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 = )

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.