0

I am using the header function to locate to another page based on certain conditions. I am monitoring a mailbox and the code redirects to another page based on the sender address. All headers are working except one. If the sender does not belongs to any existing group, I wanted to redirect it to new.php. But it is not redirecting. I am unable to figure out why. Please help me.

<?php 
session_start();

$server = '{server}INBOX';
$username = '[email protected]';
$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";
}


?>
1
  • 1
    Please edit and fix your indentation. It's too troublesome to figure out your {} nesting. Commented Oct 10, 2012 at 18:36

2 Answers 2

3

It appears as though you are nesting that redirection inside the while loop. Since there are no rows, the while condition mysql_fetch_array() will immediately return FALSE and skip the whole block, including the redirection you intended it to follow.

Move the test for mysql_num_rows() outside the while loop.

// 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");
   }
}

You actually may not need a while loop at all, depending on how many rows you are expecting to fetch. If you only expect one group per email, then forego the loop and just call $line = mysql_fetch_array() once. However, if you are expecting multiple rows but want to redirect on the first one encountered where $group == 1, then your logic works. In that case however, since you are only doing the redirection and no other action, you might as well just put that condition in your query:

// 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
}
Sign up to request clarification or add additional context in comments.

Comments

1

Easy one:

change:

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

to:

else {

The condition in the else if is probably false - so you don't get in there and thus the redirection doesn't occur.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.