PHP - Weird behaviour after calling functions in a map - php

I'm experiencing quite a strange reaction from the browser when invoking this PHP script.
<?php
$map = array(
'a' => function(){
print_r('a');
},
'b' => function(){
print_r('b');
}
);
$map($_GET['v']);
?>
I already noticed that there is a mistake there. The syntax of the call is wrong, as it should be like this:
$map[$_GET['v']]();
The thing is that the reaction of the browser to this mistake is not what it should be.
The result of running this script is a 'The connection was reset' message. The server is up and running correctly, as other PHP files (and this one after correcting the mistake) run perfectly.
But what is actually puzzling me is what the navigation bar of the browser does. When I punch in the URL
localhost/cerdo.php?v=a
the content of the bar changes to
www.localhost.com/cerdo.php?v=a
The www.localhost.com part seems to happen only in Firefox. I've tried it on Chromium and, despite showing a similar message ('No data received') the URL stays the same.
What is happening? Does this make any sense? Shouldn't PHP be reporting a syntax error? And why on earth would Firefox redirect to www.localhost.com?

Shouldn't PHP be reporting a syntax error?
No. If PHP is not reporting $map($_GET['v']); as syntax error is because it is expecting the code to be syntactically valid. So it ends up executing some very weird stuff that you are not expecting. This results in redirecting your browser to some unexplainable location.
The key here is to understand what $map($_GET['v']); actually means.

The redirection doesn't have anything to do with your code. How is your environment setup, e.g. are you using Xampp etc.? In that case make sure there is any index.php or .htaccess or anything else that contains a redirection script in your web root.

If it's not your first time with you dev enviroment, please ignore this:
I think your problem is: the OS couldn't recognise the domain name (and the OS extends it).
Under Windows you can find the 'hosts' file here:
"%SYSTEMROOT%\System32\drivers\etc\"
Add this line to the end of the file:
127.0.0.1 localhost
(Maybe you should restart your computer.)
It should solve your

Related

XML Parsing Error: xml processing instruction not at start of external entity

I have a vBulletin 3.8 forum.
When we click Edit button of any post (so the Quick Edit form should displayed), I get this error on the browser's console:
XML Parsing Error: xml processing instruction not at start of external entity
Location: http://www.xxxxx.xx/ajax.php?do=quickedit&p=438
Row number 2, Column 1:
... the Quick Edit form is not appearing the the progress bar displayed permanently.
I have try to disable hooks/ plugins, but the problem still appears.
I have this row on config.php: ini_set("display_errors", false); so I don't think it is a fatal error/ warning by PHP which brokes the xml normal syntax.
I have informed that this appear starts after the move of the site to another server. Does it say something to you?
Any general idea about this error?
EDIT:
Well, I found the reason of this issue, but I don't know how to fix it. Exact the same site on a localhost testing board works perfectly, but on the live server ANY html page/ ajax call etc, has a useless empty line as line #1.
For normal html pages, there is no reason for the browser to return an error, but when we're talking about an ajax call, this empty line at the top of the response, breaks the xml parsing from the browser. So it seems it is a server/ PHP/ Apache setting that applies this empty line. Any idea how to fix it? https://imgur.com/a/4neb0
It might be late for you but any new comers with php/nginx/apache can get an understanding of why.
Answer is simple: When moving the code, you might not be using git/rsync/scp but let me guess, you used zip (and probably Windows/Linux involved).
How to discover it was a two-day journey with many things tried:
We have the same error, we were also moving our servers. We tried:
We thought the server software version was a problem.
We thought the cloud provider OS image was a problem.
We used docker to avoid these problems, but the empty line problem persists.
We thought the code ?> ending was a problem, I went through all of them. But it wasn't.
I finally asked my colleague: How did you get the code? From Git? He said he downloaded from ZIP and then uploaded to server.
I removed code on the server (which extracted from a zip) and used git to download a fresh copy from our github.
Magic, problem solved. The empty line gone.
So I think the problem is with the zipping progress might have changed some file empty lines. Always use git.

PHP error after new install of xampp

I was recently switched computers at work, and am trying to get all my projects up and running again. I am working on testing some php/html/javascript pages for a website using a local xampp server. I had everything working properly on the only computer, but when I try to load the pages now I get the following error:
Uncaught SyntaxError: Unexpected token <
I have tried a php-only test page, so I know the sever is capable of parsing php.
I also know it is not the short tag issue, as all of the php code is wrapped in
<?php...?>
Are there any other configuration settings that would be causing this issue?
The error message doesn't look like a PHP error. Check this discussion maybe you can find the solution there.
It seems the error is reported by Google Chrome when it expects to parse a .js file and it finds HTML (or PHP?) instead.
I think it is a JavaScript error, you should check your js code, not php.
The following reasons might cause this error:
href attribute value "javascript: void ()", no brackets add "0"
Button // error
Button // correct
Non-compliant JSON string
etc.

Deploying a Symfony2 project

We were working on a Symfony2 project. Now, it's done and ready to be deployed. We uploaded the whole project files to the server (via ftp of course) and the database as well. Now when we open any page of it we got just a blank page (empty source code). Cache is clean, logs do not show anything new. We googled the steps of deploying a Symfony2 project to a hosting but we did not find a good explanation (even these ones were about Symfony not-version-2).
We believe it maybe a configuration issue, but no idea so far.
Any help will be greatly appreciated.
Thanks in advance.
Edit: the blank page is in Firefox. Google Chrome is saying something:
Server error The website encountered an error while retrieving
http://*.com/mammoky/web/app_dev.php/main. It may be down for
maintenance or configured incorrectly. Here are some suggestions:
Reload this webpage later. HTTP Error 500 (Internal Server Error): An
unexpected condition was encountered while the server was attempting
to fulfill the request.
error_log is showing:
[24-Mar-2012 23:29:24] PHP Parse error: syntax error, unexpected
T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in
/home/leadow33/public_html/mammoky/web/app.php on line 7
It's: use Symfony\Component\HttpFoundation\Request;
And
[24-Mar-2012 23:15:08] PHP Warning: Unexpected character in input:
'\' (ASCII=92) state=1 in
/home/leadow33/public_html/mammoky/web/config.php on line 84
It's: $reflector = new \ReflectionExtension('intl');
EDIT: I've posted my solution, check it out down here.
Answer:
I got it to work, this is my experience:
Upload the whole project folder to the server.
Enter www.your-website.com/project-name/web/config.php.
It should say: "This script is only accessible from localhost".
Open this web site: http://www.whatismyip.com, it should show you your public IP address, copy it.
Open the config.php from the admin panel (like cPanel) and edit that config.php:
if (!in_array(#$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1', /*your IP here*/))) {
header('HTTP/1.0 403 Forbidden');
die('This script is only accessible from localhost.');
}
Refresh your config.php file, the page will tell you if your system is missing some required conditions like: PHP version, APC extension, giving /cache and /log folders permissions to write on, etc.
After you provide the required conditions you'll see a form of configuring you project to connect to a database if you have one, this step is pretty simple.
Open the link www.your-website.com/project-name/web/app_dev.php, it'll help you get started with Symfony2 project.
In case you got in app_dev.php this message: You are not allowed to access this file... just do the same thing to app_dev.php as you did in steps 4 and 5 (add your public IP address to the array).
Note of Hakan Deryal (comment): If you don't have a fix IP address, you need to do this last step each time you get a new IP adrdress from the DHCP. So to solve that, open the app_dev.php and comment out the line die('You are not allowed to.., however this is not a recommended way because you're disabling the built-in security of the file.
One thing stopped me and may stop you too, the server I deployed the project on, was case-sensitive (unlike the localhost on my computer), so it kept telling me that the template (Index.html.php for example) does not exist, however it does exist, but I did return $this->render('...:index.html.php') with small i in DefaultController.php. So render the exact template (file) name with the same letters cases.
Now everything is going well, I hope that helps you.
In my case it was the php version* and I catched the error by running config.php with removed lines for checking ip address. On my hosting I just changed the default version of PHP for my scripts.
*Since namespaces are only from 5.3.
production isnt app_dev but app.. The page might be the same did you do a view source? Did you try to run the config.php that comes with the framework zip ?
If your installation runs on a shared webserver, try setting the following line into the app.php:
date_default_timezone_set('America/Los_Angeles');
This solves the problem in my case.

PHP website does not work, and nothing logged in the error log

I have a problem with PHP. There is an error that I have never encountered before. The home.php, which is the main page of my site, cannot be viewed. And there is nothing in error log. After there is an inclusion of a php page (a class that is a part of my API). Till that line the holl HTML (javascript and css inclusions) are echoed from php and successfully rendered in browser, but after the php kind of stops suddenly. When I delete that line(php page inclusion line), website works, but before, this inclusion didn't make any problem for me. I removed the code parts which after them I encountered this problem, but nothing works.
I am using Apache2, PHP5 with Ubuntu 11.10.
Any help will be appreciated, thanks in advance.
My first hints would be to check the following:
In your script set ini_set('display_errors', '1'); and error_reporting(E_ALL); to display all errors.
Turn on the php error log and check it for errors.
run php -l home.php on the command line to check your php file for syntax errors.
Check Apache's error log, sometimes error messages go there.
If nothing helps use a debbugger like XDebug to see where the script terminates, or alternative insert statements like die("here"); and move them around in your code, to see which parts of your scripts are passed.
Greetings and good luck.

PHP Header("Location:...") works on one computer and not another

So I just got a nasty surprise when I deployed some code I thought I'd tested. It would seem there must be some difference between my test machine and my server. The exact same code, featuring a header redirect, worked perfectly on my test machine and not at all on the server. The redirect on the server simply didn't happen, leaving a blank page as a result.
The header is called somewhere in the middle of the script - but nothing will have been output yet. It doesn't output anything until the very end of the script. Long after everything else is run. It buffers everything.
Both server and test machine are running the same PhP version, the same Apache version. Is there something in the configuration files that would allow the header to happen for one and not in the other? Is there something else going on here that would cause it to fail?
EDIT:
Here's the line that sets the header:
public function setRedirect($url) {
header('Location: '.$url);
}
And here's the code that calls that:
$url = new URL('index');
$this->layout->setRedirect($url->toString());
Where URL::toString() always generates a fully qualified domain name, in this case: http://domain/index.php?action=index
I checked both Php and Apache error logs. Nada.
Probably there was some whitespace or other form of output before the header call.
This is only work if you the ini setting output-buffering is on (or if you explicitly start output buffering, but in that case, the redirect should work in both computers).
You can confirm this by turning on error reporting.
Use Fiddler or some other client-side tool to check your headers. Determine that the Location: header is actually being sent. Also, some browsers are picky in the order that headers need to be sent.
I think the most likely explanation is that an error is causing the script to exit on your server, and you have display errors turned off (hence the blank screen). I would suggest checking the Apache error long on your server to see if PHP is putting something in there.
Otherwise you could use a browser extension like LiveHTTPHeaders (for Firefox) to see if the location header is being sent at all, or try debugging the script to see if it's even getting as far as that header call.
I think your server puts some script in your pages to track visitors and give you traffic stats or for a similar purpose. Ideally, you should get an error for this but may be your server has error reporting disabled which gives you a blank page.
I suggest you to run a script with a syntax error and check weather your server has error reporting disabled.

Categories