PHP and Web Technologies Freak
Here is the list of 43 short tips you can use for writing an optimized and more efficient PHP code:
1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
2. echo is faster than print.
3. Use echo’s multiple parameters [...]
I’m working on a security and trying to make searches in a multi-dimensional array
Here is the function;
<?php
function multi_array_search($needle, $haystack, $key, &$result, $searchlevel = 0) {
while(is_array($haystack) && isset($haystack[key($haystack)])) {
if($searchlevel == 0 && key($haystack) == $key && $haystack[$key] == $needle) {
$result = $haystack;
[...]
I just found a way to using wilcard with unlink in PHP.
Here is the scenario;
files might be:
120a-some_file1.jpg
210w-some_file1.jpg
some_file123.jpg
480h-some_file1.jpg
somefile.doc
if you want to delete;
120a-some_file1.jpg
200w-some_file1.jpg
480h-some_file1.jpg
you can use this code;
<?php
foreach( glob(’*-some_file1.jpg’) as $file ){
unlink($file);
}
?>
ClickHeat is a visual heatmap of clicks on a HTML page, showing hot and cold click zones.
Requirements
- on the browser’s client: Javascript (tested on Firefox 2.0, Internet Explorer 6 and 7, Konqueror…)
- on the server: either Linux or Windows (since ClickHeat 1.3 release), Apache or Lighttpd (other may work fine), PHP, the graphic library GD2 [...]
The failing in using the value of $_SERVER['REMOTE_ADDR'] is that if the visitor is using a proxy then you will get the proxy IP address and not the visitors real IP address.
This function works by going through any variables in the $_SERVER array that might exist that would contain information to do with IP addresses. [...]
It is sometimes a good idea to find out where users can come from. When PHP is run the $_SERVER superglobal is always available and if the user has clicked on a link and landed on your page then the HTTP_REFERER value will be set. You can retrieve it like this.
if ( isset($_SERVER[ HTTP_REFERER ]) [...]
This simple code example uses a combination of strrchr to find the last occurrence of a string and substr to return part of the string in order to find the file extension for a given filename. This is ideal if you want to quickly find a file extension.
$ext = substr(strrchr($fileName, ‘.’), 1);
This code can be [...]
There are three available cryptographic functions in PHP, these are md5(), sha1() and crc32(). All of the functions take a string and output a value that is encrypted and can’t be reversed to the original string. In fact the only way to get the original string back is to run a brute force algorithm which [...]
1
2
3
4
5
6
7
8
9
10
11
function ShortText($text) {
// display char lenght
$chars = 10;
$text = $text." ";
$text = substr($text,0,$chars);
[...]
Hi!
i made this function at 2006 but still using it’s very usefull!
image.php;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
function createimg () {
session_start();
ini_set("session.gc_maxlifetime","36000");
$fontnum=6; //count of fonts
$numbers = strtoupper(substr(rand(0,999999999999),-3));
$_SESSION["guv"] = $numbers;
$im = imagecreatetruecolor(125, 75);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 400, 200, $white);
for ($i=0;$i<strlen($numbers);$i++)
{
$font = "fonts/".rand(1,$fontnum).".TTF";
if ((rand(2,6)%2)) { $angel=rand(0, 30); } else { $angel=rand(330, 360); }
imagettfnumbers($im, 25, $angel, [...]