1

重複の可能性:
PHP で SO のような returnurl を実装するには?

ログインしていないユーザーが自分のサイトを閲覧していてリンクをクリックすると、ログインページ(jQueryライトボックスウィンドウで開く)に移動し、ログイン後に元に戻るようにしようとしています彼らがいた前/元のページ。

この単純なコードを使用してこれを達成しようとしましたが、これを使用するとログインページが表示されず、前/元のページに直接移動するため、ユーザーはログインできません。

if(!empty($_SERVER['HTTP_REFERER']))
{
  header('Location: '.$_SERVER['HTTP_REFERER']);
}

これが私のログインスクリプトコードのすべてです:

HTML:

<form action="login.php" method="post" target="_top"  >
  <div class="row email">
    <input type="email" id="email" name="email" placeholder="Email" value="<?php echo htmlentities($email); ?>" />
  </div>
  <div class="row password">
    <input type="password" id="password" name="password" placeholder="Password" value="<?php echo htmlentities($email); ?>" />
  </div>
  <input type="submit" name="submit" class="submit-login" value="Login >
</form>

PHP:

<?php
  if (logged_in())
  {
    redirect_to("dashboard.php");
  }

  include_once("includes/form_functions.php");

  // START FORM PROCESSING
  if (isset($_POST['submit'])) { // Form has been submitted.
    $errors = array();

    // perform validations on the form data
    $required_fields = array('email', 'password');
    $errors = array_merge($errors, check_required_fields($required_fields, $_POST));

    $fields_with_lengths = array('email' => 30, 'password' => 30);
    $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

    $email = trim(mysql_prep($_POST['email']));
    $password = trim(mysql_prep($_POST['password']));
    $hashed_password = md5($password);

    if ( empty($errors) ) {
      // Check database to see if email and the hashed password exist there.
      $query = "SELECT id, email, close_account ";
      $query .= "FROM ptb_users ";
      $query .= "WHERE email = '{$email}' ";
      $query .= "AND password = '{$hashed_password}' ";
      $query .= "AND close_account = '0' ";
      $query .= "LIMIT 1";
      $result_set = mysql_query($query);
      confirm_query($result_set);
      if (mysql_num_rows($result_set) == 1) {
        // email/password authenticated
        // and only 1 match
        $found_user = mysql_fetch_array($result_set);
        $_SESSION['user_id'] = $found_user['id'];
        $_SESSION['email'] = $found_user['email'];
        $_SESSION['sub_expires'] = $found_user['subscription_expires'];

        $result = mysql_query("UPDATE ptb_users SET user_online='Online' WHERE id=".$_SESSION['user_id']."") 
                or die(mysql_error());


        redirect_to("dashboard.php");
      } else {
        // email/password combo was not found in the database
        $message = "<div class=\"infobox\"><strong>Email/Password combination incorrect.</strong><br />"
                .= "Please make sure your caps lock key is off and try again.</div><br/>";
      }
    } else {
      if (count($errors) == 1) {
        $message = "<div class=\"infobox\">There was 1 error in the form.<div>";
      } else {
        $message = "<div class=\"infobox\">There were " . count($errors) . " errors in the form.<div>";
      }
    }
  } else { // Form has not been submitted.
    if (isset($_GET['logout']) && $_GET['logout'] == 1) {
      $message = "<div class=\"infobox\">You are now logged out.</div>";
    } 
    $email = "";
    $password = "";
  }
?>
<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
<?php if (!empty($errors)) { display_errors($errors); } ?>
4

2 に答える 2

2

認証が必要なページに誰かがアクセスしようとした場合、その URL を $_SESSION 変数に保存します。次に、ログイン ページにリダイレクトします。ログイン後、その $_SESSION 変数が存在するかどうかを確認します。その場合は、その URL にリダイレクトします。

于 2013-01-22T14:06:38.953 に答える
1

AJAXを使用してログインでき、ログインに成功したら、ページをリロードするだけです。それは多くの点でより役立つでしょう。

たとえば、ユーザー名/パスワードが間違っていると、ページの読み込みでエラーが表示されるのを待つのではなく、クエリライトボックスウィンドウにエラーが表示される場合があります。

于 2013-01-22T14:10:31.007 に答える