php function header() doesn't work - php

I have a php script on a apache dedicated server.
I've cleaned the code to test if header() works... here is the code I test :
<?php
header("Location : http://www.lemonde.fr/");
?>
The page loads and display a white screen, no redirection.
I think it should be a php.ini settingor a apache setting, but which one ?

The first thing to try is changing Location : to Location: (it may be sensitive to the extra space).
Is this the only code? A WSOD is usually the sign of a PHP syntax error, but this looks clean.

Related

Blank Page after include command

If I use a code like this:
<?php
echo 'Text';
include("Class/MyClass.php");
echo 'Text';
?>
This code only returns one 'Text'. (The one before the include)
I don't get an error (error reporting on), and the code works on my local XAMPP.
Debian 6.0 / PHP 5.3.3-7+squeeze15
Apologize in advance - it's not the answer but just wild guesses.
"Class/MyClass.php" (or any file it includes) contains somewhere exit() or die() calls that depend on configuration.
Try setting error_reporting(E_ALL) and ini_set('display_errors', '1') right before including.

Why is my PHP code passed in source to the client?

I'have just started to learn PHP, I'm using a free host to test my code but nothing happens and also my php code passed in source of page, does it show that server don't interpret it?
Yes, that shows that the server isn't interpreting it properly. The user should never receive PHP code, just the html/javascript/whatever that your PHP script outputs.
As for why this is happening, here are a few basic things to check:
Your PHP code should begin with the <?php tag and end with the ?> tag (the ending tag isn't strictly necessary, but any code you put after it won't be interpreted).
The document's name should end with .php (not always necessary, but some server setups may require it).
If you haven't checked already, make sure that the host you're using supports PHP in the first place.
Is php code passed in source to the client?
No.
Your PHP interpreter isn't being invoked.

How to get debug printouts from PHP?

I don't have access to the apache server or whatever is running on the server. I just have a free account with a web host. I do apparently have access to the .htaccess file, but I'm not sure if I feel safe enough to temper with the "nuts and bolts" as it were. So instead I tried to enable debug printouts in my php script like so:
error_reporting(E_ALL);
I'm not seeing any errors, but I know that there is an error since the entire script isn't executing.
Is there an easy way to get error printouts to show in the code that php returns? If I tried to temper with .htaccess, what would I have to do there? It's only 36 bytes long.
DO:
error_reporting(E_ALL);
ini_set('display_errors',"On");
You could also try debug_backtrace().
var_dump(debug_backtrace());

Do I need to modify my Bluehost php.ini file? Header function not working on remote server

This block of code shown below using a php header redirect works locally, but not on my Bluehost server:
if ($_POST['submit']=='No')
{
$url ='Location: index.php?id='.$id.'&page='.$page;
header($url);
exit;
}
When the server gets to this block of code, absolutely nothing happens. No error, no warning, just a blank page. The page that my form submit redirects to isn't supposed to do anything except reroute the user to the relevant page.
I'm pretty dang positive it has nothing to do with the common problem of including HTML before the redirect (since it works locally). Therefore I suspect it has something to do with differences between my php.ini files. I've pulled up PHPinfo() for both servers, and my local server has a module named mod_headers while my Bluehost server has none. I think this potentially could be the problem, although normally my Bluehost has no problems using header redirects, except in this one instance.
So I suspect the problem has something to do with my ini file, but I don't know exactly what.
What makes this problem even stranger is that there are other blocks of code which work just fine, for instance
if(!empty($_POST['id']))
{
$id = htmlentities(strip_tags($_POST['id']));
$sql = "UPDATE entries SET title=?, entry=? WHERE id=? LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->execute(array($title,$entry,$id));
$stmt->closeCursor();
$url= 'Location: ../index.php?id='.$id.'&page='.$page;
header($url);
exit;
}
works just great.
I had the same problem that it works fine on XAMPP but not on bluehost or 1&1.
PHP documentation says there should be no output before calling the header function.
http://php.net/manual/en/function.header.php
In my case there was a space before the opening <?PHP which made the header function not work.
This should help to solve the problem.
The quesiton remains though: why does it work on XAMPP and not on the servers of bluehost, 1&1, etc?
(since I use firefox for both tests - it is definitely not a browser issue)
I just had this problem. I realized that I was using an editor that was inserting a BOM at the beginning of the file. This BOM character is virtually invisible to most editors but will stop PHP's header from firing because it does count as ouput. Here is more information on the BOM character:
http://en.wikipedia.org/wiki/Byte_order_mark
I was using notepad++ and was able to disable this BOM by using these directions:
Go to Settings > Preferences > New Document/Default Directory
Changed Encoding to UTF-8 without BOM
The HTTP standard requires that the URL in the Location: directive is an absolute URL. You can't just use index.php for that redirect. You'll need to use:
header("Location: http://example.com/index.php");
Some browsers ignore the standard and allow relative URLs.
I faced the same problem when running moving my Wordpress blog to Bluehost.
The solution was to change output_buffering option in php.ini
Look what at my config along with the explanation of this option:
; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit. You can enable output buffering during runtime by calling the output
; buffering functions. You can also enable output buffering for all files by
; setting this directive to On. If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = 4096

Virtual function - including some html

I have a php-file which includes another html-file:
<? virtual("../top.html");?>
The problem is that any code before this include compiles and runs well, after - nothing. There aren't any errors etc. After commenting this line, everything works.
Code was written under local computer with ArchLinux + LAMP. Now I have ubuntu 10.04 with the same configuration.
What could it be?
You might try changing top.html to top.phtml and using require_once.
<?php require_once('../top.phtml'); ?>
If you just want to pass some html from a file into your output, you could also use:
<?php echo file_get_contents('../top.html'); ?>
That way, you stay independent of the underlying webserver and you make sure, that no php code that may be in the html is being executed.
However if you wish something in there to be executed, you can use require_once() as stated by Jeremy.
Can you check error log of the Apache?
Also, you should use
<?php virtual("../top.html");?>
if your php.ini has short_open_tag = Off.

Categories