Skip to content

Instantly share code, notes, and snippets.

@apphp-snippets
apphp-snippets / gist:4013433
Created November 4, 2012 20:07
This code allows to list the contents of any given directory.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-list-directory-contents */
function list_directory_content($dir){
if(is_dir($dir)){
if($handle = opendir($dir)){
while(($file = readdir($handle)) !== false){
if($file != '.' && $file != '..' && $file != '.htaccess'){
echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."\n";
}
}
@apphp-snippets
apphp-snippets / gist:4013475
Created November 4, 2012 20:13
This snippet allows you set a limitation for download rate of the file that visitors download from your site.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-download-file-with-speed-limit */
/* set here a limit of downloading rate (e.g. 10.20 Kb/s) */
$download_rate = 10.20;
$download_file = 'download-file.zip';
$target_file = 'target-file.zip';
if(file_exists($download_file)){
/* headers */
@apphp-snippets
apphp-snippets / gist:4013478
Created November 4, 2012 20:14
This code shows how to parse XML file in easy way using PHP.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-parsing-xml-file */
// this is a sample xml string
$xml_string="<?xml version='1.0'?>
<text>
<article id='Article1'>
<title>Title 1</title>
<content>..text here..</content>
</article>
<article id='Article2'>
@apphp-snippets
apphp-snippets / gist:4013482
Created November 4, 2012 20:15
This code allows to remove all duplicate elements from an array using PHP array_unique() function.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-remove-duplicates-in-array */
$input = array("a"=>"apple", "pear", "b"=>"apple", "orange", "avocado", "banana");
print_r($input);
$result = array_unique($input);
print_r($result);
?>
@apphp-snippets
apphp-snippets / gist:4013487
Created November 4, 2012 20:16
This function allows you to pass two variables that represent ID of two elements on your page. What you click on one of them it hide it and shows another one.
<script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=javascript-toggle-elements */
function toggle(id, id2){
var toggle_one = document.getElementById(id);
var toggle_two = document.getElementById(id2);
if(toggle_one.style.display == "block"){
toggle_one.style.display = "none";
toggle_two.innerHTML = "Show";
}else{
toggle_one.style.display = "block";
@apphp-snippets
apphp-snippets / gist:4013493
Created November 4, 2012 20:17
You may perform this work with two ways: pass it directly like described in sample #1 or use an anonymous function like its shown in sample #2.
<script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=javascript-pass-parameters-in-setinterval */
// sample #1
setInterval('alert_function(1)', 1500);
// sample #2
setInterval(function(){ alert_function(10); }, 1500);
// user's alert function
function alert_function(val){
@apphp-snippets
apphp-snippets / gist:4013504
Created November 4, 2012 20:18
The function that closes window is very useful when you use a pop-up window on your page, because it allows the visitor to easily close the window. You can also do it several ways: to use a button, a text link or make the window closes automatically after
<!-- Sample #1: form button -->
/* Source: http://www.apphp.com/index.php?snippet=javascript-close-window */
<form>
<input type=button value="Close This Window" onclick="javascript:window.close();">
</form>
<!-- Sample #2: text link -->
<a href="javascript:window.close();">Click Here to Close Window</a>
<!-- Sample #3: close window after a given number of seconds -->
@apphp-snippets
apphp-snippets / gist:4013518
Created November 4, 2012 20:19
One of the important things about user input is to verify that the user has supplied the email that you have requested. The function below allows you to verify email address with easy.
<script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=javascript-email-validation */
function check_email(email){
var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(reg.test(email)){
return true;
}else{
return false;
}
}
@apphp-snippets
apphp-snippets / gist:4013527
Created November 4, 2012 20:21
This can be used in casting shadows off block-level elements like divs, tables etc. (still not supported in IE7 and IE8). Parameters (from left to right): horizontal offset of the shadow, vertical offset of the shadow, blur radius (optional), spread radiu
<style type="text/css">
/* Source: http://www.apphp.com/index.php?snippet=css-box-shadow */
.shadow {
-moz-box-shadow: 4px 5px 5px 1px #777;
-webkit-box-shadow: 4px 5px 5px 1px #777;
box-shadow: 4px 5px 5px 1px #777;
}
.shadowIE {
background-color:#f5f5f5; /* need for IE*/
@apphp-snippets
apphp-snippets / gist:4013533
Created November 4, 2012 20:21
Today with CSS3, you no longer need a background image for gradients. You can use CSS to add a gradient to the background of some elements on your page. For ex.: change the color hex values ("84c8d7" and "327fbd") to the gradient color you need.
<style>
/* Source: http://www.apphp.com/index.php?snippet=css-background-gradient */
.grad{
background: -webkit-gradient(linear, left top, left bottom, from(#84c8d7), to(#327fbd));
background: -moz-linear-gradient(top, #84c8d7, #327fbd);
filter:
progid:DXImageTransform.Microsoft.gradient(startColorstr="#84c8d7", endColorstr="#327fbd");
}
</style>