php delete cookies | how to update debian | facebook api php post | stmt get_result false execute error code


How to Create, Read, Update and Delete a Cookie with PHP or Javascript

http is a stateless protocol. A way to remember information from page to page is to use cookies. According to Wikipedia:

A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is a small piece of data sent from a website and stored in a user's web browser while the user is browsing that website. Every time the user loads the website, the browser sends the cookie back to the server to notify the website of the user's previous activity. Cookies were designed to be a reliable mechanism for websites to remember stateful information (such as items in a shopping cart) or to record the user's browsing activity (including clicking particular buttons, logging in, or recording which pages were visited by the user as far back as months or years ago).

In this tutorial you will find information about creating and managing cookies with PHP or Javascript. Demo is available in each case.

PHP and Javascript Internationalization using Gettext and Poedit

The term "localization" is used as a synonym of "internationalization", which is the most proper term. Sometimes, internationalization is shortened to "i18N" (meaning "i - eighteen letters -n").

gettext is a GNU project for software application resources internationalization. It has been ported to many programming languages. One of them is php. php gettext can be used as php extension or as cgi (here the first case is mentioned).

Terms to be translated inside php source code handled as following:

echo gettext("text to be translated");

Usually, the english version (text to be translated) will be the "key" for the rest translations. If language translation in not exist, the "key" term will be used (text to be translated).

The most important gettext feature is that extracts terms parsing source code files, and updates them in case of modification (without change the already translated terms).

Additionally, as there is no web application without javascript, a very interesting issue is the translation of application resources inside javascript code. Several solutions are available (see below).

How and When To Enable MySQL Logs

If you are a web developer, you need to refer to various log files, in order to debug your application or improve its performance. Concerning the famous MySQL database server (or MariaDB server), you need to refer to the following log files:

  • The Error Log. It contains information about errors that occur while the server is running (also server start and stop)
  • The General Query Log. This is a general record of what mysqld is doing (connect, disconnect, queries etc)
  • The Slow Query Log. Ιt consists of "slow" SQL statements (as indicated by its name).

This article does not refer to The Binary Log. This requires very high standards server hardware and is useful only in special cases (e.g. replication, master - slaves setup, certain data recovery operations). Οtherwise, it is just a "performance killer".

The official documentation about MySQL logs is available here.

Dynamically Bind Params in Prepared Statements with MySQLi

Prepared statements must be used in application development with databases. There are two main reasons:

  • performance: parameterized queries with prepared statements reduces the database load, reusing access plans that were already prepared
  • security: using prepared statements, there is no need to escape user input. Binding parameters, the driver does the work for you. This is an ideal method to avoid SQL Injection attacks.

Fortunately, it is possible to use prepared statements with MySQL and PHP using MySQLi extension. Old MySQL extension does not support prepared statements. Moreover, it is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future.

More information about MySQLi you can find in this post.

PDO (PHP Data Objects) also supports prepared statements (and even better than MySQLi).

A Workaround to use Secondary icon to jQuery-UI buttons with jQuery-UI-bootstrap

jQuery UI Bootstrap is a project which brings the beauty of Twitter Bootstrap to jQuery UI widgets, as is mentioned by Addy Osmani, the creator of jQuery-UI-bootstrap.

jQuery-UI offers a lot of themes. You can test different themes (even third party themes as jQuery-UI-bootstrap or Wijmo) with jui_theme_switch, a plugin, I have developed, here). I guess you will agree that jQuery-UI-bootstrap is the most beautiful :-)

Actually, jQuery-UI-bootstrap is NOT ONLY a third party theme for jQuery-UI. Moreover, with jQuery-UI-bootstrap you can also use (most) of Twitter Bootstrap side by wide with jQuery-UI widgets without components breaking visually.

In the following example I use jQuery-UI-bootstrap just as a jQuery-UI theme. Twitter Bootstrap is not used. As you can see, I had problems using secondary icon (the right-side icon) on jQuery-UI buttons. The provided solution is just a workaround and probably not a permanent solution. This example uses jQuery 1.10.2 and jQuery-UI 1.10.3

How to Auto Post on Facebook with PHP

In a previous post I wrote about post automation on Twitter using PHP. The whole procedure is similar. You have to create a Facebook app and you need a PHP SDK to communicate with Facebook API.

However, the Facebook post automation is much more complicated than the relative process for Twitter. This is not a surprise, as Facebook itself is much more complicated than Twitter. For example, except of a Facebook Personal Profile, you may have Fan Pages or Business Pages.

A significant difference is that Twitter Access Tokens will never expire (according to current Twitter policy), while Facebook Access Tokens will expire in 60 days.

How to Auto Post on Twitter with PHP

Manually post to social networks is probably better than to use an automated method. But, if you post many new topics every day, automated post is practically inevitable.

Twitter, as almost any social network, brings an Application Programming Interface, Twitter API, in order to communicate with it programmatically. You cannot use Twitter API directly (using your username and password), but you have to create a Twitter App. Creating a Twitter App is easy (see below). After you create your app, you get ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret and you have to use them as credentials to communicate using Twitter API.

Additionally, you need a library for your programming language (PHP in my case) to easily use Twitter API. There are many libraries available. In this tutorial I use Codebird-php.

A Simple PHP Class to Create RSS Feed

RSS (Really Simple Syndication) is the use of an XML document in order to share content across the Internet. An RSS document, aka "feed" includes full or summarized text, plus metadata such as publishing dates and authorship.

This post covers RSS version 2.0. In simple words, an rss feed consists of a CHANNEL and channel ITEMS. You can find RSS specifications at RSS 2.0 at Harvard Law or RSS Advisory Board. You can extend your feed structure using XML namespaces. Details here.

Nowadays, most website owners are sharing their site content using social media. So, many Facebook, Twitter, Google plus or other social media users are using the stream of their favorite social network to remain aware of website changes. Nevertheless, RSS remains the first choice for content syndication.

How to Create a jquery-ui Autocomplete Step by Step

Autocomplete is one of the most important parts of modern rich web interface. Actually, you can create an autocomplete, using an AJAX call to build a list (server side) and afterwards to display the list using javascript (usually below autocomplete input). However, there are many tools to create it the easy way.

The most robust and efficient tool to create an autocomplete is jquery and jquery-ui. Autocomplete widget of jquery-ui is available here.

Of course, there are many other open source or commercial tools available. Twitter typeahead is probably the most important open source alternative.

Upgrade Debian 6 Squeeze to 7 Wheezy

One of the advantages of Debian operating system is the ability to live update a production server from current to next release. Under normal circumstances you do not have to re-setup the system. So you avoid downtime, something critical for server systems.

There are possibilities this procedure to fail, so you must take a full system backup before.

The same procedure can be applied to desktop machines. There is more complexity in this case, due to graphical desktop environment.

Here I describe a successful upgrade of a Debian LAMP server system from Squeeze to Wheezy.

Previous posts Next posts