【问题标题】:Redirect to different page if user account is not activated如果用户帐户未激活,则重定向到不同的页面
【发布时间】:2011-09-15 17:50:27
【问题描述】:

一段代码

$LoginRS__query=sprintf("SELECT user_handle, user_password FROM users_entity WHERE user_handle=%s AND user_password=%s AND activation_status='Active'",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

  $LoginRS = mysql_query($LoginRS__query, $f12_database_connect) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";


    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['logged_in']="True";
    $_SESSION['MM_UserGroup'] = $loginStrGroup;       

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}

数据库查询activation_status="Active" 的用户。该字段具有"Pending""Active" 这两个值之一。如果状态为"Pending",我想发送到另一个页面。该页面适用于尚未激活其帐户的用户。如何获取该结果并将其分配给变量?还是有更好的方法?

【问题讨论】:

    标签: php mysql authentication activation


    【解决方案1】:

    只选择数据不参考activation_status,用if语句检查。

    编辑:

    您可能正在寻找的代码:

    $LoginRS__query=sprintf("SELECT user_handle, user_password, activation_status FROM users_entity WHERE user_handle=%s AND user_password=%s",
        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
    
      $LoginRS = mysql_query($LoginRS__query, $f12_database_connect) or die(mysql_error());
      $loginFoundUser = mysql_num_rows($LoginRS);
      if ($loginFoundUser) {
        // Make sure user is activated.
        $row = mysql_fetch_Array($LoginRS);
        if ($row['activation_status'] === 'Pending') {
          header("Location: some-other-page");
        }
    
        $loginStrGroup = "";
    
    
        //declare two session variables and assign them
        $_SESSION['MM_Username'] = $loginUsername;
        $_SESSION['logged_in']="True";
        $_SESSION['MM_UserGroup'] = $loginStrGroup;       
    
        if (isset($_SESSION['PrevUrl']) && true) {
          $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
        }
        header("Location: " . $MM_redirectLoginSuccess );
      }
      else {
        header("Location: ". $MM_redirectLoginFailed );
      }
    }
    

    【讨论】:

    • 如何用 if 语句检查它。会是 if(loginFoundUser['activation_status']) 吗?如何为我的 if 语句选择该列?
    【解决方案2】:

    这是解决方案和正确编码...

    <?php
    $LoginRS_StrQuery   =   'SELECT user_handle, user_password, activation_status FROM users_entity '.
                            "WHERE user_handle=%s AND user_password=%s LIMIT 0,1";
    $LoginRS__query     =   sprintf($LoginRS_StrQuery, GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
    $LoginRS            =   mysql_query($LoginRS__query, $f12_database_connect);
    if(is_resource($LoginRS) && mysql_num_rows($LoginRS) > 0){
        $loginStrGroup  =   '';
    
        //declare two session variables and assign them
        $_SESSION['MM_Username']    =   $loginUsername;
        $_SESSION['logged_in']      =   'True';
        $_SESSION['MM_UserGroup']   =   $loginStrGroup;
    
        $LoggedUser     =   mysql_fetch_assoc($LoginRS);
        if($LoggedUser['activation_status'] === 'Pending'){
            // redirect him/her to wherever you want
        } elseif(isset($_SESSION['PrevUrl']) && true){
            $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
            exit;
        } else{
            header("Location: " . $MM_redirectLoginSuccess );
            exit;
        }
    } else{
        header("Location: ". $MM_redirectLoginFailed );
        exit;
    }
    ?>
    

    【讨论】:

    • 如果我希望它为 ='Pending' 重定向到另一个页面,我是否只需将其替换为 'Active'?
    • $_SESSION['logged_in'] = 'True';, isset($_SESSION['PrevUrl']) &amp;&amp; true 你称之为正确的编码? -1
    • @atno:我称这种正确的编码符合他的标准。如果我改变了那些,谁知道我们头顶上会落下什么天空。如果我更改他的所有身份验证将南下,他可能会在其他地方使用if($_SESSION['logged_in'] === 'True') 进行检查。另外,&amp;&amp; true,他可能已经将其用于测试或其他什么...所以,在你投反对票之前考虑一下上下文。
    • @Kevin Oluseun Karimu:是的,您只需将其更改为 Pending。我为你更新了代码。这行elseif(isset($_SESSION['PrevUrl']) &amp;&amp; true) 上有一个不必要的&amp;&amp; true 强制条件。因此,请确保删除 &amp;&amp; true,如果您不是故意将其添加到那里。
    猜你喜欢
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2015-04-30
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多