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.
<?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.
<?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.
The bellow PHP script allows you to grab email addresses of a web page.
An important feature of the script is that it can run very well on a local server as EasyPHP or XAMPP. It doesn’t have to run on a hosting server which might has restricted the use of PHP function file_get_contents(). For our records, this resctriction is done editing the php.ini file of the Apache server. More exactly, the following line: allow_url_fopen = Off.
The below PHP script allows you to connect securely to a MySQL database.
<?php
define('SQL_HOST', 'localhost');
define('SQL_USER', 'username');
define('SQL_PASS', 'password');
define('SQL_DB', 'database name');
@mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('I can`t connect to MySQL server!');
@mysql_select_db(SQL_DB) or die('I can`t connect to database!');
?>
Usually, this script is an include file which is called, mostly above DOCTYPE, whenever a page needs a connection to database.
<?php include("includes/connect_to_db.inc.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">