PHP Forward to next Page - php

I have a form that, on submit, is going to a PHP file.
Here the form is processed and sent to a MySQL database for storage.
After this is done I want the page to, automatically, send me to the next page wich contains another form that has to be filled in (this continues a few times).
I know that you can use
header('Location: HERE YOURE LINK ');
for sending you to the next page, but it doesn't work.
In my PHP file I have some basic stuff:
Request form, process data, INSERT INTO database....
When I put the "header....."-code after these PHP commands it returns an error, and the same thing happens when I place it in front of the PHP commands.
It keeps returning the error:
Cannot modify header information - headers already sent by .... on line 17
I hope someone can help me, and that I have given you all the information you need to help me. I've been searching all day but still haven't found the solution.
Thanks in advance!
Milaan

You need to make sure the header(...); line is called before any other text or headers are sent.

The "headers already sent" error is usually caused by having white space before or after the opening and closing PHP tags (<?php . . . ?>).
Use of ob_start() also helps.
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

A detailed description of your very common problem may be found at the following link:
http://www.phpbuilder.com/board/showthread.php?t=10262775

Putting the html tag before the header() blocks it. Please check.

Related

HTML Login form with PHP Authentication [duplicate]

Does somebody know why my header() does not redirect?
The last part of my script is:
header("location: test.php");
die('died');
It writes:
died.
:(((
It should has to redirect before it dies, but it does not.
Do you have you any idea?
It's probably that you're calling header() after you are outputting some text/HTML to the browser, which is a no-no. Your header() call will be ignored if even so much as a single space of output has been sent before the call.
In other words, your header() code needs to be at the start of your script, before you display anything to the user. If that still isn't working, make sure you don't have any spaces or other whitespace by mistake outside of your php tags.
Maybe there is some invisible output before header, which is preventing setting header, and informative warnings are suppressed.
Additionally Location-Headers should contain an absolute URL:
// Show Errors
error_reporting(E_ALL);
ini_set('display_errors','On');
// Redirect
header('Location: http://example.com/path/test.php');
die('redirect');
You should use a fully-qualified address for your location header, and not output any content:
header('Location: http://example.com/test.php');
die();
Additionally make sure that no other content was sent before setting the header, otherwise it wont be sent as the headers will have already been sent to the browser.
location: should be Location:.
Moreover...
Scroll to the top of this page.
Click on your account name.
Click on a random question
Check mark a random answer
Come back to find more serieus answers
Good luck.
Just resolve it by removing ALL things before the php tag.
Remove the SPACE before the php tag
Select all before the tag and then push the erase button.
I was stuck on this error, too, and tried to show errors with error_reporting(E_ALL),
but this didn't worked for me. Info: I was using a hosting service on Strato.
The solution to all this was answered by Floem, thanks for this great setup.
If you ever don't get errors in case you put error_reporting(E_ALL) once in,
-> You must additionally add ini_set('display_errors', 'On'), to force PHP to show all your Errors. This solved my Error issue on
Then it's possible to read out, why PHP wouldn't make your redirect.
One tip on my side: I would recommend you to build a little redirect Class, as shown in this Video: Redirect Class for easy use, if you're going to use this redirect more than usual.
If the file containing the 'header()' instruction is required directly/indirectly in a HTML file, then the instruction {include 'someFile'} should be the first line of that HTML file. Place this instruction on the top of your HTML file, as shown in this image.
Add
ob_start();
before
header("location: test.php");

How do I get Header in Php to work correctly?

I am trying to fix why my Header command won't redirect back to my main page. It instead just stays on the Php display page.
Here is the php side after I submit my form.
Php Script http://img35.imageshack.us/img35/6602/a9j3.png
Here is the page that I get when my php is put into effect.
Php display http://img40.imageshack.us/img40/6649/1qk3.png
Here is my page it's suppose to redirect to...
Html Main Page http://img801.imageshack.us/img801/8004/tulo.png
As you can see, I have no spacers in my header command for php. I've looked up multiple issues but never found anything that works.
See the line that says:
echo 'Full connection<br/>';
That line will ensure that no headers will be able to be sent after it. You can't output anything to the browser before a header call, including whitespace, HTML or this line.
I'm surprised you aren't getting an error when this happens, it's probably because your error reporting levels are turned down. It's often a good idea to have error reporting turned up high on your local machine when testing so you'll see errors like this and can fix them straight away:
error_reporting(E_ALL);
I suspect the echo early on is causing the problem. Outputting any content renders further header calls redundant.
Try commenting out that line and run it again.
When using header-Location for redirection, be sure you don't let anything echo-ing. the HTML content needs to be empty
URL in header("Location:http://...URL..."); should be full absolute path (best practices)

How do I carry over a php session from one page to another via Javascript?

I have a form (form.php) that uses the following JS code in the header:
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$.post('test.php', function(data) {
$('#test').html(data);
});
});
});
</script>
A have a link in form.php that once clicked I want a drop down to appear (without refreshing the page) e.g. "Display drop down". The code in the drop down is managed in test.php.
Test.php pulls data in from another service. In order to do get this data I use data that is kept in the session e.g. $_SESSION['data_that_is_sent_to_another_service'].
I start the session in form.php but in order for the test.php to get the information from the other service I need to start a session at the start of test.php.
The code works, but I then get a warning saying:
Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /..my directory.../test.php:1) in /...my directory.../hrdeals.php on line 4
How can I get this warning not to appear (without turning off warnings in PHP)?
Any help much appreciated.
Gregor
PS - the JS might not be correct, but it's more the SESSION issue that I can solve
session_start() must be called before outputing anything to the browser. Check test.php and make sure there is no output.
Javascript and PHP Sessions have nothing to do with one another. Javascript lives in its little bubble (client side rendering/processing). PHP lives in its bubble (server side processing/output).
The error you get is (briefly) - headers already sent.
This refers to http headers (not the head tag where you may have some javascript). You can see the whole list here: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
This error occurs when you start rendering web page stuff before outputting a header. All headers must be completed first.
Check that all echos and prints from php are occurring after session_start() occurs. One tricky place that printing creeps in is when you end php tags and have anything there - even when it's at the end of a page. You don't need ending php tags at the end of your pages and even a space following one will cause this kind of problem. The safest thing is to just remove all ending php tags at the bottom of your php files.
Another tricky thing is that this kind of error is also caused by a "Byte Order Mark" being output by your code editor. You can try opening, fixing and saving the page in just a simple plain-text editor and see if that fixes the problem.

php - track down premature headers leak

I'm using set_cookie() on a site. After adding some functionality, I'm getting Warning: Cannot modify header information - headers already sent by... error. The line number it references as to where the headers initiated from is the very line where set_cookie() is! And I checked, it's not being called twice.
How can I track down these premature headers? I looked at the source code and didn't see any stray characters or anything before the error message starts ( I'm using xdebug, so the first thing is a , which I thought was me, but is actually the beginning of the xdebug message ). I've grepped my code for extra echo and so forth -- nothing.
Can PHP tell me when and where the headers are starting? Or are they really starting on the set_cookie line, and if so, how have I gotten myself into this situation, and how do I get out?
edit: I'm not sure I can paste the code -- do you want just the set_cookie() line? I thought the headers were coming out before that. And there's a lot going on before that with different classes being instantiated. And also I don't think I can post all the classes and stuff -- you know, proprietary information ;)
Edit: I've gotten rid of all terminating delimiters ( all ?>s) and made sure that the first characters of every included file is <?. This is frustrating! It was working before I did these recent changes :(((
Also, why doesn't headers_sent() work?
if ( headers_sent($filename, $linenum) ) {
echo "headers sent: $filename:$linenum.";
}
set_cookie(...);
gives
headers sent: :0.
You can use headers_sent():
if (!headers_sent($filename, $linenum)) {
set_cookie(/*...*/);
} else {
echo "Headers already sent in $filename on line $linenum\n";
exit;
}
One approach would be to run your script from the command line with STDERR redirected to null.
This will show you everything that is being sent to STDOUT, i.e. everything that the server would send to a client.
Hopefully, once you see this, it will be clear where your headers are coming from.
Besides using headers_sent to detect if the HTTP header has already been sent, you could use the PHP’s output control to buffer the output. That allows you to change the header even if you’ve already done some output.
Here’s some example using the explicit output buffering by calling ob_start:
ob_start();
echo 'foobar';
header('Content-Type: text/plain;charset=utf-8');
But make sure you call ob_start before any output was done. Otherwise the HTTP header probably has already been sent.
Can you paste the code?
If its saying headers already sent by [line where set_cookie is], then the error is further on in the script.
Are you sure that you don't have any include files being called prior to your set_cookie() initialization? Do you have sessions running, because if you do, you could just set the cookie at that same point in your script as you started the session.
If there is even a single white space before your set_cookie() initialization it will cause the headers to not be sent correctly. Maybe try using a different editor and checking your results.
I found it! It was a flush(); that I had left lying around all by itself in one of the pages. Surprisingly, it doesn't correctly bind $filename and $linenumber to headers_sent().

Can not output image into src tags

I have the following:
$imageurl = "<img class='item_thumb'
src='getimagethumbnail.php?imagename=".urlencode($product_image)."&from=".$prodimagedir."'
min-width='150' min-height='150' border='0' class='item_thumb'>";
Which creates the following html:
<img class="item_thumb" border="0" min-height="150" min-width="150"
src="getimagethumbnail.php?imagename=productsmall_1248886833bloggingbok.jpg&
from=products/"/>
However, the image does not show up. I point my browser to that src link and it gives me a bunch of unreadable text which I am assuming is the image meaning that the script getimagethumbnail is working fine. (I am guessing).
But as I said, the image is not appearing at all. What is wrong and what steps can I take to determine the problem?
Just to add, when I point my browser to that src link: It also gives me:
Warning: Cannot modify header information - headers already sent by
(output started at /home/dji/public_html/getimagethumbnail.php:35) in
/home/dji/public_html/includes/functions.php on line 4953
I am assume this is because of the output?? This script was working fine and I have made no changes to it as far as I am aware!
Thanks
You are trying to send the header('Content-Type') command, after outputting whitespace/characters.
You need to make sure that the header command is before anything is printed on the page.
This will work:
header('Content-Type: ....');
readfile('file.png');
This won't
readfile('file.png');
header('Content-Type: ....');
This is because the header command tells the browser what to look for in the content. All of the headers must be sent before any content because that is how the connections works. The browser can't be told what to expect after the content has already been sent.
Open Connection With Server -> Get Headers -> Get Content -> Close Connection
One of the big reasons behind this is encoding. As the content comes through, the browser has to decode it properly. If you send a header in the middle of the page telling the browser that the encoding type is a, when it was processing it like b, things can get really confusing.
So, in order to send the headers properly, you must put the header command before any output.
That error is caused when you print stuff to the output and then attempt to use the header() method. You shouldn't be outputting anything until after you do what you need with header(). Nothing should precede this, not even white-space.
You already have produced some output (on line 35) before setting the header for the image type. This might simply be a whitespace between php tags, or something you forgot to remove.
Your getimagethumbnail.php script is not generating a valid image; it's including text in it (the warning message you quote), which prevents browsers from rendering it. Judging by the error text, I'd guess this is due to changes made either to getimagethumbnail.php or functions.php.
Fundamentally, the problem is that functions.php is attempting to call header() after output has already been sent to the browser, which just plain won't work. You need to check both files and make sure that any calls to header() come before anything else that sends data to the browser.
You may want to turn off the display_errors setting, as any code which generates any warning or error for any reason will cause the problem you're seeing if the warning/error occurs before your header() calls. (Just make sure you have error logging on, so you can still see what's going wrong!)

Categories