Skip to content

Instantly share code, notes, and snippets.

View finalwebsites's full-sized avatar

Olaf Lederer finalwebsites

View GitHub Profile
@finalwebsites
finalwebsites / tutorial-code.php
Last active February 9, 2020 22:35
PHP tutorial code for the email verification tutorial on www.tutdepot.com.
<?php
require dirname(__FILE__).'/smtp-validate-email.php';
$from = 'your@emailaddress.com';
if (!$db = new mysqli('localhost', 'username', 'password', 'databasename')) {
die('Can\'t connect to the database.');
} else {
// after I imported all email addresses into the data I select 25 records for validation
$res = $db->query("SELECT ID, email FROM emailaddresses WHERE status = 'valid' ORDER BY RAND() LIMIT 25");
@finalwebsites
finalwebsites / FileCache.class.php
Created September 10, 2015 06:53
A (very) simple file based cache script. The class works great for my older pages on www.finalwebsites.com (all pages that are not served by WordPress)
<?php
class FileCache {
protected $cacheValidTime;
public $cacheIsValid = false;
protected $cacheFile;
function __construct($file, $valid = 86400) {
$this->setCacheFile($file);
@finalwebsites
finalwebsites / PHP-Filesize-with-label.php
Created January 30, 2016 18:29
PHP Filesize with label - This custom PHP function reads the file size from a selected file and convert the number of bytes into KB, MB etc. Just define the file name, the file path and the size is returned by the function.
<?php
function file_size($file, $path = "") {
define("DOCUMENT_ROOT", dirname(__FILE__); // better you define this in the root
$bytes = array("B", "KB", "MB", "GB", "TB", "PB");
$file_with_path = DOCUMENT_ROOT."/".$path."/".$file;
// replace (possible) double slashes with a single one
$file_with_path = str_replace("//", "/", $file_with_path);
$size = filesize($file_with_path);
$i = 0;
while ($size >= 1024) { //divide the filesize (in bytes) with 1024 to get "bigger" bytes
@finalwebsites
finalwebsites / PHP-local-date-notations.php
Last active January 30, 2016 18:44
PHP local date notations - This simple class is an example on how-to create an object for simple tasks like a date notation. Sure there are many ways to handle your local date notations, but this "mini class" is a great example to learn how a class works. This class has methods for week day and month name translations.
<?php
class local_date {
private $week_day;
private $day;
private $month;
private $year;
public function __construct() {
$this->week_day = date("l");
@finalwebsites
finalwebsites / create-radio-group.php
Created March 29, 2016 05:54
Create a radio group with PHP
<?php
$values = array('google'=>'Google Search', 'link'=>'Link on some website', 'advert'=>'Advertisement', 'news'=>'News');
$html_elements = array('before'=>'<span>', 'after'=>'</span><br />', 'label'=>'<label for="test"><b>Some label</b></label><br />');
function dynamic_radio_group($formelement, $values, $html, $def_value = '') {
$radio_group = '<div>'."\n";
$radio_group .= (!empty($html['label'])) ? $html['label']."\n" : '';
if (isset($_REQUEST[$formelement])) {
$curr_val = stripslashes($_REQUEST[$formelement]);
@finalwebsites
finalwebsites / select-with-optgroup.php
Created March 29, 2016 06:04
Select menu with optgroup elements
<?php
// First you need some code to get a database result set.
// Split the resultset into two arrays, one for the product groups and one for the categories.
$sql = "SELECT id, parent_id, name, var_name FROM prod_category ORDER BY parent_id, id ASC"; $categories = mysql_query($sql);
while ($obj = mysql_fetch_object($categories)) {
if (!empty($obj->parent_id)) { // there are options
$arr_options[$obj->id]['parent'] = $obj->parent_id; // the corresponding key (group id)
$arr_options[$obj->id]['label'] = $obj->name;
$arr_options[$obj->id]['value'] = $obj->var_name;
// this array is build to check later the existings of options against the option groups
@finalwebsites
finalwebsites / populate-select-menu-mysql.php
Created March 29, 2016 06:07
Populate select elements with PHP / MySQL
<?php
// example sql statement
$sql = "SELECT col_for_value, col_for_label FROM some_table";
function database_select($tbl_value, $tbl_label, $select_name, $label, $init_val = "") {
global $sql;
$result = mysql_query($sql);
$menu = "<label for=\"".$select_name."\">".$label."</label>\n";
$menu .= "<select name=\"".$select_name."\">\n";
$curr_val = (isset($_REQUEST[$select_name])) ? $_REQUEST[$select_name] : $init_val;
$menu .= ($curr_val == "") ? " <option value=\"\" selected>...\n" : "<option value=\"\">...\n";
@finalwebsites
finalwebsites / create-form-fields.php
Created March 29, 2016 06:09
Create form fields with PHP
<?php
function create_form_field($formelement, $label = "", $db_value = "", $length = 25) {
$form_field = ($label != "") ? "<label for=\"".$formelement."\">".$label."</label>\n" : "";
$form_field .= " <input name=\"".$formelement."\" type=\"text\" size=\"".$length."\" value=\"";
if (isset($_REQUEST[$formelement])) {
$form_field .= $_REQUEST[$formelement];
} elseif (isset($db_value) && !isset($_REQUEST[$formelement])) {
$form_field .= $db_value;
} else {
$form_field .= "";
@finalwebsites
finalwebsites / extract-tld-from-domain.php
Last active April 9, 2016 06:07
Extract the TLD from a domain name, PHP tutorial code from http://www.tutdepot.com/parse-url-extract-domain-name/
<?php
$url_parts = parse_url("http://mail.finalwebsites.co.uk");
$domain = $url_parts['host'];
$db = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
$res = $db->query("SELECT tld FROM domain_info ORDER BY LENGTH(tld) DESC");
// "ORDER BY LENGTH(tld)" will give first the co.uk and then the uk
while ($arr = $res->fetch_array(MYSQLI_ASSOC)) {
$tmp_tld = substr($domain, -strlen(".".$arr['tld']));
if ($tmp_tld == ".".$arr['tld']) { // You found the tld
$tld = ltrim($arr['tld'], ".");
@finalwebsites
finalwebsites / random-banner.php
Created April 9, 2016 06:30
Random banner with PHP & MySQL
<?php
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// create table for the banners
$db->query("
CREATE TABLE IF NOT EXISTS `banners` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(35) NOT NULL default '',
`link` varchar(150) NOT NULL default '',
`alt_title` varchar(100) NOT NULL default '',
`image` varchar(35) NOT NULL default '',