Category: php Archives Not Just A Random Blog

08/09/2011

Default Values In Functions

by Cyle — Categories: javascript, php, Web Development — Tags: , , , Leave a comment

We can set a default value for an argument passed to a function so that if an argument is not passed, there will still be a value for it. This can be used when a common value is required for a function and you are wanting to leave it out when calling the function.

 

In PHP

This can be done in PHP by setting an argument to a value when declaring the function.

Example
< ?php
function testFunction($var1, $var2 = true) { 
  // do something here...
  if ($var2=='true'){echo $var1;}
}
 
testFunction('my message'); // will print 'my message'
testFunction('my message',false); // will not print anything
?>

 

In Javascript

Doing the same thing in Javascript is a little more difficult. There are two methods that I am familiar with. The first is not 100% bullet proof and will not work under certain situations. Read More » »

08/09/2011

Include Files Can Have A Return Value

by Cyle — Categories: php, Web Development — Tags: , , Leave a comment

Over the years of programming, I just now figured this out. Include files can have a return value that you can assign to a variable. It can be very helpful especially when you separate your code into include files. For this example I will return an array from an include file, but any variable can be returned using the return command.

Include File (config.php)
< ?php
// do something here...
return array(
    'db' => array(
        'host' => 'example.com',
        'user' => 'user1',
        // etc...
    ),
    // etc...
);
?>

Read More » »

08/08/2011

PHP Functions With an Undefined Number of Arguments

by Cyle — Categories: php, Web Development — Tags: , , Leave a comment

One very useful command in PHP that many do not know about is func_get_args(). When used inside a function, it gets all the arguments that were passed to that function. Most functions have a set number of variables to be passed to a function like below:

 

Without func_get_args()
< ?php
$listOfNames = array();
 
function addPerson($name){
  global $listOfNames; //tells function to use the variable outside of the function 
  $listOfNames[]=$name; //adds the person to listOfNames array
  echo "Successfully added ".$name.".<br>";
  }
 
addPerson('John');
addPerson('Billy');
addPerson('William');
addPerson('Brad');
//etc.
 
?>

If you only needed to add one person at a time then that is no problem, but what if you wanted to add 3 people at one time then 1 the next then 9 later. Well you can do this with func_get_args(), func_get_arg(), and func_num_args(). Read More » »

07/06/2011

Force File Downloads

by Cyle — Categories: php, tools — Tags: , , , Leave a comment

Sometimes you would rather have a user download a file instead of having to right click and choose “save target as”. For example, you may have an image gallery where users come to download the images. Instead of right clicking on an image and choosing “save image as” for every image they want, you could force a download to occur when they click on the image. This can be done easily with PHP.

I use a php file called download.php that logs the file downloaded to my database so I can keep up with a count of how many times each is downloaded. Below is an example of my download.php without the MySQL logging. Read More » »

07/06/2011

Password Hashing in Databases

by Cyle — Categories: php — Tags: , , , Leave a comment

Password hashing is vitally important to websites that have usernames, emails, and passwords saved in databases. Since most people use the same password for everything, once the password is obtained a hacker could get access to much more than what is on just your web site. If a hacker gets access to your database somehow, he/she will have a full list of your users, including email addresses and passwords. Probably the same passwords your users use elsewhere. It is extremely important and easy to protect your users’ passwords.

What is a Hash

A hash is also known as hash code, checksum, hash sums, hash values, message digest. A hash is like a fingerprint to the data it represents. A hash is found by mathematical calculations and is virtually impossible to reverse engineer to get the original data. There are many different hashes (md5, Secure Hashing Algorithm 1 (or SHA-1), SHA-2, haval, and many more. They all have different mathematical ways of creating a hash. For the purpose of this tutorial I will be using Secure Hashing Algorithm 2-256. It is more than enough security than you will probably need. To the right is a simplified diagram of the SHA-256 hashing method.

How to Use Hashes

There are many different ways to use hashes, but the method I use most is PHP’s hash() function. Read More » »

© 2013 Not Just A Random Blog All rights reserved - Theme by ([][]) TwoBeers