Caching web pages speeds-up dramatically the loading time. A ready, waiting cached version is served, no load on the server, no preparation, here it is!
Read moreDrupal ubercart log shows “cURL error: Peer’s certificate issuer has been marked as not trusted by the user.” after installing wildcard SSL certificate
Recently our shopping cart (Ubercart under Drupal) stopped finalizing transactions. The orders got up to the review step, but then, instead of completing the order,
Read moreContact form 7 get value before pipe (_raw_ value equivalent)
On one of our WordPress sites we used the excellent contact form 7 plugin. The plugin allows you to easily create forms for your site.
Read moreFinding the final url of a redirect
In one of my websites there was a complicated mechanism for redirecting mobile users from desktop pages to their mobile versions. When I needed to
Read moreRemove parameters from url in PHP
In case you have a url and you want to remove all parameters from it use this:
1 2 |
$str = 'http://www.example.com/page.php?a=123&b=567'; $strClean = preg_replace ('/\?.+$/', '', $str); |
This regex directive means – replace everything
Read moreGet current’s page full URL – works with clean urls
Here is the simplest way to get the current’s page full URL even when using clean urls (the address in the browsers line is different
Read moreMoving any website from HTTP to HTTPS – securing your site for visitors while improving SEO
For WordPress and Drupal specific instructions please visit Moving WordPress website to HTTPS and Moving Drupal website to HTTPS Technical: Buy and install a SSL
Read morePHP debugging and logging using file_put_contents
There are many ways to debug code. Obviously, the most in-depth debug process is using a full blown debugger such as xDebug (more information can
Read morePassing a variable reference to a function using “&”
When calling a function with parameters, the variables in the parameters are copied/cloned in the function and use their new names. This way you can
Read morePHP heredoc syntax <<<
Sometimes you need to use long strings (usually HTML code) in variable assignment or simply to echo them. There are several options to delimit string,
Read moreSending user input into a database and retrieving it – when to escape and how to display
The mysql_real_escape_string() prepares input for database storage: mysql_real_escape_string() calls MySQL’s library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ‘, ” and
Read moreGetting the visitor’s IP behind a CDN
Recently we installed a WAF (web application firewall) which comes with a CDN setup. The CDN is basically a proxy that all traffic to our
Read moreVisual Studio Code (vs.code) PHP debugging
I just wasted around 7 hours of my life on this thing… life sucks. All I wanted was to have a debugger working for me
Read moreStrip images from HTML / Strings
Easy and simple:
1 |
$content = preg_replace("/<img[^>]+\>/i", " ", $content); |
This came handy when I had a wordpress theme that was showing on the homepage both the featured image and the
Read moreLog in security with password_hash and password_verify
Available since PHP 5.5 password_hash is a really simple and strong way to encrypt passwords. This is a one way encryption which means you cannot
Read moreExtract variables from a key=>value array
The extract() function imports variables from an array. This function uses array keys as variable names and values as variable values. This function returns the
Read moreOutputting all variables in PHP
There is a PHP function that outputs all defined variables:
1 2 3 4 |
<?php $allvars = get_defined_vars(); print_r($allvars); ?> |
Refreshing the database on a development environment
If you have a live site which is constantly updated and a local environment once in a while an update to the local database is
Read morePHP ini_get ini_set
PHP has a php.ini file that has all sorts of environment settings. Some of these settings can be changed at run-time using ini_set or checked
Read moreCreating and deleting form fields dynamically
I needed a form that allows the user to add and remove products with quantities as he wishes. The form had one row with a
Read morePHP variable names with integer increments
In a loop I needed to create / lookup variables that end with a numeric increment. What I found was I can use the following:
Read moreWhat page did the user come from?
If you wish to know what page was the previous page the visitor visited prior to this page you can use:
1 2 |
$testurl=$_POST['urlbefore']; if (!is_null($testurl)) {$urlbefore=$_POST['urlbefore'];} else {$urlbefore= $_SERVER['HTTP_REFERER'];} |
I’m using this
Read morePHP script for general all pages canonical script
The following script will work well as a general in-header canonical one-line script, BUT ONLY FOR SITES THAT DON’T PASS PARAMETERS TO GENERATE CONTENT PAGES.
Read moreAccessing database data with jQuery using a PHP proxy and avoiding the 404 error
I needed to pull database records into a page but couldn’t use PHP directly on that page (it was limited by the CMS used). So
Read moreTagging as spam if phone field has characters in it
1 2 3 4 5 |
// This is for spam attacks that have random chars in the phone field $lwphone=strtolower($in_phone); if (strpbrk($lwphone,'abcdefghijklmnopqrstuvwxyz') !== false) { $spam='1'; } |
Error display in PHP
There are number of different types of errors in PHP . Some of the most common ones are: E_ERROR: This is a fatal runtime error.
Read moreOutputting all form fields in one recursive go
1 |
foreach ($_POST as $key => $value) echo("Field ".htmlspecialchars($key)." : ".htmlspecialchars($value)."<br>"); |
Encoding strings for curl or any type of post/get
A problem I faced was that when the string I want to pass contains “&” and goes through curl it breaks the value passed since
Read moreHTML and Plain text emails
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php //define the receiver of the email //define the subject of the email $subject = 'Test HTML email'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-<?php echo $random_hash; ?>-- <? //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> |
Secure form handling
Validate Form Data With PHP The first thing we will do is to pass all variables through PHP’s htmlspecialchars() function. When we use the htmlspecialchars()
Read more