Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:11:28+00:00 2026-06-12T19:11:28+00:00

I am using the header function to locate to another page based on certain

  • 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 = '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";
}


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:11:29+00:00Added an answer on June 12, 2026 at 7:11 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to redirect users to a different page using the Header function
I've done tons of redirects using PHP's header function. This one has stumped me.
I have the following code for a page where i am using a header
After from processing i am sending the user on the previous page using: header('Location:
I know how to redirect a page using the header thing. The problem is
I am trying to redirect a page using HTTP 410 status using php header
I'm using the header() function to turn a file into XML standard. The problem
I have tried using header() to programmatically take the user to a different page
I am using a header for a Listview. ListView header has three columns. Say
I want to encode DNS protocol header using C and create a UDP datagram.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.