I will be posting a variety of small PHP code snippets and functions which can easily be modified and integrated into your existing PHP code.
If you would like me to explain any snippet in this thread or assist you with using them in your code then please let me know. Feel free to request a snippet.
#1 Get a users IP address
Function
Add this function to your config/settings file or at the top of the file where it is going to be used.
CODE:
<?php
function get_ip(){
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return trim($ip);
}
?>
Usage
This code will store the users IP address in a variable and display it on the page.
CODE:
<?php
$ip_address = get_ip();
echo $ip_address;
?>
#2 Generate a random string
Function
Add this function to your config/settings file or at the top of the file where it is going to be used.
CODE:
<?php
function rand_string($length){
$str = '';
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen($chars);
for ($i = 0; $i < $length; $i++) {
$str .= $chars[rand(0, $size - 1)];
}
return $str;
}
?>
Usage
This code will generate a 32 character string and display it on the page.
CODE:
<?php
$string = rand_string(32);
echo $string;
?>
#3 Filter user input
Function
Add this function to your config/settings file or at the top of the file where it is going to be used.
CODE:
<?php
function filter($data){
$data = trim(htmlentities(strip_tags($data)));
if(get_magic_quotes_gpc()){
$data = stripslashes($data);
$data = mysql_real_escape_string($data);
}
return $data;
}
?>
Usage
This code will filter user input ($_POST) from a form using and store the clean input in a variable. (Please note: A MySQL connection must be opened before using this function)
CODE:
<?php
foreach($_POST as $key => $value){
$data[$key] = filter($value);
}
$username = $data['username'];
?>
#4 Check if key exists in an array
Function
Add this function to your config/settings file or at the top of the file where it is going to be used.
CODE:
<?php
function safeOutput($arr,$index) {
if (isset ($arr[$index]) ) {
return $arr[$index];
} else {
return 'N/A';
}
}
?>
Usage
This code will check the an array (variable $array) for the specified key words.
CODE:
<?php
$array = array('php'=>'yes');
$tick = safeOutput($array, 'php');
$tack = safeOutput($array, 'other');
echo $tick;
echo '<br/>';
echo $tack;
?>
Returns
yes
N/A
#5 Console debugging log
Function
Add this function to your config/settings file or at the top of the file where it is going to be used.
CODE:
<?php
function debug(str, isFunction) {
//----------------------------------------------
$prefix = "";
if (isFunction) {
prefix = "* ";
} else {
prefix = " - ";
}
try {
console.log(prefix + str);
} catch (e) {
}
}
debug("Document ready", true);
debug(currentPos, false);
?>
Usage
Check your console.
CODE:
Easy way to track what's running what.
Should output functions with * and non functions with -
#6 Check string/variable for data
Function
CODE:
<?php
function isEmpty($str) {
//----------------------------------------------
if (($str === null) || ($str === undefined) || ($str === "" )) {
return true;
} else {
return false;
}
}
?>
Usage
CODE:
<?php
$queryOne = "";
$queryTwo = "input";
if(!isEmpty($queryOne)){
echo "isn't empty <br/>";
} else {
echo "is empty <br/>";
}
if(!isEmpty($queryTwo)){
echo "isn't empty <br/>";
} else {
echo "is empty <br/>";
}
?>
Returns
is empty
isn't empty
Edited twice, last edited 31/8/11 - 9:16am.
Posted on Friday, 26th August 2011