Web design and coding samples, free tools, tips and more..
Untitled Document

Archive for the ‘Code Samples’ Category

PostHeaderIcon Bing Translator – PHP Usage

I’d like to share a php code that you can use for bing translator api. Just add your bing api id into “AppID= ” part.

<?php
echo translate(’Hello World’, ‘en’, ‘da’).”<br>”;
function translate($text, $from, $to) {
$data = file_get_contents(’http://api.bing.net/json.aspx?AppId=31B78D195F4E66D972222136272630A949274973&Sources=Translation&Version=2.2&Translation.SourceLanguage=’ . $from . ‘&Translation.TargetLanguage=’ . $to . ‘&Query=’ . urlencode($text));
$translated = json_decode($data);
if (sizeof($translated) > 0) {
if (isset($translated->SearchResponse->Translation->Results[0]->TranslatedTerm)) {
return $translated->SearchResponse->Translation->Results[0]->TranslatedTerm;
} else {
return false;
}
} else {
return false;
}
}
?>

<?php

echo translate(’Hello World’, ‘en’, ‘fr’);

function translate($text, $from, $to) {

$data = file_get_contents(’http://api.bing.net/json.aspx?AppId=YoutApiIDHere&Sources=Translation&Version=2.2&Translation.SourceLanguage=’ . $from . ‘&Translation.TargetLanguage=’ . $to . ‘&Query=’ . urlencode($text));

$translated = json_decode($data);

if (sizeof($translated) > 0) {

if (isset($translated->SearchResponse->Translation->Results[0]->TranslatedTerm)) {

return $translated->SearchResponse->Translation->Results[0]->TranslatedTerm;

} else {

return false;

}

} else {

return false;

}

}

?>

  • Share/Bookmark

PostHeaderIcon AS2 Domain Lock – Protecting SWF Files

Domain Locking is the simple way to protect your swf files at least from newbies. Here’s what i’ve found for domain locking. Just copy and paste this code into first frame;

urls_allowed = ["www.guidefordesign.com"];
sitelock(urls_allowed);
function sitelock(urls_allowed) {
lock = true;
domain_parts = _url.split(”://”);
real_domain = domain_parts[1].split(”/”);
domain.text = real_domain[0];
for (x in urls_allowed) {
if (urls_allowed[x] == real_domain[0]) {
lock = false;
}
}
if (lock) {
_root._alpha = 0;
}
}

for multiple domain lock just change the line above;

urls_allowed = ["www.guidefordesign.com","www.anotherdomain.com"];

source: emanueleferonato.com

  • Share/Bookmark

PostHeaderIcon Removing folder with the files inside with php

Another usefull php function. It removes a folder with the content inside.

//function
function delTree($dir) {
    $files = glob( $dir . ‘*’, GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == ‘/’ )
            delTree( $file );
        else
            unlink( $file );
    }
    rmdir( $dir );

}

//usage
delTree(’FolrderName’);

  • Share/Bookmark

PostHeaderIcon php function against sql injection

I’ve began to use this usefull function against sql injection and cleaning other bad characters before inserting data into database.

//function

function clean($str) {
  $str = @trim($str);
  if(get_magic_quotes_gpc()) {
   $str = stripslashes($str);
  }
  return mysql_real_escape_string($str);
 }

// usage:
clean($_POST["data"]);

hope it helps.

  • Share/Bookmark

PostHeaderIcon Top 70 ajax demos and code examples

For more attractive web sites;

  1. Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell
  2. AJAX – microlink pattern tutorial : A microlink is a link that opens up content below it.
  3. Ajax BBC News RSS Reader : demo by Nigel Crawley
  4. AJAX Chat in Python with Dojo : at AquaAjax
  5. Ajax Chess : multiplayer chess
    Read the rest of this entry »
  • Share/Bookmark

PostHeaderIcon Javascript form control for capital letters

With this example of javascript code, you may automatically return capital letters into small letters.

onblur =”this.value = this.value.toLowerCase()”

Just add this code into your textarea or textbox tag, that’s all.

  • Share/Bookmark

PostHeaderIcon Flash Actionscript Preloader

Here is a good & working  AS2.0 preloader sample.

Insert the code above  into frame 1, add new dynamic text on scene and name its var as “showpercent”, that’s all..

onEnterFrame=function(){

            var total:Number=_root.getBytesTotal();

            var loaded:Number=_root.getBytesLoaded();

            if(loaded==total){

                        delete  onEnterFrame

                        play();

            }else{

                        var percent:Number=Math.floor((loaded/total)*100);

                       showpercent.text=percent

            }

}

stop();

  • Share/Bookmark

PostHeaderIcon How to disable enter key on html form submit

Here’s a usefull javascript to disable enter key on html form submit. It directs the user to use mouse click.

 

<script type=”text/javascript”>

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type==”text”))  {return false;}
}

document.onkeypress = stopRKey;

</script>

  • Share/Bookmark

PostHeaderIcon How to disable browser cache for flash

As you know when you upload new swf file to server, browser doesn’t show it until you delete browser cache. Also it’s important to delete swf cache for websites which includes dynamic data and dynamic sub swf files. Here’s the method that we pass this problem for flash.

Read the rest of this entry »

  • Share/Bookmark

PostHeaderIcon How to delete xml cache with actionscript?

İt’s important to delete xml cache in flash if your content is renewing it self often.  Here’s the sample code for deleting xml cache with actionscript;

Just change your xml loading code with this:

Read the rest of this entry »

  • Share/Bookmark

Search