PHP
PHP, source code, scripts, developing, tips & hints
Calculate movie hash
0PHP code to calculate the movie Hash.
Uses size + 64bit chksum of the first and last 64k (even if they overlap because the file is smaller than 128k)
Usage:
echo OpenSubtitlesHash("/home/chema/Desktop/Monty Pythons Life of Brian.avi");
Ocaku – classifieds community search engine
5Last months I’ve been pretty busy with my studies and I needed to do a final project for my Master in Software Engineering.
This project is Ocaku, that was presented to the jury of the master 1 week a go. I’ve got an score of 9 over 10 possible points, not bad at all :D
Now I need to promote my work and I will love any kind of help spreading it ;)
What is Ocaku?
Ocaku.com is the finest community for classifieds sites all over the world.
With the union of all this pages we have a great multi lingual search engine that allows us to search everywhere in the world.
Ocaku is free of charge to use and will try to survive using advertisements, do not hesitate to contact us to advertise in our pages.
API for usage of Ocaku in your site.
Presentation (spanish sorry):
(more…)
PHP Geotarget IP
2Just a function to get geographical info about an IP.
I use this actually to stop spammers from certain countries…I know a bit to radical, but after using IP (of banned lists), captchas, akismet, banned names etc…I don’t know what else to do :S. (it seems is already working no spam in 1 week)
Usage:
$geoip=geoIP('81.60.189.183');//using an existing IP $geoip=geoIP();//will use clients IP print_r($geoip); echo $geoip['country']; echo $geoip['countryAb']; echo $geoip['city']; if ($geoip['country']=='india') die('country not allowed');//to ban a country
The code:
(more…)
Prevent code injection in PHP – Updated
7Long time a go I wrote this article on how to prevent code injection in PHP, but is kind of old and uses the deprecated method “eregi”.
I rewrited the function and now looks like this, the hacker defense for php:
function clean($var){//request string cleaner if(get_magic_quotes_gpc()) $var=stripslashes($var); //clean $var=mysql_real_escape_string($var); //clean return strip_tags($var, '<b><a>');//returning clean var } function hackerDefense(){//thanks to Allen Sanford // begin hacker defense foreach ($_POST as &$postvalue){ //checking posts $postvalue = clean($postvalue);//cleaning the value } } // end hacker defense </a></b>
OLD way don’t use it!!!
(more…)
Search suggestions in AJAX + PHP
2Simple search suggestions for PHP using ajax.
HTML:
<input type="text" name="s" id="s" value="" onkeyup="showSugestions();" /> <div id="livesearch"></div>
Cache expire Headers in PHP
0Really useful way to take stress to you server is sending to the browser proper header establishing when your content will expire and until when needs to be cached.
Imagine how many less request you will have since the browser have that info already cached!
How to do it in PHP, just put this code at the top of your code, before anything it’s output.
$expire=60*60*24*1;// seconds, minutes, hours, days header('Pragma: public'); header('Cache-Control: maxage='.$expire); header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expire) . ' GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
Just be aware of the time you put, if your site needs to have the latest fresh news it may be not be agood Idea to use more than few minutes expire.
(more…)
Light PHP Frameworks
4I have this thing with light and fast things….I love performance!
The thing is that I use my own framework to develop PHP, but what is a framework exactly? when is a framework or just a set of utils?
My point is that I don’t like frameworks such as Zend or others that are heavy, loaded with stuff I will never use, of course this can be great for backends where there’s lack of performance since there’s not many users…I’m talking about the scenario of front end.
In front end architecture the king is the MVC design pattern. Why not just create a bunch of utilities to have an easy fast MVC and then add the classes or functions that you need?
For this things can be great any of this solutions:
Fat Free: Light MVC framework with many extra functionality in just few KB, take a look, really good documentation. Just I don’t like too much the templating feature since PHP it’s actually a template engine it self, don’t forget that…
Miminal PHP MVC: I think the name it’s enough to know what’s about. This is more or less what I’m using with more functionality that I use in my projects, but the concept it self is great, MINIMAL.
The no-framework PHP MVC framework: This is more an article written by Rasmus Lerdorf (creator of PHP) regarding frameworks. Really interesting reading.
I still don’t have my framework clean enough to make it public but I just would like to share something really simple but at the same time really useful as it is the folder structure for my projects:
(more…)
Generating a unique Key
2I need a unique Key to use to identify an object.
I use this in PHP:
$key = md5(uniqid(mt_rand(), false));
This should be unique, but can you believe that will never be repeated? I don’t trust it…
Just check it in your DB just in case:
(more…)
Check if URL exists and is Online – PHP
8Imagine you need to check if a site is online or not, seems pretty easy since there’s plenty of tools to check this, but this can be a huge bottleneck for your app.
I have tried different ways with sockets, header and curl in order to know which one is the fastest option.
Tested code done to the site yahoo.com (10 attempts each and we keep best result as time):
(more…)
Optimize your DB from PHP
0This is a functionality I’ve been using in Open Classifieds since the 1.6.1. version.
It currently works for MySam working as follows:
- If the table has deleted or split rows, repair the table.
- If the index pages are not sorted, sort them.
- If the table’s statistics are not up to date, update them.
And for InnoDB, rebuilds the table to update index statistics and free unused space.
And if we want to do this in PHP for all the tables in your DB simply do this:
$result = mysql_query('SHOW TABLE STATUS FROM '. DB_NAME); while ($row = mysql_fetch_array($result)) $tables[]=$row[0]; $tables=implode(", ",$tables); mysql_query('OPTIMIZE TABLE '.$tables); echo "<h3>Optimized all the tables found in the database: $tables</h3>";
That’s all! simple yet effective ;)