2013-08-04 68 views
0

我想下面的代碼header ('Location:'. $U);PHP頭不工作

遵循正道,但我發現了以下警告,如何解決它。

Warning: Cannot modify header information - headers already sent by (output started at H:\xampp\htdocs\redirected\index.php:8) in H:\xampp\htdocs\redirected\index.php on line 53 

我想:

http://mydomian/google redirect to http://google.com

http://mydomian/yahoo redirect to http://yahoo.com

http://mydomian/anyname redirect to http://Every site that I like

..............................

我htacess文件:

<Files ~ "^\.(htaccess|htpasswd)$"> 
deny from all 
</Files> 
ErrorDocument 404 /redirected/index.php 
order deny,allow 
在重定向文件夾

我有文件的index.phphtaccess.txt

htaccess.txt:

/google http://google.com 
/yahoo http://yahoo.com 

的index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>script for redirect !!!</title> 
</head> 
<body> 
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post"> 
    <input type="text" name="url" placeholder="e.g google.com" required/> 
    <input type="text" name="dir" placeholder="dir" value=<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?> required/> 
    <input type="submit" /> 
</form> 
<?php 
    $url = (isset($_POST['url'])) ? $_POST['url'] : ''; 
    $dir = (isset($_POST['dir'])) ? $_POST['dir'] : ''; 
    $filename = 'htaccess.txt'; 
    /* 
    * 
    * Function to make URLs into links 
    * 
    * @param string The url string 
    * 
    * @return string 
    * 
    */ 
    function makeLink($string){ 
     /*** make sure there is an http:// on all URLs ***/ 
     $string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string); 
     /*** make all URLs links ***/ 
     $string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string); 
     /*** make all emails hot links ***/ 
     $string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string); 

     return $string; 
    } 

    function redirect($url){ 
     if (headers_sent()) { 
      die('<script type="text/javascript">window.location.href="' . $url . '";</script>'); 
     }else { 
      header('Location: ' . $url); 
      die(); 
     }  
    } 
    $f = !empty($dir) ? $dir: htmlspecialchars($_SERVER['REQUEST_URI']); 

     $fp = fopen($filename , 'r+') or die("can't open file ! "); 
     while (!feof($fp)) { 
      $line = fgets($fp, 1024); 
      if ($f === strstr($line, ' ', true)) { 
       $u = strstr($line, 'http'); 
       //redirect($u); 
       //echo '<script type="text/javascript">window.location.href="' . $u . '";</script>'; 
       header('Location: ' . $u); 
       exit; 
      } 
     } 
     if ($url != '' && !empty($dir)) { 

     fseek($fp, 0, SEEK_END); // Go to the end of the file 
     fwrite($fp,"/$dir http://$url \n"); 
     fclose($fp); 
    } 

?> 
</body> 
</html> 

感謝

+1

什麼是$ u?您已將重定向函數中的參數定義爲$ url。 –

+1

你正在輸出這行(第8行,就像錯誤告訴你的一樣):'

」'。將你的'header()'移動到該行的上方。 –

+1

因爲在使用標題位置之前,您已經輸出了一些內容。 – TroyCheng

回答

3

此錯誤執行的頁面,或者這是你的頭前,包括網頁,呼應至少一些內容你試圖重定向之前。將文件頭向上移動到文件中,或確保文件包含的文件不會回顯任何內容。在你的情況下,你必須將它移動到文件的最頂端,因爲你的文件是以回顯html文件的開始處開始的。

1

在html標記之前做標題重定向,很多帖子在stackoverflow中應該指出。

<?php 
header ('Location:'. $U); 
?> 
<html>