dqoys62082 2016-06-22 09:39
浏览 43
已采纳

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

  • 写回答

2条回答 默认 最新

  • donglun2024 2016-06-23 09:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示