1

I know, header() must be called before any actual output is sent. But I am trying to use header() to include session in my xammp server. The code is:



    $member_id=$_REQUEST['member_id'];
    $member_id = stripslashes($member_id);
    $sql="SELECT * FROM member WHERE member_id='$member_id'";
    $result=mysql_query($sql);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
     $_SESSION['member_id']=$member_id;
     header("location:index.php?page=member_account&&member=success");
    }


this above code is working well in my Localhost. But when I upload this script on online server then server shows a warning that looks like this: Warning: Cannot modify header information - headers already sent by (output started at /home/emonuniq/public_html/index.php:16) in /home/emonuniq/public_html/inc/member/check.php on line 15. Then I try another way to redirect this LINK on javascript.



     //script tag is not showing  
     echo("location.href = 'index.php?page=member_account&&member=success'; 
     ");


this code redirect successfully on success.php. But in this page session doesn't work. How can I do?

Rachid O
  • 13,013
  • 15
  • 66
  • 92
  • The concept is to build your response body (the HTML or text or whatever is the request payload) and then output it at the end, when you're done processing, checking and swearing. There's different ways of doing this, some more appreciated than others, but in general you should be cautious of `echo`ing directly out to the buffer before you're ready to put the response together and send it. – Jared Farrish Mar 23 '13 at 11:01
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – NikiC Mar 23 '13 at 11:07
  • 1
    _"I know, header() must be called before any actual output is sent"_ - Then why are you sending output? – Eric Mar 23 '13 at 11:28
  • because, my xampp server work above code –  Mar 23 '13 at 13:12

2 Answers2

1

Add this to php.ini

output_buffering = On
Engineer
  • 5,911
  • 4
  • 31
  • 58
  • You can use the [`ob_start()`](http://php.net/manual/en/function.ob-start.php)` and [`ob_get_clean()`](http://php.net/manual/en/function.ob-get-clean.php) and other assorted output buffering functions. Why bake it into the INI? – Jared Farrish Mar 23 '13 at 11:02
  • I also use ob_start, ob_flush. but same problem –  Mar 23 '13 at 11:10
  • I tried but it solve only redirect problem. session not work in success.php –  Mar 23 '13 at 11:24
  • You should explain what output buffering does, otherwise people might think this is a magic hammer for errors that start with **Warning: Cannot modify header information** – N.B. Apr 29 '13 at 13:49
0

This may help you:

<script>
  window.location.href = "index.php?page=member_account&&member=success";
</script>
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
Prashant Shilimkar
  • 533
  • 1
  • 3
  • 10
  • sucees.php doesn't show success message. it perform many complex query. i need only member_id in success.php page –  Mar 23 '13 at 11:17
  • Have you write session_start at top of page.then and only then it will store it in session and you can access it any where. – Prashant Shilimkar Mar 23 '13 at 11:21