Application works fine on localhost but fails on web server

If you ever run into situation, that your application works fine on localhost (or one of webhosting) but fails completely on webhost (or another server), then first thing, you should look for, is to check, if you don’t have an extra XHTML line in on of your files that you’re loading using include or require — meaining, that your loading and PHP-parsing these files. Because problem doesn’t exist, if you’re loading this files as simple text files, using file_get_contents for example.

Situation, I’m talking about most times causes a PHP syntax error saying, that there is an unexcepted T_STRING in first line of one of the files used in an application. If you look to this file, you may find following line as first one:

<?xml version="1.0" encoding="iso-8859-2"?>

This usually happens, when environment, where your application works, has PHP short_tags enabled in configuration, while on failing webserver most probably this config option is disabled. PHP tries to parse it and fails. Since this is XHTML-related problem, most times it comes around template files or similar.

There are many questions here, but only one answer.

I don’t know, why some editors adds this line, when creating new XHTML file, while others doesn’t add them. I don’t know, why PHP parses this line correctly (doesn’t come out with any error), when short tags are enabled and while fails, when they’re disabled — in both cases this line seems to be unparsable, as it does not contains valid PHP code. Finally I don’t know, if removing this line will or won’t make your file / app valid and go through XHTML validators without any problems — i.e. if it is or isn’t necessary as a part of valid XHTML file. But the only answer, I know to all these conserns, is — remove this line.

That’s it. I tested layout file containing this line, being loaded and parsed via require, on both simple PHP app and large framework-based (Yii of course) project. When short tags were disabled, both apps failed. And removing this line was the only remedium in this case.

BTW: You can enable or disable short tags, no matter what your server configuration is, by adding:

php_value short_tags off

to .htaccess file placed in root folder of your PHP application.

That’s all I know for now. Go ahead, and write in comments, if you have any other solution to described problem or if you can add anything here.

Simple page rendering class

I have created a simple CPage class, which I use in my non-Yii projects for easily rendering views, templates and whole pages using object-oriented approach. This class can render given contents into given view and embeded it in a selected layout file. These are very limited versions of Yii framework’s view and layout rendering mechanisms. I have also added a simple alerting system, this time taken from Twitter Bootstrap.

Read More “Simple page rendering class”

Simple function for drawing HTML dropdown list from various sources

If you’re still lurking in the darkness of writing code with pure PHP and not using any framework then in this post you can find some nifty function, useful for drawing a HTML list boxes.

Basically it tries to render (draw) a <select> element basing on input data provided in $source variable. It can be either a list (key=>value) or string, where values and items’ texts are separated with delimiter. Such string can be provided directly or read from file. In this case you only provide path to a file (source of data).

Read More “Simple function for drawing HTML dropdown list from various sources”

JS and PHP or Yii photo manipulation libraries

I was faced with a problem of picking a good photo manipulation library. Either client-side or server-side. Both for working with my newest Yii project. This article is a summary of my quick research in this field.

All JavaScript libraries works on Canvas, so require HTML5 and Canvas-enabled browsers. Most modern browsers supports both of them in newest versions.

The only PHP image manipulation library works mostly on uploaded image.

Read More “JS and PHP or Yii photo manipulation libraries”

When PHP code should really be treated as unsafe

Yesterday I took a part in interview for PHP developer position. My interview task was to solve fifteen questions in quite simple test. One of the questions was to decide if given code sample be treated as unsafe and in which conditions.

I gave a wrong (as it turned out) answer and the argumentation from the intervieerw was quite surprising for me in the first time. Finally I realized my mistake.

Read More “When PHP code should really be treated as unsafe”

Alternative module configuration that does not affect main configuration

In Yii main application is actually a module (core one) so each module configuration actually shares nearly everything what you can put to main application’s configuration file.

Thus, you can configure any Yii module, just as you would do with your main application. The only difference is that you don’t use external configuration file, but CModule::configure() function instead.

Read More “Alternative module configuration that does not affect main configuration”

Passing current or last page full URL as a part of URL

By using five separate PHP functions, in correct order, you can easily pass (code and decode) entire page’s URL as a parameter of any other script call, redirect etc. And be sure that it will be read (decoded) correctly, no matter, how long your URL is or what kind of characters it is using.

You can encode any URL (current page, last visited page or any otherwise important) and pass it in another URL in the way that your user won’t notice that you’re actually passing an URL.

Presented solution can potentially be used as URL shortening service. Generated URLs aren’t that short (as maybe expected), but for really long URLs it does provides some shortening.

Read More “Passing current or last page full URL as a part of URL”

CMarkdown usage examples

You can use CMarkdown class as typical Yii widget:

<?php $this->beginWidget('CMarkdown'); ?>
_Markdown_ *example*
<?php $this->endWidget(); ?>

or directly as a function:

$text = '_Markdown_ *example*';
$md = new CMarkdown;
echo $md->transform($text);

That’s pretty much everything, folks!

Too long URLs in Apache

It turned out that in current version of Apache you’re limited to 255 characters at most between each pair od slashes (after URL decoding is done). This short article discusses this issue and maybe useful to all developers, who are dealing with “beauty” (SEO-optimized) URLs, like for example URLs to blog posts containing entire title inside.

This is a rare situation (255 characters limit between slashes, not for the entire URL), but still worth exploring.

Read More “Too long URLs in Apache”

Things for which I trully hate PHP and its creators

PHP is the most popular programming language for development of webpages and web services. With addition of a good framework it can even produce a professional results. However, this does not change the fact, that it is also the most shitty programming language in the world, full of garbage and with number of stupidities beyond imagination. And professional programming (without a good framework) is impossible.

Even though this is a language of my choice (only after cleaning it from all the shit, by using a good framework), I decided to write this article, where I’m pointing out most annoying and stupid things, I ever found in PHP.

Notice, that I’m not talking here about things that some may see as wrong, while other will see as good. For example, I’m not talking about types auto-declaration and auto-change, which ones (mostly newbies) takes as ease of coding and others (mostly profs) as source of mess.

I’m talking here only about pure madness, confirmed piece of shit and horrible pain in the ass, this language is.

Please, take also into consideration that this text was written under strong emotional influence and thus it (intentionally!) contains a lot of harsh language and words or phrases that some readers may not accept.

Read More “Things for which I trully hate PHP and its creators”

screen: Force program to continue after logging-off from the console

So, there I was… needing to have any solution, that will allows me to write my very own port listener. Since the only language, in which I am handy is PHP, I was forced to be able to run PHP script without interruption, 24 hours a day, 7 days a week. That was a bit of challenge for me, given my quite very limited Linux knowledge, but — as they say — the only thing not possible in IT is to open an umbrella in your ass! :>

Read More “screen: Force program to continue after logging-off from the console”

Simple script for showing contents of a folder

Long long time ago in a galaxy far far away I wrote a very simple PHP script, that I usually put into index.php of a directory, which contents I’d like to print out to the browser. I’m using this solutions on all servers and hostings which has Apache-based directory listings disabled and thus are showing empty page or error screen, if folder does not contain index.php.

Here’s the code:

$dir = getcwd();

if (is_dir($dir))
{
    if ($dh = opendir($dir))
    {
        echo('Files in this folder:<ul>');

        while (($file = readdir($dh)) !== false)
        {
            if ($file != '.' && $file != '..' && $file != 'index.php')
            {
                echo('<li><a href="'.$file.'">'.$file.'</a></li>');
            }
        }
        closedir($dh);

        echo('');
    }
}

That’s pretty much everything, folks!

Using PHP and Oracle database

I was working on bigger project using PHP (Yii) and Oracle database (through PDO and Yii). This isn’t a very common combination and many developers thinks, that writing PHP application to use Oracle database is a pure madness, mostly due to many strange behaviors and nasty bugs, that are present in PDO driver for Oracle. I can actually confirm this. Following article contains links to my Yii forum discussions on various aspects of using Oracle with Yii and PHP. You may find it useful, if you’re struggling with any problem.

Read More “Using PHP and Oracle database”