Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Javascript crc32 that matches PHP crc32 and works well with unicode

Many programmers including me are using hashes to solve different problems, and when we are using hash function in web application we want client-side and server-sire values of hash for the same value to match. So, trying to solve this problem for javascript implementation of CRC32 algorithm and PHP implementation of the same algorithm I got this article and the code that I have included to the article.

Google Chrome session cookie expiration issue/feature - your personal data is insecure now!

Not long ago I was happy that modern and standards complaint browsers are gaining the market and hoped that web programming will become more and more happy and interesting job with HTML5 and three major web browsers compete with aproximately equal shares. But Google Chrome developers have not allowed me to be happy for a long time. They generating new bugs and naming them features. Some of that "features" making me really sad.

Read complete article: Google Chrome session cookie expiration issue/feature - your personal data is insecure now!

PHP MySQL incremental backup/restore implementation (script download)

I have published two small scripts that allows to create and restore data from mysql database filtered by date, so I can define this as incremental backup. Article describes the backup/restore scripts configuration. The scripts themselves can be downloaded from the GitHub, links are available at the end of the article.

Read complete article: PHP MySQL incremental backup/restore implementation (download the script)

Using MySQL PHP: select random row based on weight

Article describes algorithm of random row selection based on weight, so some rows will be selected more often than other. Article includes sample codes, so I hope will bу helpful to somebody.
Do not hesitate to leave comments if you have ideas how to improve the algorithm.

PHP MySQL prepared statement vs SQL statement

I should confess - I am already using MySQL prepared statements for some time, but very rarely and mainly because they can make certain scripts more readable. As many of us I have read a lot of articles about coolness of prepared statements and that I should use them because of security and speed improvements. Finally my сuriosity won and I decided to test prepared statements against ordinary SQL statements myself. Friendly speaking I was impressed by the results I got....

Read complete article: PHP MySQL prepared statement vs SQL statement

PHP 5.4 prominent updates. Like php file uploads & mysql query result

A few days ago I have looked through the changes that were applied to PHP in its last  release of PHP 5.4 and was impressed by them. I think that small version number change brought the changes that could be compared to the ones that added real OOP to PHP language. So, I decided to review briefly some of them. Hope somebody will find this article interesting....


PHP geocoding class with Google Maps API v3


All of us know this very useful service of Google Maps. And lot of programmers already triev Google Maps API v3. Somebody even met the problem with the limit of the geo coding requests per time period. In some case server-side scrip can help to solve the problem. If the number of the locations is less than 12.000 (monthly limit of requests per IP), you can do geocoding server side and save the results to the database. If your application requires more geocoding requests than you should implement some sort of client-side geo coding strategy. This will use IPs of your site visitors.
As usual I have prepared some code. Right now that is php class that implements server-side strategy of geocoding as a wrapper for the http requests to Google Maps service.

Read complete article: PHP geocoding with Google Maps API v3

3 good reasons not to use php template engines! Say NO to Smarty!

Friendly speaking I thought that I would never touch this topic,  but recently I had to apply changes and fixes to one project and was really surprised to see template engine that uses str_replace...

Resons for not using template engine
  1. PHP is a Hypertext Preprocessor and can be embedded into PHP. It is the main benefit of PHP. So, why one should refuse it?
  2. Speed! Really, even when you are using "cool template engines" (no names here) with caching they will be much slower than plain PHP. Especially if you are enabling Eacelerator & optimize your code.
  3. New language really? Each template engine uses its own language at least for iterations, so one should learn it. What is the reason? is it more functional than PHP?

Problems & solutions

I understand that design should be separated from the program logic - just do not put the logic into the template! I understand that developers want to have one main template with placeholders for modules & templates for modules. PHP can handle this too, and I am sure you know it.

Just some code for illustration. Very basic template handling class:

class Tpl{
	static public $Vars = array();
	
	static public function Add($var, $val, $context){
		self::$Vars[$context][$var] = $val;
	}

	static public function Out($file){
		extract(self::$Vars['core'], EXTR_OVERWRITE | EXTR_REFS);
		extract(self::$Vars[$file], EXTR_OVERWRITE | EXTR_REFS);
		require("./templates/{$file}.html");
		self::$Vars[$file] = array();
	}
	
	static public function Get(){
		extract(self::$Vars['core'], EXTR_OVERWRITE | EXTR_REFS);
		extract(self::$Vars[$file], EXTR_OVERWRITE | EXTR_REFS);
		ob_start();
		require("./templates/{$file}.html");
		self::$Vars[$file] = array();
		return ob_get_clean();
	}
}

Script code:

include("class.Tpl.php");

Tpl::Add("V1", "Hello", 'test');
Tpl::Add("V2", "World!", 'test');
Tpl::Out("test");

And template:

<html>
<head>
<title><?=$V1?> <?=$V2?></title>
</head>
<body>
Working<br/>
<?=$V1?> <?=$V2?>
</body>
</html>


Sure, in real projects I am using more complicated solution with HTML cache, controls and validators, but is is using PHP as a template language.