A fresh mix of code snippets and tutorials

Category Archive: PHP

Posts about PHP

Page 3 of 6

PHP Get Memory Usage vs. Total

Here is a useful function that I modified for my WordPress plugin, Dashboard Widgets Suite. It returns an array containing the current memory usage and the total memory usage, so you can get and display a ratio of the values as needed.

PHP Get Server Information

Here are some scrap PHP scripts leftover from my work on Dashboard Widgets Suite. These functions were considered specifically for use with the System Information Widget, and provide a variety of details about the current server configuration and software setup.

PHP check if file exists by URL

This quick PHP snippet can be used to check if a specific URL exists. I use this in one of my projects for checking the existence of a robots.txt file on the site. Can be modified to check for files on any publicly accessible site.

Cron Notes: Ping External URL with No Output

Working on setting up Cron on ASO server for testing purposes.. just want to ping a URL/file, not save or send any output whatsoever. Here are my notes for future reference.

PHP Sanitize XSS

Here is a simple PHP function I use to prevent XSS attacks. Use it to sanitize any user-input or otherwise unknown variables before use.

PHP Add and Remove Query String Variables

Two quick functions, one for adding a query string variable and another to remove a query string variable. Either of these functions can be used in any PHP script to modify query-string parameters.

PHP Shorten String by Character Count

Quick copy-n-paste function to truncate or shorten a string of text to a specific number of characters. Useful for things like “next” and “previous” navigation links and so forth.

PHP Check if Request is HTTPS

Here we have a simple and effective PHP function that checks whether or not HTTPS (via SSL) is used for the current request.

PHP foreach loop

I am always using this PHP code snippet, which basically loops through an array that may or may not include nested arrays. So to make things easier, I am posting here for quick reference.

PHP Sanitize Form Data

On the Web, Cross-Site Scripting (XSS) is one of the most common types of attacks. Fortunately, there is an easy fix: always filter input data and always escape output data.

Filter pre tags in bbPress

A couple of functions used at htaccessbook.com to enable unescaped/unencoded pre and code tags in bbPress forum posts.

Display a Simple “Tweet This” Link

Twitter changes their API quite a bit, however it is still possible to display a simple “tweet this” link anywhere on your web pages.