Wednesday, August 31, 2011

Restrict the file uploads using PHP


Below is the php code to restrict the file uploads for a particular type of files. As an example, I'm showing all the extension types in the "Allowed extensions" variable.


<?php

  $allowedExtensions = array("txt","csv","htm","html","xml",
    "css","doc","xls","rtf","ppt","pdf","swf","flv","avi",
    "wmv","mov","jpg","jpeg","gif","png");
    if ($_FILES['upload_file']['tmp_name'] > '') {
      if (!in_array(end(explode(".",
            strtolower($_FILES['upload_file']['name']))),
            $allowedExtensions)) {
       die($_FILES['upload_file']['name'].' is an invalid file type!<br/>'.
        '<a href="javascript:history.go(-1);">'.
        '&lt;&lt Go Back</a>');
      }
    }

?>





Saturday, August 13, 2011

Remove the special characters from the query string

Function to remove the special characters from the query string using PHP.

function trim_req($string){
$string=preg_replace('/[^A-Za-z0-9-]+/', '', $string);
$string = str_replace("<", "", $string);
$slug = str_replace(">", "", $string);
return $slug;
}




Change the color of selected text in a browser for ur site

Below is the css Code to change the default color of the selected text as shown in the below screen shots.


::-moz-selection{ background: #DA3E34; color:#fff; text-shadow: none; }
::selection { background:#DA3E34; color:#fff; text-shadow: none; }




Compress & decrease the page loading time using HTACCESS

Place the below code in htaccess & decrease the loading time taken for the browser.


<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_include mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

Above code will compress the HTML,CSS,JS,PHP,PL script files and also image files. 


Monday, August 8, 2011

count of checkboxes selected using javascript

Below is the code to know the number of checkboxes selected in a form. The function returns the total count of the Checkbox checked.

<script type="text/javascript">
function anyCheck(form) {
var total = 0;
var Count = document.playlist.count.value;
for (var idx = 1; idx < Count; idx++) {
if (eval("document.playlist.ckbox" + idx + ".checked") == true) {
total += 1;
}
}
alert("You selected " + total + " boxes.");
}
</script>
<form method="post" name=playlist>
1<input type=checkbox name=ckbox>
<br>2<input type="checkbox" name="ckbox1" onchange="anyCheck(this.value)">
<br>3<input type="checkbox" name="ckbox2" onchange="anyCheck(this.value)">
<br>4<input type="checkbox" name="ckbox3" onchange="anyCheck(this.value)">
<br>5<input type="checkbox" name="ckbox4" onchange="anyCheck(this.value)">
<br>6<input type="checkbox" name="ckbox5" onchange="anyCheck(this.value)">
<br>7<input type="checkbox" name="ckbox6" onchange="anyCheck(this.value)">
<br>8<input type="checkbox" name="ckbox7" onchange="anyCheck(this.value)">
<br>9<input type="checkbox" name="ckbox8" onchange="anyCheck(this.value)" >
<input type="hidden" value="8" name="count">
</form>