Why is PHP adding whitespace to PRINT output? - php

I'm having a problem with PHP that I cannot find the answer to:
I'm using PHP5.5 and Flash AS3.
I have a controlfile.php which I list the db connection info in.
When I connect via the Flash AS3 login screen, and if all is good, I expect the reply from the PHP file to be returnMsg=1 and indeed that is exactly what I get when I check with Charles 3.8.3. and the FLash Movie advances nicely
However, and this is where it starts getting weird, I have now moved my db connection lines from the controlfile.php and placed them in a connect.php. In the controlfile I have added the line require_once("connect.php");
I ran the same scenario and my Flash movie does not progress beyond the login screen as it should. I checked via Charles proxy and was indeed still getting the returnMsg=1 from the controlfile.php. Confused I checked the flash movie and put a trace on the event.target.data - it returned %20returnMsg=1. I looked back at Charles and sure enough, although I didn't notice at first, there was a blank space.
My question is What is causing this space to suddenly manifest itself when I use a require_once. As a beginner to PHP none of this makes any sense. as a work around I can ask the controlfile.php to print returnMsg=1&returnMsg=1 and the output Charles is <space>returnMsg=1&returnMsg=1. If the Flash trace it appears as %20returnMsg=1&returnMsg=1 and therefore, beacuse its is actually getting the returnMsg=1 (without the %20) the movie will progress. Sorry for the lengthy question but this is driving me completely nuts... Someone, please help with this PHP thing

Related

object not found $_GET

i'm trying to learn PHP but for some reason i am getting this weird issue (i am using XAMPP):
i have a simple code
<?php
echo $_GET['name'];
?>
and when ever i type http://localhost/lee.phpi get undefined index like i am supposed to, but when i type http://localhost/lee.php&name=lee i get an Object not found The requested URL was not found on this server error.
does anybody know why is this happening? is it my code or my pc maybe?
http://localhost/lee.php&name=lee
Is an incorrect URL altogether, it should be
http://localhost/lee.php?name=lee
^
Now if you were to add a new parameter you would use an & only then. First one always comes after an ?, for example
http://localhost/lee.php?name=lee&age=20
I was going to refer you to HTTP Specifications documents but since you mentioned you are learning PHP i thought that might be too overwhelming for you.
And your code is fine.
Free Tip since you said you just started learning:
Always read into error messages and believe in what they say while you are investigating an issue, they are there really for a purpose. For example
The requested URL was not found on this server error.
That error message would mean that the URL is not there on the server, if i was you i would care less about my code at that point and more about what is the reason that URL is not there when my file is there? And that would have lead me to the conclusion that the URL format is wrong.
A lot of people overlook error messages even in their advanced learning stages and say no I have everything fine and the error message is weird, no it's not.

Why "Max made me put this here"?

Every MediaWiki has a load.php.
If called without parameters it returns:
/* No modules requested. Max made me put this here */
As a curious programmer I wonder:
Why did he do this?
I am sure in a big project like this there is a good reason for it. Looks to me like it would be bad to return an empty file to an ajax query or something like it.
BTW: Normally it is called with parameters like this: load.php?debug=true&lang=de&modules=user.options&only=scripts&skin=modern&user=pi&*
This message comes from ResourceLoader.php. In the history of the file, using git blame, you can see the code was written by Roan Kattouw (RK) in this changeset. From the changeset comment:
Make load.php output a comment explaining what's going on when no modules were requested rather than outputting nothing. Max made me do this because he hates blank pages
So, your answer is, because Max hates blank pages, and if you want to know more, you should ask Roan. My guess would be that it's a debugging aid; rather than staring at a blank page wondering why it's blank, at least you know that you did something that caused a module loader request to load nothing...
As #svick points out, there is also a link to the code review, including discussion of whether it's a good idea to mention Max at all. Mentioning Max was seen as a possibility to partially close MediaWiki bug 20281, which notes that there aren't enough Easter Eggs in MediaWiki.
And that's why public repositories of open source software are cool :D
It is just to know, whats going on.
If i browse the load.php file of my MediaWiki installation with my web browser and want to check if there are any errors, they may get displayed or leave me a blank page.
A blank white page indicates a PHP error which isn't being printed to the screen.
But if i see a comment thats similar to /* No modules requested. Max made me put this here */. i do know its alright
AND that is the reason, why they needed to diff it.

AJAX / PHP not working on new WAMP server

We've moved a php page from one WAMP server to another and the chap who created it has left the business. I know nothing about AJAX, so am struggling!
I've scoured the code on both the old server and new, and it's exactly the same but for some reason the AJAX on the new server isn't working correctly, whereas on the old server it's fine.
What it's supposed to do is show a list of people from our database, allow users to update those records and then show the new, updated record without refreshing the page.
On the old server, it works a treat but on the new server it doesn't load the new data through. It's probably something ridiculously basic, but I'm scratching my head (mainly as I know nothing about the technology!)
Any help much appreciated.
Have you made sure the script that you are requesting with AJAX is actually there? You have said you've uploaded the script but is the path exactly the same? Different environments may have different paths to the same script.
Also how are you making the AJAX request? Are you using (for example) the jQuery library that might exist on one environment but not the other? Are they both using the same version of jQuery?
Use the Javascript console to see what errors you're getting and go from there. Chrome and Firefox have the best ones by default.
https://developer.mozilla.org/en/Error_Console
Before any of this though I would make sure the original script is working and returning the right results.
First check:
Look into the Javascript code and find where the actual URL is constructed.
It will look like:
xmlhttp.open("GET","yourAjaxPage.php",true);
but the xmlhttp can have a different name.
Now, simply alert this URL, or document.write(), or anything that gives you the url.
Put this URL into the browser-url and see the response.
In case you are using a session, make sure you use the same browser (TAB for example) where you expect the result.
Now you will see the response from the server to the request you just made. This should get you going finding the problem.
With the error console we were seeing an error 500 from the server (which seemed odd and rather unspecific...!)
Having had someone we know take a look at the code, it seems that on the old server, it seemed quite happy using mssql_close($con); but on the new server it didn't and so simply replacing it with sqlsrv_close($con) then meant the page the ajax was calling could complete and then returned the results as expected!
Always the little things...!

Internet Explorer not rendering pages correctly because of a PHP class

I have a very strange problem and i don't know what to do about it. My site seems to work just fine all browsers other than internet explorer, so i've been trying to figure out why.
I've narrowed it down to the a file that I'm including in my site, this file is a php class that has a number of different functions like login getters and setters and so on.
I took all the php code out of my pages and it renders fine, so i added the php back in line by line and released that it stopped working when i used this:
require_once 'classes/Membership2.php';
Does anyone know why some php code will be messing with the style of my website.
For more detail on the matter, i have a number of divs that are centered, they all have curved edges as well as shadows. So by taking away the php i can see that IE is loading the page properly, no incompatibilities or anything like that.
Has anyone had a problem like this before? While i'm waiting for an aswesome or two i'll be removing functions and part of the code till i can narrow it down. (I would give code, but the file has a lot of lines of code.)
Thanks for the help.
Oh yeah and I'm testing on Internet Explorer 9 and every other browser is the latest version or close enough.
Okay so i've done some more digging into this, i've found that if i delete all the code in the class (All the functions) and leave just and empty class in the include file it still doesn't work. Okay, so in my view that means the functions aren't whats making this problem. So i deleted EVERYTHING, so now the include points to a blank php file. This worked and the page rendered as it should but obviously there is no functionality, i can't login or anything like that. I decided to add a constructor instead of leaving it as default, this function does nothing but return true; and it made the site mess up again.
Does this info change anything? Also i'm reiterating the fact that i do not get this error or any other browser but Internet Explorer 9 (Haven't tried any other IE version).
Thanks again for the help.
Okay, so i've solved the problem. At the start of my PHP class i have used
<!-- blah blah blah -->
forgetting that there is only PHP in this document and no HTML. So when i include the file it just outputs that into my source code and and messes things up, should have used the PHP commenting style.
Still strange that EVERY browser other than IE just ignores this and goes about its business, even the site that #blankabout suggested didn't give me any error (Although i assure thats because its part of the included PHP file and not the HTML itself).
as #fajran says to you, save both outputs with "view source code" on the browser and compare them to find the diference. To compare outputs use winmerge or similar tool. Once you now which text it generating the trouble, modify it inside the include file.
Given that your php, because it runs on the server, should never actually reach the browser, it may very well be some unterminated HTML or similar that is causing the problem. Perhaps the PHP is causing a break in the HTML that is unexpected.

PHP Javascript page loads twice

I have written this small site, with registration and everything, and I got to a point I think I am not too sure what is happening.
It first started as the DB reporting to me that the user I am trying to write into the DB has a duplicate entry (where it should be unique), which really puzzled me, how can it be that I have duplicate ? Well. It took me three days to realize that the page is being somehow called twice !
I put a
$_SESSION['one']=0;
and a
$_SESSION['two']=0;
in the topmost and bottommost parts of the page accordingly.
then I changed them both to ++, so I can check how many times they have been passed through.
I used the verification link from the emaill the site has sent, and tested their values.
Strangely enough, 'one' would equal 2 and 'two' would equal 1...
This explains exactly why all worked registration-wise, but I got all those errors about those duplicates.
Though, I used firebug to trace any redirections, but couldn't see anything...
It shows the page has 12 GETs and a POST.
I was hoping to bump into a redirect and debug accordingly, but alas, or maybe I don't know how to use firebug to trace these redirects...
I would appreciate any suggestion
Thanks in advance!
Like I said in the comments, here is the answer that worked for me and Ted:
What I do to fix it is change my doctype to html 5 and then validate the page using w3c validator, this problem was only occurring for my in firefox using firebug.
Do you have some <img> or <script> with src=""? Or maybe some <link> stylesheet with empty href?
Is firebug showing you, in net tab, that your site is called twice?
This kind of issues usually happen when you have a fatal or otherwise unignorable error that forces PHP to terminate you script early, in the midst of processing a request.
Check you error log for details of happening.
It is Firebug that is causing the page to load twice. Apparently changing the doctype to html5
as:
instead of html4 has overridden that bug.
When you are going to deploy your site, go back to html 4, and of course, always keep the server safe from such bugs. Use DB constraints, validation and escaping when needed.
Hope this helps, and Big thanks to #jeffreydev!!

Categories