Timothy Wilson

PHP coder for a games company working on websites, API and more.

Very simple fake date generator for when creating realistic format data for database load testing, unit testing, or front end UI template style stressing.

It’s also been used as part of generating realistic family relationship management trees, for this reason it has an age backwards compatibility generator function that’ll randomly place a date within the range of an age.

https://github.com/Altrozero/fakeDate

I’ve pretty much wasted 10 hours over the past 2 work days fretting over a PHP script taking over a second to execute on the server and the culprit in all this was Apache. Let me explain, the script was fairly complex and part of large CMS website with heavily integrated user/community systems and a few public and private APIs for external projects. Basically there are a lot of things that could cause problems, however, after separating each system, which was a tangled messed, I traced the bottle neck of the 1 second execution time down to the template object, used on virtually every page.

Loading just HTML from cached template files using either of PHP functions include/require/readfile accounted for roughly 99% of the page load time. The larger the HTML file the larger the lag. In the end rather than printing the text straight away I decided to try and load the files information in to PHP then echo it. Loading of the HTML in to PHP took roughly 3ms where as echoing took roughly 0.9 seconds.

The closer the user was to the server in the world the less time it took to echo the statement. Obviously PHP was getting hung up with waiting for the user to receive the data. Somehow we managed to stumble across the apache variable SendBufferSize which was set surprisingly low (only 4kbs). The PHP script had to wait for this buffer to clear before it could send more information to apache to then send to the user.

So far our solution has been to set SendBufferSize to 128kb in the hope that no HTML is going to get anywhere close to this size. If it does we will have to deal with that later. What I personally cannot work out is why this is set so low by default? Ram is never going to get swamped on a server by setting this buffer higher unless your executing 10k+ scripts all at once. Which means the PHP script is over in 20ms rather than a second.

So if your scripts are lagging due to the include function or because of an echo statement then your sending too much data to apache before it can clear it. Set SendBufferSize higher and you’ll be laughing.

Or course I should point out that this won’t really get anything to the users browers faster, but at least you’ll know that rather than constantly tweaking PHP scripts for speed, you’ll be better looking at your networking, server configuration and the amount of HTML your sending to the user.

Hope this helped.

One of the issue I ran in to this week was the default upload size of PHPMyAdmin for Importing SQL in to a database. The .sql file was exported from a DB and all of the queries were none DB specific. Rather than trying to edit a few hundred very long queries to be able to execute from MySql command line it was a lot easier to increase these limits in PHPMyAdmin.

My file in question was a little under 100MB, even after hard compression to .gz I could not get the file to under 30MB. A little muddling around show that it appeared to be limits imposed in the PHP.ini file rather than by PHPMyAdmin, there were 2 variables that needed updating.

 php |  copy code |? 
1
post_max_size = 8M

And

 php |  copy code |? 
1
upload_max_filesize = 10M

By changing these values both to 100M PHPMyAdmin accepted and executed the file with out an issue. You may also need to change the below variables if your file needs extracting by PHPMyAdmin

 php |  copy code |? 
1
memory_limit = 128M

Remember after a change to the php.ini file you will need to restart the process for them to take affect. Also I wouldn’t advise having these settings on a public folder or file. Rather than editing the PHP.ini file you may wish to use htaccess to change these variables without restarting the server.

 php |  copy code |? 
1
php_value  upload_max_filesize  100M
2
3
php_value  post_max_size  100M
4
5
php_value  memory_limit  128M 

 

It’s handy to force users to view your website from only one domain, e.g www.example.com over example.com especially when dealing with sessions which are normally not domain specific. Rather than messing with server & cookie settings to try and get sessions to work across domain it is a lot easier to just force them on to one or the other.

It is also important to maintain the original uri they are requesting, in fact this is very easy to do. Take the below code which forces users to www.example.com over example.com

1
define('VALID_DOMAIN', 'www.example.com');
2
3
if(defined(VALID_DOMAIN) && VALID_DOMAIN !== null && $_SERVER['SERVER_NAME'] != VALID_DOMAIN) {
4
      header('location: http://'.VALID_DOMAIN.$_SERVER['REQUEST_URI']);
5
      exit;
6
}

Simple and effective.

Sometimes its handy to know if a website is up or not from code. E.G In a list of websites you are managing. I put this function together to tell me if the site is online or not.

 php |  copy code |? 
01
function site_exists($url) {
02
 if (!empty($url) && ($con = curl_init($url)) !== false) { //start the connection
03
 //Setup connection operators
04
 curl_setopt($con, CURLOPT_TIMEOUT, 10);
05
 curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
06
 
07
 if (($data = curl_exec($con)) !== false && ($code = curl_getinfo($con, CURLINFO_HTTP_CODE)) !== false) {
08
 return true;
09
 }
10
 
11
 curl_close();
12
 }
13
 
14
 return false;
15
 }

by changing line 8 to the below code you can get the http status code for a more detailed report.

8
return $code;

If you are checking a lot of website you may want to change line 4, the current setting tells cURL to wait 10 seconds before failing. However, normally 2-3 seconds is acceptable. If you are checking hundreds of website you may want to turn off (Not available in PHP safe mode) php maximum execution time with set_time_limit ( int $seconds ) or infact turn this on. 

Tag System Example

Example of the tags system

A very simple way of randomizing a tag page to make it look a little more interesting than normal. If you have a little more time and your tag system supports it you can make it so that the popularity of the tag affects it’s size rather than it being random. So that the tags that are used most often are the largest, but that is a task for a rainy day.

 php |  copy code |? 
01
02
03
$tag_sizes = Array('10','15','20');
04
$tag_weights = Array('normal', 'bold');
05
$tag_colors = Array('000000', '660000', '006600', '000066', '666600', '006666', '660066');
06
07
?>
08
09

10
 
11

Random password generation in PHP is a fairly simple, however one of the problems I often find is I’m left with a password made up entirely of numbers or letters with no special characters what so ever. Take for example the following function;

01
02
03
/*
04
* Altroleet.com 2011
05
*/
06
07
// Generate a password with a guarantee of lowercase, uppercase, numbers and special characters
08
function generate_password($len = 8) {
09
 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890!"$%^&*()_+-=[]{}:;@#~<>?';
10
 $password = '';
11
 
12
 for ($i = 0; $i < $len; $i++)
13
 $password .= $chars[rand(0, strlen($chars) - 1)];
14
 
15
 return $password;
16
}
17
18
echo generate_password();
19
20
echo '

';
21
22
echo generate_password(3);
23
24
echo '

';
25
26
echo generate_password(16);
27
28
?>

On most occasions it will return a string made up of a wide variety of characters, however you aren’t guaranteed a secure password. While unlikely it is possible to generate normal dictionary hackable words or strings made up of entirely one character. You can solve this be forcing the function to select characters from four different sources; upper case, lower case, numbers and special characters. E.G

01
02
03
/*
04
* Altroleet.com 2011
05
*/
06
07
// Generate a password with a guarantee of lowercase, uppercase, numbers and special characters
08
function generate_password($len = 8) {
09
 $chars = Array('abcdefghijklmnopqrstuvwxyz',
10
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
11
    '01234567890',
12
    '!"$%^&*()_+-=[]{}:;@#~<>?');
13
 $len = (($len < count($chars)) ? count($chars) : $len);
14
 $per_sec = floor($len / count($chars));
15
16
 $password = '';
17
 for ($i = 0, $max = count($chars); $i < $max; $i++) {
18
 for ($j = 0, $amount = (($i + 1 < $max) ? $per_sec : ($len - ($per_sec * (count($chars) - 1)))); $j < $amount; $j++) {
19
 if (strlen($password) == 0)
20
 $password = $chars[$i][rand(0, strlen($chars[$i]) - 1)];
21
 else
22
 $password = substr($password, 0, ($split_point = rand(0, strlen($password)))).$chars[$i][rand(0, strlen($chars[$i]) - 1)].substr($password, $split_point, strlen($password) - $split_point);
23
 }
24
 }
25
 
26
 return $password;
27
}
28
29
echo generate_password();
30
31
echo '

';
32
33
echo generate_password(3);
34
35
echo '

';
36
37
echo generate_password(16);
38
39
?>

This code is slower than the first function, however you get more reliable results and unless you are batch resetting a large amount of passwords you are unlikely to notice a difference. Especially when the auto generate password is one of the least used functions in a user system.

Basic flip clock images that were made for a really old university project, I find that they still come in handy for websites and games. Feel free to use and modify;

   

Download Zip File [contains 21 files]

© 2014 Altroleet Suffusion theme by Sayontan Sinha