PHP头重定向奇怪的问题

PHP头重定向奇怪的问题

问题描述:

I'm using PHP header re-directs to push my users through my PHP-HTML site, however I'm getting an unusual issue when attempting to do this.

The content displayed on-screen is not blank, but instead is the re-directed URL, however the URL bar still seems to show the old URL prior to re-directing.

There are no echo ""; statements, print_r(); statements or var_dump(); statements that could be causing this, nor is there any wild HTML or white-spaces (I've checked thrice, all HTML is encased in <<<HTML [htmlhere] HTML; and white-spaces have been eliminated using "Find & Replace")

I've also attempted to use exit(); after my header code to no assistance. Could anyone let me know if I'm being silly here?

Code

<?PHP
if(!isset($_SESSION))
{
  session_start();
}
function getPage($link)
{
  $contents="";
  $cSession=curl_init();
  curl_setopt($cSession,CURLOPT_URL,$link);
  curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
  curl_setopt($cSession,CURLOPT_HEADER,true);
  curl_setopt($cSession,CURLOPT_FOLLOWLOCATION,false);
  $contents.=curl_exec($cSession);

  if(preg_match('#HTTP/1.1(.*)#',$contents,$t))
  {
    $code=trim($t[1]);
  }
  else
  {
    $code="";
  }

  if($code=="301 Moved Permanently")
  {
    if(preg_match('#Location:(.*)#',$contents,$r))
    {
      $l=trim($r[1]);
    }

    $contents="";
    $handler=fopen($l,"r");

    while(!feof($handler))
    {
      $contents.=fread($handler,1024);
    }

    fclose($handler);
  }
  else if($code=="200 OK")
  {
    $contents=curl_exec($cSession);
  }
  else
  {
    $contents="";
  }
  curl_close($cSession);

return $contents;
}

function checkPage($content)
{
  $links=array();
  $textLen=strlen($content);
  if($textLen>10)
  {
    $startPos=0;
    $valid=true;

    while($valid)
    {
      $spos=strpos($content,'<a',$startPos);
      if($spos<$startPos)
      {
        $valid=false;
      }
      else if($spos===FALSE)
      {
        $valid=false;
      }
      else
      {
        $spos=strpos($content,'href',$spos);
        $spos=strpos($content,'"',$spos)+1;
        $epos=strpos($content,'"',$spos);
        $startPos=$epos;
        $link=substr($content,$spos,$epos-$spos);
        if(strpos($link,"http://")!==false)
        {
          $link=rtrim($link,'/');
          $links[]=$link;
        }
        else if(strpos($link,"https://")!==false)
        {
          $link=rtrim($link,'/');
          $links[]=$link;
        }
      }
    }
  }
  return $links;
}

function checkArray($array,$url)
{
  $new_array=array();
  $update=array();

  foreach($array as $child)
  {
    if(strpos($child,$url)===FALSE)
    {
      $child="";
    }
    else
    {
      if($child===$url)
      {
        $child="";
      }
      $new_array[]=$child;
    }
  }

  $new_array=array_unique($new_array);
  $new_array=array_filter($new_array);

  return $new_array;
}

$v_url_to_check="";
$v_api="";

if(isset($_POST["URL"])&&isset($_POST["Feedback"]))
{
  $v_url_to_check=filter_var($_POST["URL"],FILTER_SANITIZE_URL);
  $v_api=filter_var($_POST["Feedback"],FILTER_SANITIZE_STRING);
}
else if(isset($_POST["URL"])&&!isset($_POST["Feedback"]))
{
  $v_url_to_check=filter_var($_POST["URL"],FILTER_SANITIZE_URL);
  $v_api="";
}
else
{
  $_SESSION["Message"]="BadData";
  header("Location:".$_SESSION['loc'],true,301);
  exit;
}

if(strpos($v_url_to_check,".")===FALSE)//
{   
  $_SESSION["Message"]="Failed";
  header("Location:".$_SESSION['loc'],true,301);
  exit;
}
else
{
  $a_content=checkPage(getPage($v_url_to_check));
  $new_array=checkArray($a_content,$v_url_to_check);

  $count=0;
  foreach($new_array as $child)
  {
    $count++;
  }
  $i=0;
  foreach($new_array as $childs)
  {
    $update["Child$i"]=$childs;
    $i++;
  }

  if($count!=0)
  {
    $_SESSION["Message"]="Success";
    $_SESSION["Links"]=$count;
    $_SESSION["ReturnedArray"]=$update;
    $_SESSION["RequestedURL"]=$v_url_to_check;
    $_SESSION["FeedbackValue"]=$v_api;

    header('Location:'.$_SESSION['loc'],true,301);
    exit;
  }

  else
  {
    $_SESSION["Message"]="Failed";

    header("Location:".$_SESSION['loc'],true,301);
    exit;
  }
}
?>

$_SESSION['loc'] is being set in the main code (index.php) but, as that has no issue with re-directing, I'm not including it.

EDIT: header("Location: ".$_SESSION['loc'],true,301); (using a space after the colon) did not make a difference in the result

Okay. Wow, so I was totally wrong! It wasn't the re-directing that was the issue, it was the main index page (which re-directed to the form PHP code perfectly well)

In the main index page I had the following code that, when taken out resolved the problem:

CODE

<META name="viewport" content="width=device-width, initial-scale=1">
<LINK rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<SCRIPT src="//code.jquery.com/jquery-1.11.3.min.js"></SCRIPT>
<SCRIPT src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></SCRIPT>

It seems including jquery was the thing that killed it.

Not sure exactly how it did this, but removing this takes away some functionality. I need it for a HTML form input range slider that changes values on the fly. If anyone wants to have a look and tell me in the comments how jquery could cause PHP header location information to crash, I'd be much appreciated.

Try using space after location like this may be error is due to space,

header("Location: ".$_SESSION['loc'],true,301);

Also Visit this Link for header with permanent or temporary redirect. Manual