简体   繁体   中英

How To regenerate Session id?

I am trying to regenerate session id but not getting succeed, i used 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"

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.

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.

Is also possible to turn on output buffering by default, set output-buffering in php.ini to 'On' and restart your server. See also this: How do i edit php.ini file in xampp server

After your edit: Make sure <?php is on the first line and there is no white space before it. Your error says line 4, but the code is on line 3.

  1. You forgot to call session_start().
  2. Make sure you don't have any output before "session_start()", or you'll get "headers already sent error".
  3. I tried your code in a separate php file and everything works fine:
<?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;  
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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