Please help me in Warning: Cannot modify header information

Discussion in 'PHP' started by hallianonline, Apr 24, 2014.

  1. #1
    Helllo I am getting Warning: Cannot modify header information - headers already sent by error on header("location: sucessful_registration.php"); line
    please help me how can i fix it

    function NewUser()
    {
        $full_name = $_POST['full_name'];
        $user_name = $_POST['user_name'];
        $password = $_POST['password'];   
        $email_address = $_POST['email_address'];
        $phone = $_POST['phone'];
        $address = $_POST['address'];
        $city = $_POST['city'];   
        $zip = $_POST['zip'];
        $state = $_POST['state'];
        $country = $_POST['default_country'];
        $member_id = $_POST['member_id'];
        $card_csv = $_POST['card_csv'];   
        $activation_new = "0";
        $query = "INSERT INTO user_registration (full_name,username,password,email,phone,address,city,zip,state_province,country,member_id,member_csv,activation) VALUES ('$full_name','$user_name','$password','$email_address','$phone','$address','$city','$zip','$state','$country','$member_id','$card_csv','$activation_new')";
        $data = mysql_query ($query)or die(mysql_error());
        if($data)
        {
                    header("location: sucessful_registration.php");
        }
    }
    newuser();
    
        } 
    PHP:
     
    hallianonline, Apr 24, 2014 IP
  2. humanwebs

    humanwebs Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #2
    try adding ob_start();
    in the beggining of the page
    ie
    <?php
    ob_start();
    //your code goes here

    ?>
     
    humanwebs, Apr 25, 2014 IP
  3. hallianonline

    hallianonline Active Member

    Messages:
    104
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    still not working :(
     
    hallianonline, Apr 25, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    You can only call header BEFORE anything is output -- either outside php tags or using echo/print. The bug isn't in that function so much as it's likely before wherever it is you are calling that function. Find out where you are calling it, and see what's outputting markup (or even blank lines) before it. Part of why I still say there should only be one <?php per php file and it should be the FIRST thing in it.... actually, not entirely accurate; I say that the PHP tags need to be removed from PHP entirely -- no more <?php ?> I say!

    Though honestly, doing a stupid handshake redirect instead of just *SHOCK* showing the successfuly result page means your methodology is flawed. I'll NEVER understand why people do that crap in their systems as it's just a waste of the user and server's time. Why are you redirecting instead of just doing an include?!?

    Of course, the endless pointless variables for nothing, blind string addition with no sanitation, and soon to be deprecated mysql_ functions we've been told for eight to nine years to stop using probably aren't helping matters in a "security? What's that?" kind of way.
     
    deathshadow, Apr 25, 2014 IP
  5. swapinbiz

    swapinbiz Member

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Please use javascript for redirecting to successful page.. the best and easy way.

    OR

    There are some problems with your header() calls, one of which might be causing problems

    • You should put an exit() after each of the header("Location: calls otherwise code execution will continue
    • You should have a space after the : so it reads "Location: url"
    • It's not valid to use a relative URL in a Location header, you should form an absolute URL like http://www.mysite.com/some/path.php
     
    swapinbiz, Apr 25, 2014 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Please don't, it's a terrible way.
     
    nico_swd, Apr 25, 2014 IP
    deathshadow likes this.
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    @nico_swd, really kind of wish I could mash the like button a few thousand more times...

    Because of course, dumbass scripttardery is going to be such an improvement over a header redirect that ALSO serves no legitimate purpose.
     
    deathshadow, Apr 25, 2014 IP
    nico_swd likes this.
  8. xtmx

    xtmx Active Member

    Messages:
    359
    Likes Received:
    12
    Best Answers:
    4
    Trophy Points:
    88
    #8
    What if the PHP script posts on a forum or makes a purchase? Do you really want the user refreshing it?
     
    xtmx, Apr 26, 2014 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Of course not, that's why you use a unique (and expiring) token for each transaction where repetition is bad... something you should probably have there ANYWAYS to make session hijacks harder.
     
    deathshadow, Apr 26, 2014 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    The fact that he used the words "please" and "best way" felt like a kick in the nut sack. It still hurts, to be honest.
     
    nico_swd, Apr 28, 2014 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #11
    For me it was more like Larry the Cable guy's joke about something so hideous it makes your wiener shrink back so far it sticks out the other end.

    I tell you what that's funny right, there... don't care who you are that's funny.
     
    deathshadow, Apr 28, 2014 IP
    nico_swd likes this.
  12. xtmx

    xtmx Active Member

    Messages:
    359
    Likes Received:
    12
    Best Answers:
    4
    Trophy Points:
    88
    #12
    I have to admit. That made me laugh.
     
    xtmx, Apr 28, 2014 IP
  13. Chris Paris-Haines

    Chris Paris-Haines Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    Hello, I was looking at the original code at I think in there the 3rd curly bracket was not opened before it was closed.

    I am new here btw but was not sure where to introduce myself.
     
    Chris Paris-Haines, Apr 28, 2014 IP
  14. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #14
    While the third end-bracket doesn't have any starting bracket to match, it has nothing to do with the OP's original question.
     
    PoPSiCLe, Apr 28, 2014 IP
  15. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #15
    I just assumed that we're not seeing the whole picture as it's a snippet, instead of a complete file... though some sane and rational formatting would have gone a long ways to help with the code clarity. (but then I'm so old school I call K&R formatting "new kid BS")
     
    deathshadow, Apr 29, 2014 IP