Jump to content

Cannot modify header information - headers already sent by


Nefferson

Recommended Posts

hello there! i am new to php ! i was trying to create a login form. in line 202

 

<form class="login-form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

 

so i can make the verification process on the same page and send the errors to the users when there are some

.

if there is no error (i.e all data correct) process data and make connection to database(mysql), put the data(username, password,

 

email) in table, then at least redirect user to the home page with (header('Location: homepage.php'). but i got an error while loading

 

page. I know why the problem. I just want someone to give me an advice on how to solve it. please help me !!!!

Link to comment
Share on other sites

if you read the error message, it tells you where the OUTPUT is occurring at, that is preventing the header() statement from working.

 

the way to fix this is to find and prevent the output from occurring before the header() statement. this may involve rearranging your code so that the logic containing the header() statement comes near the top of your code, before you produce and output any html.

Link to comment
Share on other sites

i found another solution. what i wanted to do is to redirect automaticly   my user to the homepage after registering his data input in the data base after registering on my webpage. so i created a variable $redirect = ''thanks for registering, please click here to get access to all our services" and then i put a link <a href="homapage.php">Homepage</a>. thus he can keep going by himself on the next pages.

Link to comment
Share on other sites

That's silly. When your code doesn't do what it should, the solution is to fix it, not make your users click on links.

 

You'll probably encounter the same problem many more times, so why don't you take the chance to understand it and avoid it in the future?

if you can give me any idea, i would be really grateful. the fact is that i made somee researches, and based on waht i found, it was like there nothing i could do to fix it. the only other solution i saw, was to send all the data to another page <action="verify.php"> and then retrieve the data with $_POST[] and make the connection to the database, after storing the data in tables, redirect user to homage with (header) but my problem was, what if the user didnt enter all the data from the form. i would like to send an error message to notify him there is a data missing but i am not good enough to do this, so i had to find a solution. but if you have any idea, please tell me.

Link to comment
Share on other sites

Post your code.

 

The solution is to first do all PHP processing, and then generate the HTML markup:

<?php

// PHP code goes here

?>
<!DOCTYPE HTML>

<!-- HTML markup goes here -->

This way there will never be any output before a header() call. It also leads to much cleaner and more maintainable code, because the business logic (PHP) is cleanly separated from the representational part (HTML).

Link to comment
Share on other sites

Post your code.

 

The solution is to first do all PHP processing, and then generate the HTML markup:

<?php

// PHP code goes here

?>
<!DOCTYPE HTML>

<!-- HTML markup goes here -->

This way there will never be any output before a header() call. It also leads to much cleaner and more maintainable code, because the business logic (PHP) is cleanly separated from the representational part (HTML).

I tried it and it worked !!! thank you so much !!! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.