PHP 7's Null Coalescing Operator
can be replaced with :
| php | 0 comments | Monday, January 13, 2020 |
| php | 0 comments | Monday, January 13, 2020 |
| php, security | 0 comments | Friday, July 27, 2018 |
| php | 0 comments | Tuesday, March 06, 2018 |
If you want to check if a file exists publically on Google Cloud Storage,
| Google Cloud Storage, php | 0 comments | Saturday, December 09, 2017 |
Compress might not be the right word, as it doesn't really compress / zip the output HTML code, but combines all the HTML lines to a single line and removes trailing / leading whitespaces.
This has to be your index.php and am assuming you are routing all incoming page requests to index.php which will be handled by either rewrites in htaccess or direct query strings. For example, I would have either
RewriteRule ^about\/vision$ index.html?page=about-vision [QSA,L] or RewriteRule ^register$ index.php?module=RegisterThere is one gotcha in this. You can't use single line comments start with // in inline Javascript code in the HTML. Either place the JavaScript code outside of the HTML page, in separate .js page or use multi-line comments /* */
| php | 0 comments | Thursday, August 06, 2015 |
The WebP image format for the web is starting to get a lot of hype for its massive file compression. Chrome supports it since ver 9 and Opera since ver 11.10.
Image file sizes are reduced by more than 50% in WebPs when compared to JPGs.
Looking at Google Analytics on various sites, Chrome 9+ users constitute 20% - 25%. So providing one-fifth or one-fourth of the users with a much reduced page load is worth the extra effort.
1. Convert JPGs / PNGs to WebPs :
for i in *.jpg; do j=`echo "$i" | cut -d . -f 1`; convert -colorspace RGB "$i" "$i"; ~/libwebp/cwebp -q 75 "$i" -o "${j}.webp"; doneconvert is used to convert the colourspace from RGB to CMYK since as of now, webp encoder doesn't support CMYK colourspace.
2. Use 2 different CSS files for background images - one for standard jpg/png and another for webp. Chrome 9+ and Opera 11.10+ would be served webp.css while all others img.css.
get_browser is pretty useful for browser detection, but requires to set browscap in php.ini which is a PHP_INI_SYSTEM directive and can't be set in using ini_set(), .htaccess or user php.ini which rules out almost all shared hosting enviroments.
But interestingly, this is possible in WebFaction where I could set browscap in a user php.ini file which was parsed.
If you are getting something like ...
<b>Warning</b>: get_browser() [<a href='function.get-browser'>function.get-browser</a>]: browscap ini directive not set in <b>/path/script.php</b> on line <b>7</b>and if there is no way to set browscap, then you need to find an alternative browser detection like phpbrowscap.
I benchmarked this on a graphically heavy wordpress site, having 11 photographs in a slider on the homepage which takes a megabyte alone.
| Browser | Requests | Size | Time |
|---|---|---|---|
| FireFox 3.6 | 31 | 1.9 MB | 21.48s (onload: 20.62s) |
| Chrome 11 | 32 | 577.90KB | 6.51s (onload: 6.51s, DOMContentLoaded: 2.88s) |
(I could not find the option to view total bytes downloaded in Opera dragonfly's network tab)
Side Note : When uploading webp images to google storage, S3 or any other cloud service, specify the content-type / mime-type.
gsutil -h "Cache-Control:public,max-age=31536000" -h "Content-Type: image/webp" cp -a public-read kitten.webp gs://[bucket]/images/kitten.webps3cmd put kitten.webp s3://[bucket]/images/ --acl-public --mime-type "image/webp" --add-header="Cache-Control:max-age=315360000"
| css, images, php, webp | 0 comments | Friday, April 15, 2011 |
If you have a simple contact / registration form for a non-CMS / non-framework website with many input fields, it becomes cumbersome to do a server check if all fields passed through doing isset for every field. Many people just check for the existance of a single POST field, but for security reasons, it is better to check for the existence of all required fields.
What if there were 20 fields ? That'll be one long line of issets.
array_diff is a built-in array function in PHP that computes the difference between arrays - it returns an array containing all the entries from the first array that are not present in any of the other arrays.
All we need to check is if the list of required fields are all present as keys in $_POST - this is easily achievable by checking against array_keys of $_POST.
And finally for the count - if all required fields are present in the $_POST's keys, it'll return 0 which is what we want and how we can confirm that all POST field values got sent through.
| php | 0 comments | Monday, January 25, 2010 |
In traditional OOP, if we wanted to get or set an object's private member, we would need to write a public method to do the job.
The reason behind making a member data private and then accessing it through a public method is to avoid the implementing-user to access it directly.
There can be many reasons for prohibiting direct access to member data to the implementing-code.
For example, you may want a radius of a Circle to be within the range of 10 - 500.
In languages like PHP & Python & PERL which have a weak type system, a further check of the data type may be necessary.
But for Python unfortunately even traditional getter & setter methods cannot be implemted because Python doesn't have a public or private. Everything is public by default - though a member can be made private by prefixing two underscores to it. But this is only for convention, it doesn't really serve its true purpose.
PHP 5 and C# .NET have a getter and setter method feature, making it look like we're accessing the data member directly.
C# .NET 3.0 has introduced Automatic Properties where you don't need to specify code for the getter & setter - the compiler takes generates the method body.
JavaScript does seem to have a getter and setter method which I came across just now when searching for the official source to Java 7's new features' documentation, but :
The Getter & Setter feature does not seem to be a much "wanted" feature in the Java community.
Still, this has been proposed by Rémi Forax for JDK 7.
| C#, Java, JavaScript, php | 0 comments | Monday, February 25, 2008 |
| C#, Java, JavaScript, php, Python | 0 comments | Wednesday, August 22, 2007 |
In PHP there's a function called parse_url which splits a given absolute url into various parts as an array.
You can probably get the absolute URI with something as simple as : but unfortunately, how filename & directory name values are stored in $_SERVER varies in server setup and relying on it may not be 100% accurate, though in 99% of the cases, it should do just fine.| php | 1 comments | Monday, August 13, 2007 |
| php | 2 comments | Monday, May 07, 2007 |
| php | 0 comments | Sunday, May 06, 2007 |
| php | 0 comments | Sunday, February 18, 2007 |