Php error : Cannot modify header information - headers already sent

Discussion in 'Programming' started by joe_mat, Jan 16, 2007.

  1. #1
    hello

    My PHP code is :

    <?php
    $myname = ' its come from input text
    $filename = "/home/public_html/namelink.txt";
    $fp = fopen ($filename, "r");
    while (!feof($fp)) {
    $line = fgets($fp, 1024);
    list ($field1, $field2) = split ('#', $line);
    if ($field1 = $myname)
    {
    header("Location: http://www.mydomain.com");
    exit;
    }
    $fp++;
    }
    fclose($fp);
    ?>

    i am getting an error :
    Cannot modify header information - headers already sent
    please suggest me how to solve this error

    Thanks
     
    joe_mat, Jan 16, 2007 IP
  2. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #2
    Have you used echo before header("Location.............|) ? If yes it will give you an error if no, then paste complete code here or in pm so that I can help you.

    Thanks
     
    technoguy, Jan 16, 2007 IP
  3. ThomasNederman

    ThomasNederman Peon

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am not sure if it can be as simpel as on this line change

    $myname = ' its come from input text
    to
    $myname = ' its come from input text ';


    you could also try changing
    if ($field1 = $myname)
    to
    if ($field1 == $myname)

    you can also remove this "$fp++; "

    Hope some of the above help
     
    ThomasNederman, Jan 16, 2007 IP
  4. joe_mat

    joe_mat Peon

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    its a complete code i hav not change anything

    i am having a if statment in while loop if i get the match i redirect it to some of my domain
     
    joe_mat, Jan 16, 2007 IP
  5. West

    West Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well first off the $myname = ' its come from input text is definitely wrong. It's not closed at all. That's probably not the root of the error, but does need fixed. Secondly make sure there is NO whitespace before or after <? ?>
     
    West, Jan 16, 2007 IP
  6. joe_mat

    joe_mat Peon

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Its close, actually its just to show u that the value in variable Myname comes frm text box
     
    joe_mat, Jan 16, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    
    $line = split("\n", file_get_contents("namelink.txt"));
    foreach( $line as $data )
    {
    	$segs = split( "#", trim($data) );
    	if($segs[0] == $myname )
    	{
    		header("Location: http://www.mydomain.com");
    		exit;	
    	}	
    }
    ?>
    
    PHP:
    ???

    If you could post nameslist.txt that would be good .....
     
    krakjoe, Jan 16, 2007 IP
  8. joe_mat

    joe_mat Peon

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    its still giving me a same error
     
    joe_mat, Jan 16, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    paste the text file you're trying to parse then .....
     
    krakjoe, Jan 17, 2007 IP
  10. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I have had same problem . In my case it was due to some blank lines at the end of the file . I mean after ?>. It was for I was using notepad to code. Notepad keeps the blank line may be. Then I have copied the code to wordpad and tried , my problem solved.

    You could try this,

    thanks
     
    kashem, Jan 17, 2007 IP