PHP – set hour to Eastern European Time(Bucharest)

This is the test script, you normally just need the first line of code.
Enjoy!

jQuery (Ajax) Contact form with all elements

This is my default CONTACT form built with jQuery and CSS.
The form uses a jQuery call to a PHP script that inserts data into a Mysql DB and sends two emails of confirmation, all this under the hood… meanwhile, there is a CSS only animation that displays a message that the data is been sent to the server and shows a positive message if that data was sent without errors and an error message if there was an error.

Mysql: Table structure for table `contact`

This is the basic table structure for my contact section on many projects I work on…
The id field is auto_increment and the time field is just a PHP time() string.

PHP & HTML – easy spam prevention using hidden form fields (sample code)

The easy and fast working method of securing your forms agains spam bots is as easy as a few lines of PHP and HTML code.

Spam bots, at least, most of them, crawl random websites and fill their forms with all kind of ads or unwanted content. There are a lot of alternatives to stop this, Google Captcha, reCaptcha, etc., I used all of this methods, they work pretty well but, sometimes, when you move your site to a new server or domain, you need to spend a lot of time working around this methods to make them work again.

I simply tried a really simple method that I found a few years ago, the method implies using simple PHP and HTML to trick the bot to fill a field that the user doesn’t see(hidden via CSS).

The code is pretty simple…

Here is the HTML…

 

Here is the PHP…

How to Optimize Your Website And Obtain a Google Page Speed of 98-100?

First, please backup your files before you start modifying anything I write in this post.

Thank You!

First, why you should follow my advice? Well, almost anyone can tell you that a fast website is a good website and a good website is a mobile and desktop 100% compatible website but, as a developer, you need to test your site with Google’s Page Speed tool(https://developers.goo…).

Obtaining a good grade there can help your search engine traffic… So, what Google Page Speed score your project gets?

I’ve done a few projects with 98-99 out of 100, and a few with perfect 100.

I will list here a few “secrets” I used to boost my grade from 68/100 to 99/100.

1. Use only inline CSS

You need to use only inline css, period. Don’t link to any external frameworks(Bootstrap, etc.), don’t link to any Google Fonts file, to any particular style…

If you want to be productive, you ca use PHP to include_once(“”); your css files into the interpreted file.

If you want to use fontawesome or Google Fonts, you can put fontawesome file before you close your </body> tag.

You can also modify the Google Fonts .css file and include it inline into your source via PHP. Also, you can remove all the Vietnamese or Greek fonts from the Google Fonts file and use only the Latin style, works with english projects and most of the languages.

2. Insert the .js files at the end of the file

This is no secret that, inserting the .js files before you close </body> tag can get you a good Page Speed grade.

3. Use Server Side compression(gzip)

This is a pretty easy step, I provided instructions here on another post on how to do that.

4. Optimize images

Don’t exaggerate with images, use a low resolution image thumbnail, I use only < 10kb images. Specify the width and height of the image. You can use a free online tool to reduce the size of the images.

5. Minify css, js and html

This step gives anyone a lot of work if you are updating your site constantly… You can use free online tools to minify your css, html and js code.

6. Reduce the Server Response Time

Don’t load your server with requests, use, as much as you can only static content or make Ajax requests to content.

Bonus Point!

Transform your favicon in BASE64 text and include the code via PHP and get rid of a few connections that can slow down your website… I use this tool to convert my favicons: https://xaviesteve.co…

 

Hope this experience helps, try and play with this list and tell me your result.

Take a look at a sample page that I’ve recently made and got a great score: https://developers.google.com…

jQuery load() external file inside div

Sometimes I use the jQuery load() function to display dynamic content inside html elements. As the description says:

load() Description: Load data from the server and place the returned HTML into the matched element.

I provided a working example that loads a simple html file with html tags and another php files that contains variables.

Here is the jQuery code:

Here is the PHP code that deals with the two variables:

Live Demo Here

The simplest way for gzip compression (PHP)

ob_start("ob_gzhandler") returns FALSE if browser doesn’t support gzip, so then is called normal ob_start();

You can use the browser header information to see if the page is compressed or a tool that checks this for you.

MySQLi Object-Oriented PHP Database Connection

PHP - Mysql logoBefore we do anything with our project, we need to access the data stored in the MySQL database. For that, we need to be able to connect to the database server.

  • MySQL is the most popular database system used with PHP.
  • With PHP, you can connect to and manipulate databases(insert, edit, delete data).

You can create a connect.php file that contains this functionality so that you can include(“connect.php”); anywhere you need mysql along your project.

You can use a simple – MySQLi Procedural – type of connection or you can use a MySQLi Object-Oriented connection type or a PDO connection (PHP Data Objects).

Most of the people I know are using a MySQLi Object-Oriented connection like this:

Also, the simple procedural method looks almost the same as the OOP method, with a small difference:

Most of the projects can handle the database with the two methods from above, along this, there is another method for a connection this time using PDO (PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API)