ACCES INTERZIS | blog de programare si securitate IT




 22 Feb 2010 @ 15:51 

As everybody knows, PHP has two functions which generate randomly a number: rand() and mt_rand(). Using them we can generate easily a random number. Unfortunately, Javascript doesn’t have such functions. I was quite shocked when I started studying Javascript and I saw that there aren’t such functions. So, to get a random number we must develop our own function.

<script type="text/javascript">
function rand(x, y) {
    var random_number;
    if (isNaN(x) == false && isNaN(y) == false && x < y) {
        random_number = Math.ceil(Math.random() * (y-x)) + x;
    } else {
        random_number = -1;
    }

    return(random_number);
}

//example
document.write(rand(4,8));
</script>

Now, let me explain a little bit how the function works. First, I declare a variable which will store the random number. The second step is to make sure that first parameter is smaller than the second one. Finally, we can generate randomly a number using a method of the Math object called random(). But this method generates only values between 0 and 1 such 0.6357802708373617 for instance. This values are pretty ugly. So, we have to polish what we get from random(). To do that we use another method of Math object called ceil(). This method rounds a number upwards to the nearest integer.

Post to Twitter Tweet This Post

Posted By: admin
Last Edit: 22 Feb 2010 @ 15:51

EmailPermalinkComments (0)
Tags
Tags: ,
Categories: programming
 22 Feb 2010 @ 15:49 
<?php
$day = array('Duminica', 'Luni', 'Marti', 'Miercuri', 'Joi', 'Vineri', 'Sambata');
$mounth = array(NULL, 'Ianuarie', 'Februarie', 'Martie', 'Aprilie', 'Mai', 'Iunie', 'Iulile', 'August', 'Septembrie', 'Octombrie', 'Noiembrie', 'Decembrie');

echo $day[date('w')].date(', j ').$mounth[date('n')].date(' Y, H:i:s');
?>

The above PHP script generates the current date in a language other than English. As everybody knows, the PHP function date() returns values only in English language which sometimes might be pretty disturbing. But this PHP script fixes entirely this issue.

Post to Twitter Tweet This Post

Posted By: admin
Last Edit: 22 Feb 2010 @ 15:51

EmailPermalinkComments (0)
Tags
Tags:
Categories: programming
 22 Feb 2010 @ 15:47 
<?php
function extract_extension($file) {
	if (strrpos($file,'.')) {
		$extension = strtolower(substr($file,strrpos($file,'.'),100));
	} else {
		$extension = '';
	}
	return($extension);
}

//example
echo extract_extension("header.inc.php");
?>

The above PHP function extracts the extension of a file. It is useful when we create upload forms or when we read dynamic a folder of files. For instance, a folder of images used by an image gallery or a folder of fonts used by a CAPTCHA.

When we create an upload form for adding images we must not allow the user to upload on our hosting server whatever his mind wants. We must make sure that what the user uploads is truely an image and not something else. Let’s say a malicious executable or scam pages which obviously can harm our server, website, image. So, each time when we develop server-side code we must keep in mind that what the user enters is not trustable. Therefore, we must add all restrictions which are required.

Usually, an image gallery feeds dynamic with the images of a folder of the hosting server. This PHP script helps us to be sure that it grabs only the images of the folder.

Post to Twitter Tweet This Post

Posted By: admin
Last Edit: 22 Feb 2010 @ 15:47

EmailPermalinkComments (0)
Tags
Tags:
Categories: programming
 22 Feb 2010 @ 15:46 

This article is addressed to beginners, those who want to get familiar with the basic concepts of web development and web design. I’ve decided to write it down because I’ve seen many times on forums people trying to understand where web development starts and where it ends. I want to mention that in the article I won’t discuss about ASP technology. So..

More »

Post to Twitter Tweet This Post

Posted By: admin
Last Edit: 23 Feb 2010 @ 09:51

EmailPermalinkComments (0)
Tags
Categories: programming, web
 22 Feb 2010 @ 15:43 

The bellow C++ program allows you to find how many prime numbers are between two positive integers. Before showing the program I just want to mention that a prime number is a positive integer which is devided only by 1 and itself.

More »

Post to Twitter Tweet This Post

Posted By: admin
Last Edit: 22 Feb 2010 @ 15:45

EmailPermalinkComments (0)
Tags
Tags:
Categories: programming