Jump to content

Redirect upon login


PigsHidePies

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.
Link to comment
https://forums.phpfreaks.com/topic/20456-redirect-upon-login/
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 = )
Link to comment
https://forums.phpfreaks.com/topic/20456-redirect-upon-login/#findComment-90236
Share on other sites

Archived

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

×
×
  • Create New...

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.