简体   繁体   English

如何重新生成会话ID?

[英]How To regenerate Session id?

I am trying to regenerate session id but not getting succeed, i used session_regenerate_id(). 我正在尝试重新生成会话ID,但没有成功,我使用了session_regenerate_id()。 but while we submitting form it's showing :--- 但是,当我们提交表单时,它显示:---
"Warning: session_regenerate_id(): Cannot regenerate session id - headers already sent in C:\\xampp\\htdocs\\yogesh_traders\\yadmin\\quo_pro_temp.php on line 4" “警告:session_regenerate_id():无法重新生成会话ID-第4行的C:\\ xampp \\ htdocs \\ yogesh_traders \\ yadmin \\ quo_pro_temp.php中已发送的标头”

please suggest how to solve this problem 请建议如何解决这个问题

<?php
$old_sessionid = session_id();
session_regenerate_id();
$new_sessionid = session_id();
echo "Old Session: $old_sessionid<br />";
echo "New Session: $new_sessionid<br />";
exit();
?>
<div align="center">
<img src="images/ajax-loader.gif" />
</div>
<?php
/* if($_REQUEST['clear']=='y' ){
$db->Delete('quotation_pro_temp',"session_id='".session_id()."'",1);
//session_regenerate_id();
} */

if(isset($_POST['senditem']))
{
if(count($_POST['senditem'])>0)
{

foreach($_POST['senditem'] as $setitem)
{
$a=$db->SelectSingle("product_details","id='".$setitem."'","");
$data["prod_id"] =$a["id"] ;
$data["pname"] = $a['pname'];
$data["brand"] = $a['brand'];
$data["qty"] = $_POST['qty'."-".$setitem];
$data["measure"] = $a['measure'];
$data["price"] =$_POST['price'."-".$setitem];
$data["mrp_field"] =$a['mrp_field'];
$data["discount"] = $_POST['disc'."-".$setitem]."-".$_POST['distype'."-".$setitem];
$data["total"] = $_POST['equals'."-".$setitem];
$data["session_id"] =$old_sessionid;
$db->Insert('quotation_pro_temp',$data);
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=inner_index.php?pagename=quotation_productwise_view&ins=y\">";  
}
}
}   
?>

You have to call this function before anything is send to the client (before any output). 在任何内容发送到客户端之前(任何输出之前),您必须调用此函数。

Make sure you don't have anything that outputs before this code (non php code or echo 's for example) or move the function up. 确保在此代码之前没有任何输出(例如非PHP代码或echo的输出)或向上移动功能。

You can also use output buffering (see ob_start ) to send all the output to a buffer instead of sending it directly to the client. 您还可以使用输出缓冲(请参见ob_start )将所有输出发送到缓冲区,而不是直接将其发送到客户端。

Is also possible to turn on output buffering by default, set output-buffering in php.ini to 'On' and restart your server. 也可以通过默认设置为打开输出缓冲输出缓冲在php.ini为“打开”,然后重新启动服务器。 See also this: How do i edit php.ini file in xampp server 另请参见: 如何在xampp服务器中编辑php.ini文件

After your edit: Make sure <?php is on the first line and there is no white space before it. 编辑后:确保<?php位于第一行,并且前面没有空格。 Your error says line 4, but the code is on line 3. 您的错误显示在第4行,但代码在第3行。

  1. You forgot to call session_start(). 您忘记了调用session_start()。
  2. Make sure you don't have any output before "session_start()", or you'll get "headers already sent error". 确保在“ session_start()”之前没有任何输出,否则将收到“标题已发送错误”。
  3. I tried your code in a separate php file and everything works fine: 我在一个单独的php文件中尝试了您的代码,一切正常:
<?php  
session_start();  

$old_sessionid = session_id();  
session_regenerate_id();  
$new_sessionid = session_id();  

echo "Old Session: $old_sessionid";  
echo "New Session: $new_sessionid";  
exit;  
?>

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

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