PHP: How to create unique short string IDs with PHP & MySQL

PHP: How to create unique short string IDs with PHP & MySQL

PHP: How to create unique short string IDs with PHP & MySQL

IDs are often numbers. Unfortunately there are only 10 digits to work with, so if you have a lot of records, IDs tend to get very lengthy. For computers that’s OK. But human beings like their IDs as short as possible. So how can we make IDs shorter? Well, we could borrow characters from the alphabet as have them pose as additional numbers…. Alphabet to the rescue!

I created this function a long time ago. Time to be nice and share.

More is Less – the ‘math’

The alphabet has 26 characters. That’s a lot more than 10 digits. If we also distinguish upper- and lowercase, and add digits to the bunch or the heck of it, we already have (26 x 2 + 10) 62 options we can use per position in the ID.

Now of course we can also add additional funny characters to ‘the bunch’ like – / * & # but those may cause problems in URLs and that’s our target audience for now.

OK so because there are roughly 6x more characters we will use per position, IDs will get muchshorter. We can just fit a lot more data in each position.

This is basically what url shortening services do like tinyurl, is.gd, or bit.ly. But similar IDs can also be found at youtube: http://www.youtube.com/watch?v=yzNjIBEdyww
(more…)

PHP: Convert string to variable?!

PHP: Convert string to variable?!

PHP: Convert string to variable?!

Problem:

$button0 = "candy";
$button1 = "pop";
$button2 = "nuts";
$button3 = "sugar";

$buttons = 4;
$counter = 0;
while ($counter < $buttons) {
$printbutton = "$"."button".$counter;
echo($printbutton."<br />"); // echos $button0 but not the value of $button0
$counter = $counter+1;
}

What happens is that $printbutton always prints $buttonx (where x = $count) but I want it to print candy or nuts or whatever $buttonx’s value is.

Solution:
(more…)

Check if website is available or down

Performs a cURL-Request to check, if a website exists / is online

//Cron job comand line: /usr/local/bin/php /home/USERNAME/site-monitor/monitor.php > /dev/null
ini_set('error_log','/home/USERNAME/site-monitor/downtime.log');

//returns true, if domain is availible, false if not
function isDomainAvailible($domain) {
	//check, if a valid url is provided
	if(!filter_var($domain, FILTER_VALIDATE_URL)) {
		return false;
	}
	//initialize curl
	$curlInit = curl_init($domain);
	curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
	curl_setopt($curlInit,CURLOPT_HEADER,true);
	curl_setopt($curlInit,CURLOPT_NOBODY,true);
	curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
	//get answer
	$response = curl_exec($curlInit);
	curl_close($curlInit);
	if ($response) return true;
	return false;
}

if (!isDomainAvailible('http://www.example.com/updown.html')) {
	echo "Woops, nothing found there.";
	$message = 'Woops, Your site was down on '.date("Y-m-d H:i:s");
	$headers = 'From: [email protected]';
	mail("[email protected]", "Website is down", $message, $headers);
	error_log($message, 0);
}

Source:
http://css-tricks.com/snippets/php/check-if-website-is-available/

PHP: Downloading a Remote File With cURL and PHP

    $url  = 'http://www.example.com/a-large-file.zip';
    $path = '/path/to/a-large-file.zip';
 
    $fp = fopen($path, 'w');
 
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
 
    $data = curl_exec($ch);
 
    curl_close($ch);
    fclose($fp);

PHP: Adding Parameters To A URL in PHP

PHP: Adding Parameters To A URL in PHP

Adding Parameters To A URL in PHP

This article shows how to add query parameters to a URL in a php script. It is aimed at the beginner to intermediate level PHP programmer but developers from other languages may find some useful information here. At first this seems like a simple enough task. Starting with the url as a string, just add the new parameters to the end. Yeah, that would be nice. It turns out that there are a few conditions that make the process just a bit more difficult. It’s still not a hard problem but there are enough special cases to make it interesting.
(more…)

PHP: Load Time Script

PHP: Load Time

Load Time


This one is a must for developers and system admins. You need to know how long your site takes to load. Nobody sticks around waiting for a site to load anymore – there’s way too much content out there for that. Use this script to find out if your site is taking too long, and if it is, fix it. (more…)

PHP: Getting the difference between two time strings

To get the difference between two time strings use this code

<?php

function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");

?>

PHP: How to Get the Current Page URL

If you want to get the current page URL, you can use the following code:

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER&#91;"HTTPS"&#93; == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER&#91;"SERVER_PORT"&#93; != "80") {
  $pageURL .= $_SERVER&#91;"SERVER_NAME"&#93;.":".$_SERVER&#91;"SERVER_PORT"&#93;.$_SERVER&#91;"REQUEST_URI"&#93;;
 } else {
  $pageURL .= $_SERVER&#91;"SERVER_NAME"&#93;.$_SERVER&#91;"REQUEST_URI"&#93;;
 }
 return $pageURL;
}
?>
<?php
  echo curPageURL();
?>