Location: PHPKode > scripts > plusPHP Easy Short URL > plusphp-easy-short-url/_pages/post_process.php
<?php

if(!defined('NON_DIRECT')) { exit(header("location: ../")); }

// generating random short tag
function generatr ($length = 6) {
        global $_adres_dir;

        // generating random string
        $string = md5(time().microtime());
        $random_position = rand(1,strlen($string));
        $dummy = substr($string,$random_position,$length);
        // checking if file already exist
        if(is_file($_adres_dir.$dummy.'.txt') || empty($dummy)) {
                // we found file exist - new try (recursion here)
                $dummy = generatr();
        }
        return $dummy;
}

/*
// auto check for apache modules
function apache_is_module_loaded($mod_name) {

        if(function_exists(apache_get_modules)) { $modules = apache_get_modules(); } else { $modules = array(); }

        if(in_array($mod_name, $modules)) {
                return true;
        } else {
                return false;
        }
}
// checking if mod_rewrite loaded anyway - by default system works in rewrite mode
if(!$_rewrite_mode) $_rewrite_mode = apache_is_module_loaded('mod_rewrite');
*/

// regular expression to validate URL
// feel free to replace it to your own
$strRegex = '^(http|https|ftp)://'
                        . '?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?' //user@
                        . '(([0-9]{1,3}\.){3}[0-9]{1,3}' // IP- 199.194.52.184
                        . '|' // allows either IP or domain
                        . "([0-9a-z_!~*'()-]+\.)*" // tertiary domain(s)- www.
                        . '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.' // second level domain
                        . '[a-z]{2,6})' // first level domain- .com or .museum
                        . '(:[0-9]{1,4})?' // port number- :80
                        . "((/?)|" // a slash isn't required if there is no file name
                        . '(/[0-9a-z_!~*\'().;?:@&=+$,%#-]+)+/?)$';

$urls = array();

if(empty($_POST['url']) && empty($_url)) {
        die($error['forgotURL']);
// Let's check if user posted valid URL
} elseif($_POST['url']) {
        $_POST['url'] = trim($_POST['url']);
        if(!eregi($strRegex,$_POST['url'])) {
                die($error['invalidURL']);
        } else {
                $urls[] = $_POST['url'];
        }
} elseif($_url) {
        $urls[] = $_url;
}

if(count($urls) == 1 && !empty($urls[0])) {
                // user want random generated URL
                if(empty($_POST['desired'])) {
                        $go_url = generatr($_url_length);
                // user entered short URL he want to use
                } else {
                        // let's check if this new URL can interfere with existing files
                        $_POST['desired'] = preg_replace("'[^[:alnum:]]'i","-",$_POST['desired']);
                        $possible_file = $_adres_dir.$_POST['desired'].'.txt';
                        $valid_file = $_adres_dir.$_POST['desired'].'_valid.txt';

                        if(is_file($_POST['desired']) || is_file($possible_file) || is_file( $_pages_dir.$_POST['desired'].'php')) {
                                $handle = file($possible_file);
                                $_valid = trim(@implode('',@file($valid_file)));
                                if($_valid > 0 && $_valid < time()) {
                                        unlink($possible_file);
                                        unlink($valid_file);
                                        $go_url = $_POST['desired'];
                                } elseif($handle[0] == $urls[0]) {
                                        $go_url = $_POST['desired'];
                                } else {
                                        die($error['takenURL']);
                                }
                        } else {
                                $go_url = $_POST['desired'];
                        }
                }

                // We got short url - let's put it in file
                if($go_url) {
                        $handle = fopen($_adres_dir.$go_url.'.txt','w') OR die($error['fileUnwriteable']);
                        fwrite($handle, $urls[0]) OR die($error['writeError']);
                        fclose($handle);

                        $timing = explode('*',$_POST['temporary']);
                        if($timing[1] > 0) $_POST['temporary'] = trim($timing[0])*trim($timing[1]);

                        if($_POST['temporary'] > 0) {
                                $_valid_till = time() + $_POST['temporary'] * 60 * 60;
                                $handle = fopen($_adres_dir.$go_url.'_valid.txt','w') OR die($error['fileUnwriteable']);
                                fwrite($handle, $_valid_till) OR die($error['writeError']);
                                fclose($handle);
                        }
                        // output string
                        // if mod_rewrite is up
                        if($_rewrite_mode) {
                                $new_url = "http://$_short_host$BASE_URI$go_url";
                                // seo links mode
                                if($_seo_links && $_POST['desired']) $new_url = $new_url.'.html';
                        // no mod_rewrite - query string mode
                        } else {
                                $new_url = "http://$_short_host$BASE_URI?$go_url";
                        }

                        if($_url) {
                                print $new_url;
                        } else {
                                print str_replace('_NEW_URL_',$new_url,str_replace('_OLD_URL_',$urls[0],$error['success']));
                        }

                }
} elseif($_POST) {
        die($error['forgotURL']);
}

?>
Return current item: plusPHP Easy Short URL