php重定向到另一个页面
我正在使用header函数根据某些条件定位到另一个页面。我正在监视一个邮箱,代码根据发件人地址重定向到另一个页面。除一个标头外,所有标头都正常工作。如果发送者不属于任何现有组,我想将其重定向到new.php。但它不是重定向。我不知道为什么。请帮帮我php重定向到另一个页面,php,mysql,Php,Mysql,我正在使用header函数根据某些条件定位到另一个页面。我正在监视一个邮箱,代码根据发件人地址重定向到另一个页面。除一个标头外,所有标头都正常工作。如果发送者不属于任何现有组,我想将其重定向到new.php。但它不是重定向。我不知道为什么。请帮帮我 <?php session_start(); $server = '{server}INBOX'; $username = 'aaa@bbb.com'; $password = 'password'; require_once '../sw
<?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";
}
?>
简单一点:
更改:
elseif (mysql_num_rows($result1) == 0){
致:
else if
中的条件可能为false-因此您无法进入,因此不会发生重定向。简单一点:
更改:
elseif (mysql_num_rows($result1) == 0){
致:
else if
中的条件可能为false-因此您不会进入那里,因此重定向不会发生。看起来好像您正在while循环中嵌套该重定向。由于没有行,while条件mysql\u fetch\u array()
将立即返回FALSE
,并跳过整个块,包括您希望它遵循的重定向
将mysql\u num\u 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
循环,这取决于您希望获取的行数。如果您希望每封电子邮件只有一个组,那么放弃循环,只需调用一次$line=mysql\u fetch\u 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
}
//首先测试查询中的组。
$query=“从用户组中选择*,其中email='$fromaddr'和group=1”;
$result1=mysql\u query($query)或die($query。“
”。mysql\u error();
if(mysql_num_rows($result1)==0){
//您的行不匹配,请重定向到new.php
}
否则{
//如果您有匹配项,请重定向到facilitator.php
}
看起来好像您正在while循环中嵌套重定向。由于没有行,while条件mysql\u fetch\u array()
将立即返回FALSE
,并跳过整个块,包括您希望它遵循的重定向
将mysql\u num\u 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
循环,这取决于您希望获取的行数。如果您希望每封电子邮件只有一个组,那么放弃循环,只需调用一次$line=mysql\u fetch\u 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
}
//首先测试查询中的组。
$query=“从用户组中选择*,其中email='$fromaddr'和group=1”;
$result1=mysql\u query($query)或die($query。“
”。mysql\u error();
if(mysql_num_rows($result1)==0){
//您的行不匹配,请重定向到new.php
}
否则{
//如果您有匹配项,请重定向到facilitator.php
}
请编辑并修复缩进。计算{}
嵌套太麻烦了。请编辑并修复缩进。计算出{}
嵌套太麻烦了。