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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.