Skip to content

Instantly share code, notes, and snippets.

@apphp-snippets
apphp-snippets / gist:2759498
Created May 20, 2012 20:54
Sometimes you need to generate passwords for customers automatically when a new account is created. This code allows you choose the desired length and strength for the password and it is very flexible
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function GeneratePassword($length=8, $strength=0){
$vowels = 'aeuy';
$consonants = 'bdghjmnpqrstvz';
if($strength >= 1) $consonants .= 'BDGHJLMNPQRSTVWXZ';
if($strength >= 2) $vowels .= 'AEUY';
if($strength >= 3) $consonants .= '12345';
if($strength >= 4) $consonants .= '67890';
if($strength >= 5) $vowels .= '@#$%';
@apphp-snippets
apphp-snippets / gist:2759502
Created May 20, 2012 20:55
You may retrieve all needed file path data using PHP's built-in function pathinfo. You don't need to create your own functions or use regular expressions to get this info. It was already been created for this purpose
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
// pathinfo() constants parameter list
// PATHINFO_DIRNAME => directory name
// PATHINFO_BASENAME => name of file (w/out extension)
// PATHINFO_EXTENSION => file extension
// PATHINFO_FILENAME => file name w/ extension
$path = '/images/thumbs/my_avatar.gif';
@apphp-snippets
apphp-snippets / gist:2759504
Created May 20, 2012 20:55
This function returns the duration of the given time period in days, hours, minutes and seconds. For example: echo convertSecToStr('654321'); would return "7 days, 13 hours, 45 minutes, 21 seconds"
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function convertSecToStr($secs){
$output = '';
if($secs >= 86400) {
$days = floor($secs/86400);
$secs = $secs%86400;
$output = $days.' day';
if($days != 1) $output .= 's';
if($secs > 0) $output .= ', ';
@apphp-snippets
apphp-snippets / gist:2770224
Created May 22, 2012 16:54
You may create a back button like your browser has. The following script allows the visitor to press a button and returns the him to the previous page.
<form>
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
<input type="button" value="Previous Page" onclick="history.go(-1)">
</form>
@apphp-snippets
apphp-snippets / gist:2770245
Created May 22, 2012 16:57
Sometimes you have some information on your page and your visitors might want to copy it. The easiest way is to provide a mechanism that allows them to simply click a button to do so. You have to paste this code into the head of your web page
<script language="javascript">
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function CopyText(el){
var selectedText = "";
if(window.getSelection){
selectedText = window.getSelection();
}else if (document.getSelection){
@apphp-snippets
apphp-snippets / gist:2770263
Created May 22, 2012 16:59
You can use JavaScript window.location to redirect a visitor to a required page. You may have seen this feature used by sites with full page ads, or to redirect visitors to the site's new domain name. You can add also a timer to the script that will allow
<script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
var count = 6;
var redirect = "http://www.apphp.com";
function countDown(){
var timer = document.getElementById("timer");
if(count > 0){
count--;
timer.innerHTML = "This page will redirect in "+count+" seconds.";
setTimeout("countDown()", 1000);
@apphp-snippets
apphp-snippets / gist:2770274
Created May 22, 2012 17:01
Most today's web browsers like Firefox (Ctrl+D), Opera (Ctrl+T) and IE (Ctrl+D) provide a keyboard shortcuts to enable users bookmark their favorite pages. But if you want to provide your visitors with a "Bookmark this page" link they can click you may us
<script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function CreateBookmarkLink(){
var title = document.title;
var url = document.location.href;
if(window.sidebar){
/* Mozilla Firefox Bookmark */
window.sidebar.addPanel(title, url, "");
}else if(window.external){
/* IE Favorite */
@apphp-snippets
apphp-snippets / gist:2770287
Created May 22, 2012 17:02
You may add rounded corners to your CSS3-based elements, like a border or button. You can change the radius to increase or decrease the rounding of the corners. This is a styles definition rounding of each corner, it lets you individually round each of th
<style type="text/css">
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
.round{
border:1px solid #c1c13a;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 10px; /* for old Konqueror browsers */
}
</style>
@apphp-snippets
apphp-snippets / gist:2770294
Created May 22, 2012 17:03
Sometimes it's desirable to change a style of the first and/or last elements in a container. You can do this by manually applying classes to your HTML elements: ("last-child" still not supported in IE8).
<style type="text/css">
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
p.first { margin-top: 0 !important; margin-left: 0 !important; }
p.last { margin-bottom: 0 !important; margin-right: 0 !important; }
/* or */
div#articles p:first-child { border:1px solid #c1c13a; }
div#articles p:last-child { border:1px solid #3ac13a; }
/* or */
div#articles > :first-child { text-align:left; }
@apphp-snippets
apphp-snippets / gist:4013426
Created November 4, 2012 20:06
This code allows to pass filename in the $file_name variable and function will return file extension only.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-file-extension */
function get_file_extension($file_name)
{
/* may contain multiple dots */
$string_parts = explode('.', $file_name);
$extension = $string_parts[count($string_parts) - 1];
$extension = strtolower($extension);
return $extension;
}