Apple looses ALAC

by admin on oktober 28, 2011

ALAC, wich stands for ”Apple Lossless is a lossless audio codec that Apple developed some time ago for digital music. The codec can compress music files anywhere from 40-60% of their original size with no discernible loss in audio quality or fidelity.

Apple’s ALAC codec is now open source.

WordPress categories with descriptions

by admin on september 12, 2011

Some WordPress-themes has the description of the category below the category name.
This is not standard in WordPress.
So this is how you can do it

$args=array(
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li><strong><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></strong>';
echo '<br />'. $category->description . '</li>';
}
#wpyagnav{
height: 61px;
background: #d0d0d0;
margin-left: auto;
margin-right: auto;
width: 980px;
}

ul.wpyagcat{
padding-top: 11px;
}

ul.wpyagcat li{
display: inline;
float: left;
position: relative;
margin-left: 18px;
text-align: center;
}

Charset

by admin on augusti 31, 2011

For those of you who have troubles with Charset (it’s easy to forget!) in your code, you can add the code to your .htaccess to make sure that all your pages are shown in correct charset! Even if you forget to add charset on your pages!

<FilesMatch "\.(htm|html|css|js)$">
AddDefaultCharset UTF-8
</FilesMatch>

Email validation with filter_var

by admin on juli 26, 2011

When having a website that allows user-registration it’s good to check that the user data inputed is valid. A lot of people use RegExp to check if an email is valid. This is generally not the best way to do it. I recently came across a good way to do e-mail verification by using the php filter_var function.

This is how it’s done

$email = "someone@exa mple.com";

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "E-mail is not valid";
  // Rest of PHP-code
  }
else
  {
  echo "E-mail is valid";
  // Rest of PHP-code
  }

Secure GET with intval

by admin on juli 26, 2011

A big problem, and a rather common one, is that users try to modify (read: hack) a websites to do things that the code wasen’t supposed to. Such as allow a basic user – to gain admin-access. This can easily be done by changing the data posted by $_GET.

So – how can we prevent this?
By using php’s function called intval() we can check that it returns as an integer.

This is how it can be done:

// Echo out the user-id wich we get from $_GET wich will return 1 or 1=1
echo $_GET['userId'];
// Wrap the $_GET with intval()-function and set it to the $id variable
$id=intval($_GET['userId']);
// Echoing out the id will now return '1'
echo $id; // 1

// We're now selecting som the table "users" with out '$id' var - wich is a clean integer variable! This is safe!
mysql_query('SELECT * FROM users WHERE id='.$id);

Do remember to sanitize your variables with more functions if they contain other data than just integers. Using stripslashes and mysql_real_escape_string is a good method! More on this in a later post!

RegExp snippets

by admin on juli 26, 2011

I recently came across a webpage that shares some RegExp code for some verification-purposes. And as i use RegExp a lot in my projects to check/validate – i thought i’d share it with the rest of you.

Validate username

Validate username, consist of alpha-numeric (a-z, A-Z, 0-9), underscores, and has minimum 5 character and maximum 20 character. You could change the minimum character and maximum character to any number you like.

$username = "user_name12";
if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) {
    echo "Your username is ok.";
} else {
    echo "Wrong username format.";
}

Validate IP addresses

Validate Ip adresses

$IP = "198.168.1.78";
if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)) {
    echo "Your IP address is ok.";
} else {
    echo "Wrong IP address.";
}

Validate Creditcard

Validate creditcards with this regexp

$cc = "378282246310005";
if (preg_match('/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/', $cc)) {
    echo "Your credit card number is ok.";
} else {
    echo "Wrong credit card number.";
}

Validate URL’s

Used to validate the urls

$url = "http://nlstudio.se/";
if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) {
    echo "Your url is ok.";
} else {
    echo "Wrong url.";
}

Extract domain name from certain URL

This regexp will extract the domainname from a URL

$url = "http://nlstudio.com/index.html";
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1];

echo $host;

Highlight

Used to highlight certain words in a string

$text = "This is a sample text from nlstudio where you can read more about php, webdesign, photography and get to know the user behind the blog.";

$text = preg_replace("/\b(nlstudio)\b/i", '<span style="background:#5fc9f6">\1</span>', $text);

echo $text;

Filter out duplicate data from MySQL

by admin on juli 26, 2011

Alright – i did this big mistake – i forgot to check and see if a user-email was registerd in the database before another user was added (i wasen’t going to allow 2 account per email!). If i had a small databas with just a few users – i could go through them manually and remove those rows. But i have quite a few users on my webpage – so i went the easier way – sql!

select email,count(email) from `tbl_users`
group by email having count(email) > 1

By doing this is could see wich emails where registered 2+ and simply remove them!

censor-functions

by admin on juli 24, 2011

Needed a simple function to check for bad words on my webpage – so i made a function for it.
Just wrap it around your variable like this:

include ('includes/functions.php');
censor($userPost);

And make sure that your functions.php file contains this code:


function censor($text){
// Create an array of 'badwords'
$change = array('badword1', 'badword2', 'badword3');

// Replace all the bad words
$text = str_replace($change, "***", $text);

return $text;
}