Jump to content


Photo

Redirect upon login


  • Please log in to reply
4 replies to this topic

#1 PigsHidePies

PigsHidePies
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 12 September 2006 - 01:57 AM

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.

#2 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 12 September 2006 - 05:47 AM

As long as you're script doesn't output the headers you can use the header() function to redirect...

header("Location:page.php");
I'm considered good looking in some countries

#3 PigsHidePies

PigsHidePies
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 12 September 2006 - 06:06 AM

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?

#4 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 12 September 2006 - 06:55 AM

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
<?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';
}
?>

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 = )
I'm considered good looking in some countries

#5 PigsHidePies

PigsHidePies
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 12 September 2006 - 07:40 AM

It works, brilliant. Thanks a lot mate.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users