WP-Mix

A fresh mix of code snippets and tutorials

Category Archive: PHP

Posts about PHP

Page 2 of 6

PHP Get Absolute Path, Document Root, Base URL

Depending on your server configuration, getting correct path information can be challenging. For example, PHP does not provide a variable that will return the the base URL of your site. To help out, you can use the following code snippets to get the absolute path, document root, and base URL, respectively.

PHP Spoofing Headers with cURL

While working on a recent book-sale script, I needed a way to test various request headers. This script is what I used to spoof just about everything except the IP address (which it seems is not possible to spoof via PHP/cURL).

PHP securing email scripts

This tutorial describes several important strategies for securing contact forms and scripts that make use of PHP’s mail() functionality. If your script is sending email based on user input, these tips will help to keep things safe and secure.

Get WP, PHP, and SQL Info

Here are some sweet code snippets for getting various types of server information. Includes more functions for getting and/or checking various types of WordPress, PHP, and SQL data. Strictly plug-&-play for quick reference and copy/paste usage.

jQuery Auto Resize Multiple Textareas

The User Notes Widget that’s included in my Dashboard Widgets Suite plugin enables users to edit notes right on the WP Dashboard. To make things extra awesome, each note automatically expands as the user types more lines of text. This post explains how I achieved this trick, aka multiple auto-resize textareas.

PHP fix exif_imagetype() error

Working on my plugin USP Pro, it’s become apparent to me that some servers for whatever reason disable the function exif_imagetype() function. So some users would get an error when trying to upload images. To resolve this, I added the following simple function to the plugin core.

PHP output buffering example

PHP’s ob_start() and ob_get_clean() are useful for buffering output of printed content and so forth, but I use them very rarely and tend to forget their order/syntax. So this post provides a quick copy/paste example that I can grab the next time I need to output buffer something.

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.