RSSAll Entries in the "PHP" Category

Timestamp in PHP

In this tutorial I will give you example how to add timestamp to file name in PHP .
This is useful when you create similar files like database backups, regular reports, or whatever you are saving in PHP with timestamp in the file name.

Bellow is an example that creates one CSV file with timestamp in the file name:

The part that sets the timestamp in PHP is:

The file created using this method will be like this: testFile.04-10-2011.csv – certainly with different date.
You can modify the PHP date function and use different formats. For example if you need only the current time for timestamp use:

The file name with this timestamp will be like: testFile.06:18:08.csv

For full timestamp including year-month-day-hour-minute-seconds change the code to:

My file appeared with timestamp: testFile.2011-10-04_06:18:08.csv

Certainly you can use different separators for the timestamp, also there are more options available which can be found at PHP date function page.


Incorrect integer value WATCHDOG_ERR

This tutorial is related to issue with Drupal after upgrade, module change or module activation. The error is similar to this one:

“PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ‘WATCHDOG_ERR’ for column ‘severity’ at row 1: INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => file [:db_insert_placeholder_2] => The specified file %file could not be copied to %destination. [:db_insert_placeholder_3] => a:2:{s:5:”%file”;s:22:”temporary://filefCLBmE”;s:12:”%destination”;s:56:”public://ctools/css/818a9e4e3a8a0543c79825df2bf73444.css”;} [:db_insert_placeholder_4] => WATCHDOG_ERR [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => [:db_insert_placeholder_9] => 1317299034 ) in dblog_watchdog() (line 157 of /var/www/vhosts/”

After some search and investigation the issue appeared to be in a file called ‘’ located inside ‘includes’ folder. All the rest variables were WATCHDOG_ERROR, but there was one WATCHDOG_ERR which seams to be not declared anywhere.

The output was :

One of the watchdog lines is not like the rest, and instead of WATCHDOG_ERROR is WATCHDOG_ERR:

So use your preferred editor and just change the variable to WATCHDOG_ERROR and save the file.
Then the issue should be resolved.


PHP relocation error

This will be quick tutorial about how to fix PHP relocation error like this one: php: symbol zlibVersion , version libmysqlclient_16 not defined in in file with link time reference. Instead php: relocation error: php: symbol zlibVersion the error could be php: relocation error: php: symbol crc32.

This is an example and the issue could happen with any shared library not only with .

As per the error message the library version is not matching so we have to check the shared library dependencies. This can be done with tool called ‘ldd’ which prints the shared library dependencies. In this case we will check PHP for “libmysqlclient” so the command should be like this:

Or if for some reason the PHP binary is located elsewhere:

The output should be similar to this one:

Let’s check the file:

In my case this is a symbolic link to the library in the same folder:

Now let’s check if this file exist in another place.

My output is:

So there are two libraries, and now we have to check where is the missing symbol – for example zlibVersion. For that will use ‘nm’ command lists symbols from object files. Let’s try the current linked file:

The output is:

So it seams there are no symbols in this library.

With the other file:

The output is:

Which seams better.

Now I will just remote the current link:

And will create a new one with the library that contains the symbol:

This should resolve the case, and the php: symbol zlibVersion , version libmysqlclient_16 not defined in in file with link time reference error was solved.


Host multiple domains on one hosting – cheating the hosting companies.

I barely remember my first site and how it was created, but I still remember the filling when I saw the price for the webhosting I was searching for. Then I was a student and if I wanted to see my website live, I had to pay one third of my monthly budget. That was before more than ten years and the webhosting was pretty expensive than now.

Nowadays the hosting prices are way to low, but if you are familiar with webhosting and understand the services that some hosting companies provide – you probably know that even price of couple of bugs is too much for what you actually get. I am not going to fail into detailed discussion about hosting companies, plans etc.- so if you are at the point where you do not have much to spend on webhosting, and in the same time you want to start building websites and host them cheep you may use the method described bellow.

Lets consider the following case:
You have bought webhosting which include something like this:
One domain hosted, 160GB drive space, MySQL database, PHP, any-other-marketing-bla-bla . (For Windows hosting users, please read Redirect domain alias to a particular folder in Windows)

Certainly the main point here is that you have pretty much space, PHP and MySQL which allows you to do almost everything, and .. just one domain hosted. Here I have to say that the whole website is not more than 600MB and if I decide to host it on such hosting, the rest of the 160GB space will remain unused. You may decide to use the rest of the space as a storage, but in most cases it just remain like that.

With the PHP script bellow you can host multiple domains on one hosting even it the hosting company allows only one domain to be hosted, which will allow you to use the rest of the hosting space for different websites.

Before that I have to mention that all domain you want hosted in this hosting should be set like CNAME for the main domain. The reason is that when a domain is set as a A CNAME record or Canonical Name record in the Domain Name System (DNS) – this specifies that the domain name is an alias of the other domain. This is rather simple and can be done in with the company where the domain is registered for free. (If not try GoDaddy ;)

Once a domain is set as CNAME it will do exactly the same as the first domain. Here comes my script which basically check as which domain the browser request is done and redirects to a particular folder where the according domain files are set. The script should be in the main index.php file, otherwise it will not be opened and will not redirect accordingly.

Ok, it may looks confusing, but what is actually happening is when someone lands on your hosting, his browser opens the main index file. The script checks ‘HTTP_HOST’ global PHP variable which contains actually via which domain the server was reached and then redirects to its index (you can see it in the example as index2.php and index1.php ), or to a particular folder. If there is no match the visitor’s browser will stay in the same file and will continue with the main domain code.

Well that’s it … and yes, it is legal to host multiple domains on one hosting using this way!

Error while connect to network services in PHP

Error when you connect to network service is another runtime error in PHP.
When you connect other machines using a network we can imagine how the other hardware fails. Or not just the hardware, there are a lot of devices and services that are not under your control. That is why you really need to carefully check the return value of functions that attempt to interact with a network service.

Let me give one example – if we call a function like that:

Call to undefined function and wrong parameters in function.

In this tutorial we will discuss runtime errors in PHP caused by functions.
If your script call to undefined function or the function does exist, but the number of parameters is wrong you will receive an error.

It is easy to call a function that does not exist.
You can call function that does not exist in the current script but might exist elsewhere. If your code contains a call to a nonexistent function, such as wrong_name_function(); or missing_function(); – you will receive an error message from the parser similar to this:

Fatal error: Call to undefined function: wrong_name_function()
in /home/public_html/file.php on line 3

Also, if you call a function that exists but call it with an incorrect number of parameters,
you will receive a warning.

The function two_strings() requires two strings – for example bottle and size of the bottle ‘“ and you run the function just like this:

Runtime errors in PHP

In a previous tutorial we discussed PHP syntax errors description , and now we will talk about Runtime errors in PHP.

Sometimes Runtime errors are harder to detect. Runtime errors are not caused only by the contents of your script. They can rely on interactions between your scripts and other events or conditions.
For example the next construction:

PHP syntax errors description

Every programming language has number of rules, called syntax, which must be followed in the constructions in order to be valid. If a construction is not following these rules, then we said that there is a syntax error. Syntax errors often are called parser errors, when we talk about languages like PHP. Other errors could be compiler errors like in C or Java.

If we do not follow the rules when we speak or write to other people, probably they will understand, but when we write a PHP script most probably there will be an error.

If a script is not written correctly according to the rules, the PHP parser will not be able to process a part or the whole script. Within many other rules PHP syntax demands all the constructions to end with semicolon, string to be enclosed in quotes and the parameters to be separated with comas and enclosed within brackets.

The PHP errors are very usefully because they are providing us with information, what is wrong with the script.

For example :

PHP Parse error: syntax error, unexpected T_ECHO in /home//public_html/test/date.php on line 3‘ will be generated by a script where you have forgotten to add semicolon.

PHP script sending with external SMTP authentication

In my work I often have clients calling with the following statement: “My PHP script which is sending with external SMTP authentication is not working. I checked all my code and it appears it is not working because of your (shared hosting) server.”

Well since it is rather not possible all the customer\”s scripts to be checked so I need something to check from the client”s side.

In such case the script provided here is very useful.


Check MySQL database connection using PHP test script

As a tech I’ve been in situations when a database is not working or the connection to it is interrupted.

This tutorial will show you how can be done MySQL database connection check using PHP script.