<table>
<tr>
<td width="30"></td>
<td><img src="Images/logo.jpg" width="100" height="100" />
<td><td><h2> Kunal Structures (India) Pvt. Ltd.</h2></td>
</tr>
</table>
<?php
if(!isset($_SESSION['user_id']))
{
header("location:http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php");
}
else
{
$now = time(); // checking the time now when home page starts
if($now > $_SESSION['expire'])
{
header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php");
echo "Session Expired !"." <a href='http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php'>"." <h1> ".
"Login again"." </h1>"."</a>";
session_destroy();
}
else
{
$qry = mysql_query("SELECT * FROM user_main WHERE user_id=".$_SESSION['user_id']."");
$loguser=mysql_fetch_assoc($qry);
?>
<table class="headerinfo" align="right">
<tr>
<td>You are <span style="color:#40B3BA;"><?php echo $loguser['user_name']; ?></span>,</td>
<td><a style="color:#f5f5f5; text-decoration:none;" href="logout.php"><b>Logout</b></a></td>
<td> </td>
</tr>
</table>
<br/>
</div>
<?php
if($loguser['user_type'] == "Administrator") //Custom Menubar for Administrator
{
?>
<div class="row-fluid">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#"></a>
<ul class="nav">
<li><a href="">Home</a></li>
<li><a href="">Documents</a></li>
</ul>
</div>
</div>
</div>
<?php
}
}
}
为什么它向我显示一个错误,例如标头已发送并且不重定向到给定路径。当session_['expire'] 条件变为真时,它应该移动到index.php
文件,但由于标头错误而没有发生。我该如何解决这个问题。是否有任何解决方案可以避免标头错误。
因为当您使用 php 标头函数时,顾名思义,它是一个标头,必须在任何内容之前发送。
每当您开始发送 html 时,您的服务器都会关闭标题部分并开始发送内容。然后,几行之后,你要求他在标题中添加一些东西。这是错误"标头已发送",他无法再添加它。
要解决此问题,请在编写任何纯 html 之前移动您的 php(即使是很小的空间也足以触发标头结束(。
另一个细节是,在标题之后回显某些内容是没有意义的,因为它永远不会显示。
您可以替换
header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php");
echo "Session Expired !"." <a href='http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php'>"." <h1> ".
"Login again"." </h1>"."</a>";
session_destroy();
跟
session_destroy();
header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php");
在
进行任何输出之前,您必须使用 php 标头函数。所以你将不得不重组一些代码
<?php
session_start();
if(!isset($_SESSION['user_id'])){
header("location:http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php");
}
else{
$now = time(); // checking the time now when home page starts
if($now > $_SESSION['expire']) {
session_destroy();
header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php");
}
}
?>
<html>
<!-- put html code here... ->
如果您收到"标头已发送"错误,则有三个可能的原因。
如果此错误不是页面上的第一条错误消息,则很可能是先前错误的"雪崩效应",您可以忽略它。相反,专注于修复它之前的错误。修复第一条错误消息后,"标头已发送"错误很可能会消失。
这通常可以通过快速自定义 php .ini更改来解决:
1(创建自定义php.ini文件(如果还没有(
- 有关如何创建自定义 php.ini 文件的信息,请单击此处。
2( 找到如下所示的行:
output_buffering = Off
3( 将output_buffering行更改为:
output_buffering = On
这应该是使网站重新上线所需的全部内容。
参考: http://kb.site5.com/php/how-to-repair-headers-already-sent-php-errors/
您也可以使用 在 php 文件顶部添加<?php @ob_start();?>
我同意文森特·杜普雷斯的观点但是在javaScript中还有替代方案
这是"达克"的更新代码
<?php
session_start();
if(!isset($_SESSION['user_id'])){
//header("location:http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php");?>
<script>
window.location="http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php";
</script>
<?php
}
else{
$now = time(); // checking the time now when home page starts
if($now > $_SESSION['expire']) {
//header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php");
?>
<script>
window.location="http://127.0.0.1/KSIPL1.10/KSIPL/index.php";
</script>
<?php
session_destroy();
}
}
?>
它会起作用,请运行:)