Useful PHP Functions

Web Stuff

mysql functions php

Today we will share some really useful PHP functions that will make your life easier and faster when creating new code.

PHP functions are great to reduce and simplify your code. As soon you use a code more then once, make a class for it. Very simple and very practical.

Now let's have a few very useful PHP functions, we use it daily and in all our projects.

Redirect your visitor to another page.

function redirect($url, $code = 302) {
    header('Location: '.$url, true, $code);

To use this function and redirect your visitor to another page you will only need to use:


How simple is that? Plus you can use it as many times you want for all your redirection calls, but hang on why no second parameter? You can but because we set a default parameter you don't have to if you use 302 anyway.

Filter User Input

function input_filter($value) {
	$value = filter_var($value, FILTER_SANITIZE_STRING);
	return preg_replace("/[^0-9 _,.@-p{L}]/u", '', $value);

This small function will filter the content from input fields for example:

$filtered = $input_filter($_POST["title"];

Escape for MySQL

function smartsql($value) {
    // To your database
	global $jakdb;
	// Check Magic Quotes
	if (get_magic_quotes_gpc()) {
		$value = stripslashes($value);
	// Not integer
    if (!is_int($value)) {
        $value = $jakdb->real_escape_string($value);
    return $value;

This function does a few things, it will check if magic_quotes are enabled, if so it will strip off backslashes. The next step is to check if the value is not integer, if not it will escape the string and make it secure for your database. Again to use the function in your database updates or inserts only use:


Cut some long text

function cut_text($text,$limit,$ending) {
	// empty limit
	if (empty($limit)) $limit = 160;
    $text = trim($text);
    $text = strip_tags($text);
    $text = str_replace(array("r","n",'"'), "", $text);
    $txtl = strlen($text);
    if($txtl > $limit) {
        for($i=1;$text[$limit-$i]!=" ";$i++) {
            if($i == $limit) {
                return substr($text,0,$limit).$ending;
        $jakdata = substr($text,0,$limit-$i+1).$ending;
    } else {
    	$jakdata = $text;
    return $jakdata;

Let's say you code a blog and you want to preview the article instead of showing the whole content, easily use the cut_text function. The function will cut to the length you set and does not cut off words, so your preview text always looks nice. You know how to use it now, don't you?!

Create a random password

// Password generator
function password_creator($length = 8) {
	return substr(md5(rand().rand()), 0, $length);

That should be self explained, the function will return a password with the length of your choice (8 characters are standard).

Encode email address

// encrypt email address (prevent spam)
function encode_email($e) {
	for ($i = 0; $i < strlen($e); $i++) { $output .= '&#'.ord($e[$i]).';'; }
	return $output;

Very useful function to prevent spam on your displayed email addresses. Simply encode your email address with this function before you display the email address.

Get IP Address

function get_ip_address() {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return $ip;
    return 0;

Last function for today is get your visitors IP address the best and most accurate way.

Now get back to work, use our functions and simplify your code. Thank you for reading another article from JAKWEB. Please feel free to post your suggestions or feedbacks below.

Comment(s) (5)

  • avatar syarat

    Hey! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting fed up of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform.I would be awesome if you could point me in the direction of a good platform.

    01.09.2016 0 Reply
  • avatar Simon

    Hey! I just wanted to ask if you ever have any trouble with hackers?My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any solutions to prevent hackers?

    21.09.2016 0 Reply
    • avatar Jerome

      With WordPress and any other software you should always update to the latest version. There is a reason why new versions are released, it is not just for more features usually developers also work in security improvement and bug fixing.

      23.09.2016 0
  • avatar Thomas

    Hallo, Ich finde den Aufbau der Seite klasse. Macht weiter so.

    5 days ago 0 Reply

Hey, leave a comment...