警告:不能修改标题信息 - 已经发送的标题(PHP) [英] Warning: Cannot modify header information - headers already sent (PHP)

查看:80
本文介绍了警告:不能修改标题信息 - 已经发送的标题(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有点类似于以下帖子。



PHP错误:无法修改标题信息 - 标题已发送



但在我的情况一旦我确定登录表单中没有验证错误并且用户的登录信息与数据库的登录信息相匹配,我就选择开始会话。这里是以下代码:

登录页面(在任何html之前)

 会话名称( '用户名'); 
session_name('ip');
session_name('start');
session_start();

Login.php snippet(在html中)

 } else {
$ user = $ _POST ['username'];
$ userpass = md5($ _ POST ['password']);
$ login_results = statement(从'$ admin`选择用户名,密码,其中username ='$ user'和password ='$ userpass');

if(mysql_num_rows($ login_results)!= 1){
$ errmsg =< span id ='error'>登录失败:用户名或密码不在文件中< / span> ;;
} else {

$ _SESSION ['username'] =$ user;
$ _SESSION ['ip'] = $ _SERVER ['REMOTE_ADDR'];
header(Location:index.php);
}
}
}

}

如果你看上面的代码的else块,我正在验证登录,如果它的好,我想分配会话变量并进入我的索引页面。其中有这样的代码:

  //会话超时脚本 - 用于确定用户拥有的时间闲置。如果用户空闲了更长的时间然后会话时间,则将用户注销。 
//超时到超时脚本,检查用户名和IP地址是否有效,以及是否将用户重定向到登录页面。
session_cache_expire(20);
session_start();

$ inactive = 1200;

if(isset($ _ SESSION ['start'])){
$ session_life = time() - $ _SESSION ['start'];
if($ session_life> $ inactive){
header(Location:logout.php);
}
}

$ _SESSION ['start'] = time();

$ newip = $ _SERVER ['REMOTE_ADDR'];
if(!isset($ _ SESSION ['username'])|| empty($ _ SESSION ['username'])|| $ newip!= $ _SESSION ['ip']){
header 'Location:login.php');
}

现在阅读前面作者提出的问题, )应该是在发送重定向的代码中执行的第一件事,在我的例子中是login.php。这样做可以让我登录,但是当我注销时,我会销毁所有会话,并使用header()将我发回登录页面。这又会使登录页面重定向回到索引页面,因为它的第一行代码被读取。有没有办法避免这种情况?所以我不需要重复一些我已经在login.php顶部的代码逻辑?

Andre


其他输出必须在 之前调用标头由http本身,没有办法绕过它。但是,您可以在 session_start()之后调用头部。

所以您可以启动会话,检查登录数据 $ _ POST 并且启动html输出。 为什么使用三个 session_name code>连续?


My issue is somewhat similar to the following post..

PHP error: Cannot modify header information – headers already sent

But in my case I chose to start the session once I determine there is no validation errors from the login form and the user's login info matches that of the database. Here is the following code:

Login page (before any html)

session_name('username');
session_name('ip');
session_name('start');
session_start();    

Login.php snippet (in the body of html)

         } else {
            $user = $_POST['username']; 
            $userpass = md5($_POST['password']); 
            $login_results = statement("select username, password from `$admin` where username='$user' and password='$userpass'");

            if (mysql_num_rows($login_results)!= 1) { 
                $errmsg = "<span id='error'>Login failed: Username or password not on file</span>";
            }else {

                $_SESSION['username'] = "$user"; 
                $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
                header("Location: index.php"); 
            }
        }
    }

}

if you look at the else block of the code above i'm verifying the login and if its good I want to assign the sessions variables and go to my index page. Which has this code at the very beginning:

 //Session Timeout Script -- used to determine the amount of time the user has been idle.  If it the user has been idle for longer then the session time, log the user out.
 //Secondary to the Timeout Script, the username and ip address is checked for validility and if either fails redirect the user to the login page. 
 session_cache_expire( 20 );
 session_start(); 

 $inactive = 1200;     

 if(isset($_SESSION['start']) ) {
      $session_life = time() - $_SESSION['start'];
  if($session_life > $inactive){
        header("Location: logout.php");
   }
}

  $_SESSION['start'] = time();

    $newip = $_SERVER['REMOTE_ADDR']; 
   if (!isset($_SESSION['username']) ||  empty($_SESSION['username']) || $newip!=    $_SESSION['ip']) { 
 header('Location: login.php'); 
} 

Now reading through the question from that previous author, it was mentioned that header() should be the first thing to execute in the code thats sending the redirect, which in my case is login.php. And doing that allows me to login, but when I log out i'm destroying all my sessions and and using the header() to send me back to the login page. Which will in turn make the login page redirect back to the index page because its the first line of code read. Is there a way to avoid this? so I wouldn't need to repeat some of my code logic I already have in place at the top of login.php?

Andre

解决方案

Yes, header must be called before any other output, it's needed by http itself, no way around it. However, you can call header after session_start().

So you can start session, check the login data from $_POST and than start html output.

Btw, why use three session_name in succession?

这篇关于警告:不能修改标题信息 - 已经发送的标题(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆