我的PHP代码与以下代码一起位于HTML文件中。我不知道PHP header()为什么不重定向。 FireFox说“页面没有正确重定向”
更新我添加了我正在尝试重定向的整个HTML代码。
<?php
function http_protocol() {
return (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://';
}
function http_host() {
return $_SERVER['HTTP_HOST'];
}
function http_uri() {
return parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
}
function http_refactored_query_strings() {
$queries = explode('&', $_SERVER['QUERY_STRING']);
$refactoredQueries = [];
foreach( $queries as $query ) {
$refactoredQueries[] = filter_var(explode('=', $query)[1], FILTER_SANITIZE_STRING);
}
$queries = implode('/', $refactoredQueries);
return $queries ?: '';
}
function http_refactored_url() {
return http_protocol() . http_host() . http_uri() . http_refactored_query_strings();
}
header ('Location ' . http_refactored_url());exit;
?>
<?php
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public $pages_articles;
public function __construct($page=1, $per_page=20, $total_count=0) {
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
$this->pages_articles=array(
'<div class="article-loop"><img src="http://i.imgur.com/CmU3tnl.jpg"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/TDdxS9H.png"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/39rpmwB.jpg"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/1lBZQ1B.png"></div>',
'<div class="article-loop"><img src="https://i.imgur.com/Y5Ld4Qfh.jpg"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/8HumESY.jpg"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/CqCZBvk.png"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/q3I72Ul.png"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/YrzP9A3.jpg"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/xWmaeb6.png"></div>',
'<div class="article-loop"><img src="https://i.imgur.com/GpGBVZW.png"></div>',
'<div class="article-loop"><img src="http://i.imgur.com/wQVPRVp.png"></div>');
$this->total_count = sizeof($this->pages_articles);
}
public function offset() {
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages() {
//$this->total_count=sizeof($this->pages_articles);
return ceil($this->total_count/$this->per_page);
}
public function previous_page() {
return $this->current_page - 1;
}
public function next_page() {
return $this->current_page + 1;
}
public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 3;
//$total_count=8;
$pagination = new Pagination($page, $per_page, $total_count);
?>
<html>
<body>
<div>
<?php
$i = $pagination->offset() ;
$limit = $pagination->per_page;
while($i<$pagination->total_count && $limit>0) {
echo $pagination->pages_articles[$i]."<br>";
$i++;
$limit--;
}
?>
</div>
<ul>
<?php
if($pagination->has_previous_page()) {
echo '<li style="display:inline"><a href="?page='.$pagination->previous_page().'">«</a></li>';
} else {
echo '<li style="display:inline" class="disabled"><a href="#">«</a></li>';
}
for($i=1; $i<=$pagination->total_pages(); $i++) {
echo '<a href="?page='.$i.'"><li style="display:inline; margin-left:5px; margin-right:5px">'.$i.'</li></a>';
}
if($pagination->has_next_page()) {
echo '<li style="display:inline"><a href="?page='.$pagination->next_page().'">»</a></li>';
} else {
echo '<li style="display:inline" class="disabled"><a href="#">»</a></li>';
}
?>
</ul>
</body>
</html>
答案 0 :(得分:0)
我假设您的网址中没有$_GET
个参数。在这种情况下,当您遍历$refactoredQueries[] = filter_var(explode('=', $query)[1]
时,您将抛出异常:
未定义的偏移量:1
如果您拥有 $_GET
参数,该页面将正确重定向。
要解决此问题,您需要在尝试访问之前检查查询是否存在:
if ($query) {
$refactoredQueries[] = filter_var(explode('=', $query)[1], FILTER_SANITIZE_STRING);
}
重定向没有正确发生,因为当没有查询字符串时它会陷入无限循环。要更正此问题,只需在尝试重定向时检查查询字符串是否存在。如果没有任何查询字符串,您最终会在当前所在的页面上,因此您只需exit
:
function http_refactored_url() {
if (http_refactored_query_strings()) {
return http_protocol() . http_host() . http_uri() . http_refactored_query_strings();
}
else {
exit;
}
}
希望这有帮助! :)