Why include prevents a php page to load? - php

I have my index.php page and just afrer my header i want to include a message.php file.
This message.php file executes some mysql queries, runs a couple of functions and in the end makes echo of some text.
If i go to mysite.com/message.php - i can see all the text i need in my browser. But if i add the following line:
<?php include "message.php"; ?>
to my index.php, the page loads only until the include statement and then produces 500 internal server error. How is that even possible? I'm totaly stuck here...
EDIT: I've figured it out, thanks for pointing me to the right direction with apache logs. Even though i didn't have the log access, i've made the edit to .htaccess with a statement php_value error_log log.txt. It dumped the error in the txt file in the same directory. The error was produced by a function that was a name that was already in use. That's why a separate enviroment wasn't producing the error.

Add the following to the top of your message.php:
ini_set('display_errors',1);
error_reporting(E_ALL);
As everyone has said, there is probably a simple error (maybe a missed quotation mark) that is throwing the error, but in that environment, it gets thrown as a 500 error rather than output to the screen. Enabling the above should reveal the problem.

As dm03514 pointed out, it might be that there's something in your message.php which is breaking your code. If there's a fatal error in that file, it'll cause a similar error in your main file.
Ask support for your error_log - this really helps debugging.

This is possible becuase you have an error in your message.php file! The next step is to figure out what is throwing the error. You can look at your web servers error logs to see the exact line. To solve this problem you only have to focus on the code in your message.php file.

Well, if you don't have access to the log files, try to run the same file in any other environment where you can see the errors.
That's the only way available.
Asking strangers to tell you what error you have in your php file makes very little sense, you know ;)

Try to make a copy of index.php, say copy.php, insert a test.ph include, displaying just some "KILLROY WAS HERE".
If that does work, go for <?php echo "..."; ?>.
if that does not work, place the include elsewhere and/or remove code.
One cause could be a hidden redirect, blocked because output already happened.
More likely some global variable was shared.

Related

Cant see any changes on browser when modify code YII

Hi I have got project where I have to do some changes, but then I do some changes in php files I can't see any changes in web browser, only then I deleted files, when I see error in windows, but if I comment all lines from same file, and want see changes, when I refreshed the page where will be page like before, what means I see page like normally, and if I download the file and open it I see commented lines.
So I am using YII framework, I understand that I should turn on debugging on, so in [project-name]/index.php file in the top I pasted code.
defined('YII_DEBUG') or define('YII_DEBUG',true);
But it didin't work for me, still can't see any changes.
I also try ctrl+f5 on page refresh.
Maybe I should look in to Apache configuration?
If some one know please help.
I believe that defined('YII_DEBUG') or define('YII_DEBUG',true); causes a debug dump to be sent to the screen when an exception is thrown. Without that code there will be a single line exception error.

Why PHP include is working on local server and not on website

Problem Description in Brief:
PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html file, but does not work when I upload it to my website. Note that I have made sure that all paths are correct, and that the script file has its own php tags, etc.
Problem Description in Detail:
Yes, I am new to PHP scripting, and yes, variants of this question have probably been asked before. The answers to a few of the questions I have read have noted the path of the php script files to be incorrect. I have checked all paths and confirmed that they are indeed correct (including those on the web hosting server). Furthermore, I have been successful in getting the script to work on my local server running Apache2 with PHP5, but have not been successful when uploading it to my website.
Essentially, I am trying to implement a hit counter script which I have acquired from a Stack Overflow post labelled Visitors counter for simple web sites like Vinaora. The code that invokes the php script looks something like this....
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include($_SERVER['DOCUMENT_ROOT'].'/php/hitcounter.php'); ?&gt
&lt/footer&gt
For the likes of me, I cannot figure out why it does not work on the web hosting server. I have tried other combinations of invoking the script like,
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include('./php/hitcounter.php'); ?&gt
&lt/footer&gt
and,
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include(dirname(__FILE__).'/php/hitcounter.php'); ?&gt
&lt/footer&gt
All combinations seem to work on my local web server, but not on the website! Also note that, I have no problem invoking other PHP scripts using other methods (even on the web hosting server), eg.
&ltform id="form-query" onsubmit="this.checkValidity();" action="./php/contact.php" method="post"&gt
Any advice/suggestions would be appreciated.
Do you get any PHP error?
First of all, you need to activate error reporting.
Put this before including your file
ini_set('display_errors',1);
error_reporting(-1);
PHP should tell you what's happening.
If you don't see anything, change the filename index.html to index.php and try again.
Maybe be you have used "\" in your include path
Wrong:
<?php include 'includes\header.php'; ?>
You should use "/" to work.
Current:
<?php include 'includes/header.php'; ?>
sometimes it might be dues to casing. check if you you uppercase in naming. on some servers Index.php is not equal to index.php
You might try pasting the "include" code directly into the calling code. Maybe it's the included code itself that's misbehaving...
You are doing completely incorrect thing in the first place.
PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html
is just totally wrong
There is no such thing as embedding php file within html file (aside from mod_rewrite ...).
For PHP script to be interpreted you must have it (in 99% of cases) with php suffix. This way you allow PHP to distinguish it from regular PHP and send to php interpreter.
Put simply - create this file (a.html):
<body>
abcd<?php echo 'efgh';?>
</body>
and see the result in your browser - use .../a.html
What do you see?
abcd
and not
abcdefgh
On top you always have to have php not the other way around. Solve this or update your question if incorrect.

How to view PHP errors when everypage file is above header

This is something that has been bothering me for a while now and I'm sure there's a simple solution.
So my basic page structure starts like this with the every page file first and the header second.
require_once '../private/core/ep.php';
include template('header');
the ep file contains autoincludes for classes and functions, so everything inside of there occurs above the header. Whenever errors occur internally I often have to inspect the elements in my browser and navigate through the HTML to find what the error is which is really time consuming. Is there anyway around this?
There are a few things you can do to keep your errors from showing up in your header. If you have access to make changes to your php.ini you can configure your error log so that all of your php errors can be logged there and set display errors to false. These can also be set in your ep.php file at the very top using the ini_set function.
Another solution is to configure your own error handler using http://www.php.net/manual/en/function.set-error-handler.php
You can then open a terminal and use "tail -f /my/log/file/location" to actively monitor your errors.
You could write a LOG function to a file or better to a database.
You will see exactly where your code broke.
Another thing you can try in enable error reporting on your server (php.ini)
I don't think there is any other way.
Ovidiu

Handling white screen of death in PHP

After debugging a CodeIgniter application that were installed into a new development environment, I have started to freak out when seeing white screens with nothing more available. I have been able to solve each and every one of the errors that have caused this, but it has taken seriously way too long time.
PHP error_reporting(E_ALL) & display_errors", 1 is set as well. I even installed Xdebug in hope of getting more output, but no. My logging settings are also working, but nothing is written to the log.
Is there a way to get something informative printed out instead of a complete white screen? It would certainly shorten my time spent on solving the eventual errors that cause this.
Reference:
Why does Code Igniter give me a white page?
If there's a fatal compilation error, then you may well get a blank page.
Try doing a
php -l <filename.php>
against your script
Look near the top of /index.php for a call to error_reporting() - and make sure it's not changing your php.ini configuration to something else (besides E_ALL).
And since you didn't mention your php.ini configuration, check to ensure you have error_reporting = E_ALL there as well.
I've found out, since the time of my question, that nothing seems to ensure that errors are always outputted with PHP, which seems to throw white screens here and there. Regardless of PHP's ini-settings.
I've found out that the best workaround however is to use the following line to ensure that error logging is put into a file easily is accessed and monitored by the application:
ini_set('error_log', MYPATH .'logs/errorlog.log');
As far as I've tested it, when white screens appear - it also gets logged into this errorlog. It seems to be the easiest way to know what happens when things go wrong.
Grep the files for 'error_reporting', and 'display_errors'. The application might turn it off somewhere.
Also, to be able to see parse errors, you need to set error_reporting/display_errors in the php.ini file, or a .htaccess file. Setting it in the script files will not do and will lead to the white page you describe if there are parsing errors.
Aside from everything else posted, also make sure that something masked with the # (error suppression operator) isn't throwing a fatal error.
The best thing is to have a checklist of the common problems that could cause this since CodeIgniter's default is already
error_reporting(E_ALL);
Same name controllers and models
using reserved words as methods
The list goes on...
Consider setting PHP's error_log configuration variable -- it can be helpful when you have code setting error_reporting() without your knowledge. Then you can check the error log and see what errors, if any, occurred.
I had this problem on my freshly installed server. Debian 7.
I enabled logging, error reporting, disabled gzip and so on.
However, my PHP-installation did not have MySQL enabled. Enabling MySQL did the trick for me.
Make sure your logs and cache folder inside /system are chmod'ed to 777.
Ensure that there isn't any whitespace in your files output outside of the CodeIgniter buffer, especially if compression is turned on. You can test this by turning off compression in your CodeIgniter configuration file.
See step two at: http://codeigniter.com/user_guide/installation/upgrade_141.html (Note that while this is for the upgrade, it contains a snippet of the configuration file which explains the problem.)
If you by chance have happened to create a cached output for that particular method inside your controller, then it may create a cached version of the page and practically that page is not even running.
The cached error output page is showing up. Please check your cache folder inside the application. It should only contain the index.html file.

php file not outputting results

I have developed a script but whenever i open admin panel it shows just a blank page, however the same script works fine on my another server. I tried changing chmod of files and folders 644, 755, 775 and 777 but still it outputs a blank page.
The main script works fine but its only admin panel thats not working. I check .htaccess also same on both servers.
Any idea what is wrong?
Thank You.
Check the error log of your HTTP server (e.g. Apache). In 99% of cases of a blank page, PHP has experienced a fatal error and exited before generating any output.
If this script works on another server, I'd check that the script can find everything it needs to include or require. Files not found are common fatal errors when moving a script from one environment to another. For instance, if you deployed the file to the new server without configuring the include_path correctly.
Re your comment about the notice you got:
Notice: Undefined index: submit in /var/www/admin/index.php on line 8
the function on line 8 is if($_POST['submit'] == 'Login')
This means your $_POST array does not contain a field 'submit'. Referencing a non-existant array index in PHP is an E_NOTICE. You can fix this in the following way:
if (array_key_exists('submit', $_POST) && $_POST['submit'] == 'Login')
put this at the top of your admin page
ini_set('display_errors',1);
error_reporting(E_ALL);
The servers probably have different things installed which is causing an error that you can see.
This may sound stupid, but seeing as you all you said was it "outputs a blank page", are you sure there's no output at all? Have you checked the page source, just in case? Remember that if your page starts with a bad HTML tag or something similar, there will be output but that doesn't mean anything is actually rendered in the browser.
Also make sure output buffering isn't on... I can't remember if this can cause issues when trying to debug, but who knows?

Categories