-1

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\">";  
}
}
}   
?>
Mahesh
  • 3
  • 5
  • Use `ob_*` functions to capture output instead of direct printing it. – Justinas Feb 04 '15 at 07:51
  • *headers already sent* take sure, that you don't have any output before you call session_regenerate_id() – donald123 Feb 04 '15 at 07:52
  • 1
    possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Hugo Delsing Feb 04 '15 at 07:56
  • @Randyka Yudhistria : Please check above code – Mahesh Feb 04 '15 at 08:00
  • for stop execution it's used at this time i am only checking new and old id same or different that's why i used this exit() function . – Mahesh Feb 04 '15 at 08:06
  • I give 99% that you include this file somewhere else and you outputted something before include. – Forien Feb 04 '15 at 08:10
  • @Forien : in header file only i used session_start() ; nothing else included. – Mahesh Feb 04 '15 at 08:12
  • Even if you have an `space`, `enter`, `tab` or anything outside `` is output. – Hugo Delsing Feb 04 '15 at 08:18
  • @Mahesh it works perfectly for me (copied your code and added `session_start()` between line 1 and 2. **YOU DID** some output. It can be anything, an `echo`, empty space between `?> .. – Forien Feb 04 '15 at 08:19

2 Answers2

2

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.

Community
  • 1
  • 1
Bart Haalstra
  • 1,062
  • 6
  • 11
0
  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;  
?>
Valeriu Ciuca
  • 2,084
  • 11
  • 14