简体   繁体   English

如何在PHP程序中调试“警告:session_regenerate_id():无法重新生成会话ID”?

[英]How to debug “Warning: session_regenerate_id(): Cannot regenerate session id” in a PHP program?

I am making a messaging system, where the message id is session_id and it is on a session_regenerate id. 我正在创建一个消息传递系统,其中消息ID为session_id,它位于session_regenerate ID上。

It's working fine but, when I changed my template, it keeps on giving errors like below : 它工作正常,但是,当我更改模板时,它继续显示如下错误:

" Warning: session_regenerate_id(): Cannot regenerate session id - headers already sent in C:\\xampp\\htdocs\\READS Website MAIN\\Admin\\admin-page\\admin\\message.php on line 24" “警告:session_regenerate_id():无法重新生成会话ID-第24行的C:\\ xampp \\ htdocs \\ READS网站MAIN \\ Admin \\ admin-page \\ admin \\ message.php中已发送的标头”

Below is my code: 下面是我的代码:

<?php
include('header.php');
include('config.php');


if(isset($_POST['submit'])){

$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$id = session_id();
$sender = $_SESSION['id'];

date_default_timezone_set("ASIA/MANILA");
$date = date('m-d-Y h:i a');




$sql = "INSERT INTO `messages`(`session_id`, `sender`, `recipient`,   
`subject`, `content`, `date`, `stat`) 
VALUES ('$id','$sender','$to','$subject','$message','$date','unread')";
mysql_query($sql) or die(mysql_error());
session_regenerate_id();
if($sql){

echo "<script>alert('Message Sent')</script>";

}else {
echo "<script>alert('error')</script>";
}

}

?>
<div>
<ul class="breadcrumb">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">User</a>
</li>
</ul>
</div>

<div class="box-content">


<form action='' method='POST'>

To: <select name='to' class="form-control" id="inputEmail3">

<?php $sql = "SELECT * FROM users ";
$result=mysql_query($sql); 
while ($row = mysql_fetch_array($result)){
echo "<option value='".$row['id']."'>".$row['Fname']."      
".$row['Lname']." (".$row['user_type'].")</option>";

}

?>
</select>


<div class='col-md-2 col-sm-2 col-xs-5' style='margin-left:150px;    
margin-top:-10px; width:500px;'>

<br>
Subject: <input type='textfield' name='subject' class="form-control"        
id="inputEmail3"><br><br>
Message: <textarea name='message'  class="form-control" id="inputEmail3"             
cols='30' rows='8'></textarea>
<div style = 'margin-left:420px; margin-top: 10px;'>
<input type='submit' name='submit'  value='Send'></div>
</td>




</body>
</html>
<?php include('footer.php');
?>

Make sure you are setting sessions or cookies before any content is sent. 发送任何内容之前,请确保设置会话或cookie。 Sessions and cookies are set in the header of a response, and since you can't change a header after the content (body) has been sent, you are faced with this warning. 会话和cookie设置在响应的标头中,由于发送内容(正文)后无法更改标头,因此您将面临此警告。

And after reading your code more thoroughly, I must remind you that this code should not be put into the real world, since you're not escaping any queries. 而且,在更彻底地阅读了您的代码之后,我必须提醒您,由于您没有逃避任何查询,因此不应将此代码放到现实世界中。 Plus that you should really switch to MySQLi or PDO, since the mysql_* functions are deprecated since a few PHP-versions. 此外,您还应该真正切换到MySQLi或PDO,因为自几个PHP版本以来,不推荐使用mysql_ *函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM