0

ヘッダー関数を使用して、特定の条件に基づいて別のページを検索しています。メールボックスを監視していますが、コードは送信者アドレスに基づいて別のページにリダイレクトされます。1つを除いてすべてのヘッダーが機能しています。送信者が既存のグループに属していない場合は、new.phpにリダイレクトしたいと思います。しかし、それはリダイレクトではありません。理由がわかりません。私を助けてください。

<?php 
session_start();

$server = '{server}INBOX';
$username = 'aaa@bbb.com';
$password = 'password';
require_once '../swift/lib/swift_required.php';
include('connection.php');


$connection  = imap_open($server,$username,$password) or die('Cannot connect to Gmail: ' .    imap_last_error());

$_SESSION['connection']=$connection;

$result = imap_search($connection,'UNSEEN');
if($result) {

    rsort($result);

    foreach($result as $email_number) 
    {         

        $header = imap_headerinfo($connection, $email_number);

        $fromaddr = $header->from[0]->mailbox . "@" . $header->from[0]->host;

        $query = "select * from usergroup where email='$fromaddr'";
        $_SESSION['fromaddr']=$fromaddr;

        $result1 = mysql_query($query) or die($query."<br/><br/>".mysql_error());


        while($line=mysql_fetch_array($result1,MYSQL_ASSOC))
        {
            $email=$line['email'];
            $group=$line['group'];

            if(mysql_num_rows($result1) == 1){

                if($group == 1){
                    header("Location: facilitator.php");
                }
                elseif($group == 2){
                    header("Location: learner.php");
                }

            }
            elseif (mysql_num_rows($result1) == 0) {
                header("Location: new.php");
            }

        }
    }

}
elseif (!$result)
{
     echo "No unread messages found";
}


?>
4

2 に答える 2

3

while ループ内でそのリダイレクトをネストしているように見えます。行がないため、 while 条件mysql_fetch_array()はすぐに返さFALSEれ、意図したリダイレクトを含むブロック全体をスキップします。

テスト for をループのmysql_num_rows()外に移動します。while

// Test for rows and redirect BEFORE entering the while loop.
if (mysql_num_rows($result1) === 0) {
  header("Location: new.php");
  // Always explicitly call exit() after a redirection header!
  exit();
}
// Otherwise, there are rows so loop them.
while($line=mysql_fetch_array($result1,MYSQL_ASSOC))
{
   $email=$line['email'];
   $group=$line['group'];

   if($group == 1){
     header("Location: facilitator.php");
   }
}

whileフェッチする予定の行数によっては、実際にはループがまったく必要ない場合があります。メールごとに 1 つのグループのみを想定している場合は、ループをやめて 1 回だけ呼び出します$line = mysql_fetch_array()。ただし、複数の行が予想されるが、最初に遭遇した行にリダイレクトしたい場合は$group == 1、ロジックが機能します。ただし、その場合は、リダイレクトのみを実行し、他のアクションを実行しないため、その条件をクエリに追加するだけで済みます。

// Test the group in your query in the first place.
$query = "select * from usergroup where email='$fromaddr' AND group = 1";
$result1 = mysql_query($query) or die($query."<br/><br/>".mysql_error());

if (mysql_num_rows($result1) === 0) {
  // you didn't match a row, redirect to new.php
} 
else {
  // you had a match, redirect to facilitator.php
}
于 2012-10-10T18:38:34.223 に答える
1

簡単なもの:

変化する:

elseif (mysql_num_rows($result1) == 0){

に:

else {

の条件else ifはおそらく false です。そのため、そこに入らず、リダイレクトは発生しません。

于 2012-10-10T18:37:14.983 に答える