I'm using an ajax request to send comments to DB. Succesful response is marked by
1. OK
The problem actually is that the response from the php script is
1.
2. OK
So I debugged the script and noted that the newline character si being added when the script executes the following line:
require_once($ABS_APPS."/quotes/classQuote.php");
After some searches i read that it could be a BOM (Byte Order Mark) problem. So I just downloaded and opened the classQuote.php file with an hex editor and noticed that there's no BOM... can someone help me?
P.S. All files in my project are encoted in UTF-8, and I'm currently usint NetBeans which doesn't add BOM to files.
This is the incriminated script:
// Send new comment to DB
case "send":
$notification = new Notification();
if($comment->insert($_POST["username"], $_POST["comment"], $_POST["app"], $_POST["entryId"])){
switch ($_POST["app"]) {
case "quotes":
require_once($ABS_APPS."/quotes/classQuote.php");
$quote = new Quote();
$quoteData = $quote->get($_POST["entryId"]);
// If user comments his own entry we don't have to send the notification
if($quoteData["UserAuthor"] != $_SESSION["User"]){
$notification->newComment($_POST["username"], $quoteData["UserAuthor"], $_POST["entryId"], $_POST["app"]);
}
break;
default:
break;
}
echo "OK";
} else {
echo "ERROR";
}
break;
Make sure there is nothing, preceeding the opening <?php in your classQuote.php
Make sure there are no trailing characters / lines after the closing ?>
Check to see if a ?> tag exists somewhere in the lines of code (follow flow from your __construct and where you invoke stuff)
Infact, it could prove helpful to leave out the closing tag. Another possibility is this:
// capture output
ob_start();
require_once($ABS_APPS."/quotes/classQuote.php");
$quote = new Quote();
$quoteData = $quote->get($_POST["entryId"]);
// If user comments his own entry we don't have to send the notification
if($quoteData["UserAuthor"] != $_SESSION["User"]){
$notification->newComment($_POST["username"], $quoteData["UserAuthor"], $_POST["entryId"], $_POST["app"]);
// trim whitespace
echo trim(ob_get_clean());
}
If you're using jQuery
you can use jQuery.trim(responseData) in your AJAX success callback, to get rid of the white spaces
see also here
http://api.jquery.com/jQuery.trim/
hope it helps
I also faced the same problem.
The final solution I figured out is following
Check all the files which are loaded into the file to which you are sending AJAX request
Remove the extra white spaces from the very first <? line and PHP starting tag If your file starts from line number 1, the first ever character should be this <?.
If they are PHP files, then don't add ?> at the end.
Try to keep the last line marker only up to the last line of code written. means that do not add extra spaces at the end of the file. If code finishes at line 32, do not go to line number 33 via editor. In fact, press the back button and clear everything below up to line number 32.
If you are using any PHP framework, then make sure that the hierarchy of all files loaded should not have the same problem. e.g. if you are using codeigniter framework them make sure that the calling function's controller and all the models loaded into that controller should not have the same problem.
I fixed by these
Make sure there is nothing, preceeding the opening
Make sure there are no trailing characters / lines after the closing
?>
Check to see if a ?> tag exists somewhere in the lines of code
(follow flow from your __construct and where you invoke stuff)
Related
functions/SkriptParser.php
<?php
$text = file_get_contents(basename($_SERVER['PHP_SELF']));
preg_match_all("/{(.*?)}/", $text, $matches);
var_dump($matches[0]);
echo str_replace($matches[0],"Test",$text);
?>
Is my current code, this is called from my index page which is here:
index.php
require_once 'functions/SkriptParser.php';
When i open index.php it replace the correct strings [ It'll replace any string inside {} however, it seems to be replacing <?php and I have no clue why.
Any ideas?
I'm not sure I fully understand what you are trying to do.
This line $text = file_get_contents(basename($_SERVER['PHP_SELF']));resolves to a local filename which will be the name of the script it's included in.
It won't load the page which that script would output, it will load the file without executing the code. The contents of $text will be a string containing the pure php content.
When I run your code from the command line and use "Here is a test of {your code}" as my string the output is:
{your code}<?php
require_once 'includetest.php';
print_r(basename($_SERVER['PHP_SELF']));
echo "Here's a test of Test";
If I run it from a browser and view the source I see that too, but the browser escapes the php so it isn't rendered on the page. So (for me at least), your concept kinda works. However, the fact the code isn't executed means it will never do what you intend.
Here's what I don't really understand though. In essence within index.php what you are telling your code to do is load a script which will load another copy of index.php and replace content within it.
If you want to change the behaviour of index.php then alter the code within index.php, don't generate unsuitable output and then load another script in an attempt to parse it before returning it. Just output it the way you want the first time.
Where is the content within the {} that you want to remove coming from? Why do you want/need to remove it? If you can clarify exactly what your aim is then it will be easier to suggest a solution.
I am developing w web site. Here I have included a thumnail page. It’s working well in my localhost. But when I uploaded its show me an error see
Warning: Cannot modify header information - headers already sent by (output started at /mydomain/demo1/admin/thumbnail.php:50) in /mydomain/demo1/admin/act-addVehicle.php on line 191
my code
if(move_uploaded_file ($tmpName,$path.$actual_image_name)){
$succ=1;
chmod("$add",0777);
$imgSucc=1;
//strt image thumbnail
include("./thumbnail.php");
// ends
}else{ echo "Failed to upload file Contact Site admin to fix the problem";
exit;}
How I avoid this error
is there any settings in php ini ?
does anyone know ?
please reply
Without seeing thumbnail.php's code its hard to tell exactly what's happening, but basically its outputting something before act-addVehicle.php on line 191 is setting another header.
Most likely a single space at the end of the script. Check after ?> perhaps line 50 if just after.
You could wrap the include('thumbnail.php') into an ob_start() and ob_end_clean() but that will not solve the underlying issue of outputting before setting a new header.
This kind of error typically comes from having a closing php tag in one of your file that is followed by some white space. This white space is sent to the browser when the script is executed BEFORE it executes a "header()" function.
For example, I would not be surprised that your thumbnail.php or act-addverhicule.php ends with a "?>" tag and that there is some white space after it.
As a good practice, it is always better to remove all closing php tag (the "?>") at the end of your files to avoid these kind of problems...
of course. use ob_start funciton somewhere in the beginning of your code
I apologize if this has been answered, but I haven't been able to find anything about it. I've been having trouble with a certain block of PHP code on my server and have finally realized that it doesn't like single line comments. With this code, the method get_all_articles is never called.
<?php
$article_class = new Articles();
// Fetch all articles
$articles = $article_class->get_all_articles();
?>
However, removing the comment or converting it to a block comment allows the page to render perfectly. I realize that I've already figured out what's causing the problem, but what I'm looking for is why or how I might be able to fix it. Any help would be greatly appreciated. Thanks!
Maybe the formatting is getting lost on upload to where line breaks are being deleted? Try downloading the PHP file after you've uploaded it and see if the line breaks are still intact.
This can be frustrating... One time I had an if statement that would ALWAYS execute, no matter what the values were...
Started out as this, where $x was equal to 5 (I verified this with debugging)
if($x > 10);
{
....
}
Eventually, I had it down to this:
if(false);
{
echo("This should never happen");
echo("but it does!!!!!!!");
}
After much loss of hair, I realized that I had a semi-colon at the end of the if() line, therefore translating into:
if(false)
/*do nothing*/;
{
//Nice block that always executes
}
The moral of this story is that while the problem you percieve is actually giving you a problem, it is not a PHP problem. Try to find out the root cause by first verifying that the actual code that is executing is EXACTLY what you typed. Re-download the file, publish with different protocol, publish as binary, check sha1sum() on files to make sure the same... Look and look and you will find it.
Let us know.
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
function new_photo()
{
if( !empty($this->data))
{
$this->data['Photo']['showcase_id'] = $this->Session->read('CurrShowcase.id');
$this->data['Photo']['added'] = date("Y-m-d H:i:s");
$this->Showcase->Photo->save($this->data);
$flasher = 'Photo uploaded successfully';
$flasher .= '<br/><img src="' . $this->data['Photo']['thumbnail_url'] . '"/>';
$this->Session->setFlash($flasher);
//$this->redirect(array('action'=>'sc',));
}
}
I have a Showcase Controller in my CakePHP app, and a new photo form to submit new photos. Whenever I uncomment the last line that redirects after the data is saved, I get this error:
Warning (2): Cannot modify header information - headers already
sent by (output started at D:\.....
Even if I get this error, $this->data still gets saved properly in the database. However, if I comment the redirect line as shown above, everything works fine and error-free. I HAVE checked for blank spaces around the tags, so I'm pretty sure it's not that.
Any ideas?
Edit:
commenting out the setFlash statement does not fix the problem.
Change your debug mode to 0 to make sure it's not a notice/warning being generated prior to the redirect. Also, you might want to tighten up your processing section to (be paranoid and) ensure that it's not using invalid indexes, as well as anywhere else throughout the application flow for that action to make sure you're not getting any ouput (if it works when you change debug to 0).
Is there a debug statement somewhere that you're not showing us?
You may be up against an invisible UTF-8 BOM character somewhere. Check your text editor settings whether it saves your files with BOM or without.
I'd check for whitespace in the models. Anyone of them. That was one of the gotchas I hit.
Either this code outputs something to the browser, or you have a whitespace after ?> in the end of the file (or any other included file). The whitespace is sent to the user thus sending http header.
I'm assuming setFlash outputs something to the browser?
If whitespace before or after your <?php ?> tags isn't your issue you might have to try passing 'null' for the 'layout' parameter of setFlash();
i.e.
$this->Session->setFlash($flasher, null);
I have a test.php script which contains this:
<?php echo 'test'; ?>
When I point to it via my browser, it works and prints out "test" as expected.
I have another script which I am trying to test but however, it is ignoring all my echos! I thought I would put an echo right at the top as this will surely work, but when I get to this script via POST request from a form. It does not even echo out what is at the top of the line:
<?php echo 'START START START';
error_reporting(E_ALL);
I even point to from my browser and still no output?
What the hell is going on?
Update
The error log shows this :
PHP Parse error: syntax error, unexpected T_ECHO in /var/www/tester/convertor.php
But the echo is at the top of the line. I am going to set diaplay errors.
You have a parse error, so the script isn't even executed. So it's expected that it won't output anything.
The parse error can be for any echo in the script. It may be because of an "echo" statement after a line missing a semicolon, for example.
Things to try:
Check your error log
Check the headers (is it coming back 404?)
Make sure you view-source: don't just look in the browser
Delete everything except the echo. If it works, add things back a bit at a time until it breaks.
Load your script in a hex editor and make sure there aren't any weird characters in there
Are you including this file from another file? If so, check the other file too. Watch out for output buffering.
Put exit; after the echo start start start and see if that works. Look in the apache error log for clues. Comment out the rest of the PHP code in the file and see if it works then...
UPDATE
I have had this when copy pasting white space from a browser sometimes - e.g. copy/pasting some code from a web page. Sometimes weird control characters get embedded invisibly in the white space, and I find that deleting the whitespace and re-entering it fixes it. Did you copy paste anything into this file?
Are you hosting this page on a server with PHP installed?
If you just have a .php file on your hard drive somewhere and open it in a web browser, it won't work. You need to be running a web server with PHP extensions and access the file using the HTTP or HTTPS protocols.
On a similar note to this i have seen alot of scripts throw errors like this when they have come from developers on windows (i'm on linux).
I have to go to the start of the php file and hit delete a couple of times to get rid of some invisible characters before the script will run.
You're missing the ending ?>
<?php
echo 'START START START';
error_reporting(E_ALL);
?>