Thursday, June 8, 2017

mysqli connection file and some function

Here i display Mysqli connection setting and some important functions.

connect.php

<?php
date_default_timezone_set("Asia/Kolkata");

$current_date = date('Y-m-d H-i-s');
global $current_date;

global $db_conx;

$db_server = "localhost";
$db_user = "dbusername";
$db_password = "passwordhere";
$db_database = "dbnamehere";
*/
$db_conx = mysqli_connect($db_server, $db_user, $db_password, $db_database);

if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
}

/*
$check_ban_ip_actual = @$_SERVER['REMOTE_ADDR'];
$check_ban_ip = mysqli_query($db_conx, "SELECT ip_address FROM banned_ip WHERE ip_address = '".$check_ban_ip_actual."'");
if(mysqli_num_rows($check_ban_ip)==1){
    die();
}*/

global $server_address;
$server_address = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
?>

functions.php

<?php
function string_texteditor($text)
{
    global $db_conx;
        return mysqli_real_escape_string($db_conx, str_replace("'","",$text));
}
function string_sanitize($string){
        global $db_conx;
       
        return mysqli_real_escape_string($db_conx, str_replace("'","",stripslashes(htmlentities(strip_tags($string)))));
    }
function get_all_user(){
        global $db_conx;
       
        $ret = array();
        $select = mysqli_query($db_conx, "SELECT * FROM user WHERE first_name != '' ");
        while($row = mysqli_fetch_assoc($select)){
            $ret[] = $row;
        }
        return $ret;
    }

function get_categoryid_from_title($title){
        global $db_conx;
       
        if($title != ""){
            $select = mysqli_query($db_conx, "SELECT * FROM review WHERE title = '".$title."'");
            return $row[] = mysqli_fetch_assoc($select);
        }
    }
    function get_categoryName_from_catid($catid){
        global $db_conx;
       
        if($catid != ""){
            $select = mysqli_query($db_conx, "SELECT * FROM category WHERE id = '".(int)$catid."'");
            return $row[] = mysqli_fetch_assoc($select);
        }
    }
    function get_categoryName($catid){
        global $db_conx;
       
        if($catid != ""){
            $select = mysqli_query($db_conx, "SELECT * FROM category WHERE category_id = '".(int)$catid."'");
            return $row[] = mysqli_fetch_assoc($select);
        }
    }

    function edit_customer($first_name, $last_name, $mobile_number, $email, $customer_id){
        global $db_conx;
       
        if($first_name != "" && $last_name != "" && $mobile_number != "" && $email != "" && $customer_id != ""){
            $first_name = string_sanitize($first_name);
            $last_name = string_sanitize($last_name);
            $mobile_number = string_sanitize($mobile_number);
            $email = string_sanitize($email);
           
            mysqli_query($db_conx, "UPDATE customer SET first_name = '".$first_name."', last_name = '".$last_name."', mobile_number = '".$mobile_number."', email = '".$email."' WHERE customer_id = '".(int)$customer_id."'");
        }
    }
   
    function get_customer($customer_id){
        global $db_conx;
       
        if($customer_id != ""){
            $select = mysqli_query($db_conx, "SELECT * FROM customer WHERE customer_id = '".(int)$customer_id."'");
            return $row[] = mysqli_fetch_assoc($select);
        }
    }

function add_product($product_name, $product_description, $discription, $product_length, $product_max_weight, $product_price5,$product_price10, $product_price15, $product_price25, $product_price50, $product_price100, $product_price, $tax_id, $product_status, $category_id, $subcatagory_id, $supersubcatagory_id, $product_test, $product_width, $product_height, $bundle, $special_offer, $shipping_id = ""){
        global $db_conx; global $current_date;
        //echo "<script type=\"text/javascript\">alert('submit clicked');</script>";
        if($product_name !== "" && $category_id !== "")
        {
            $product_name = string_sanitize($product_name);
            $product_name = strtolower($product_name);
            $product_test = string_sanitize($product_test);
            $alias = get_alias($product_name);
            $table_name = 'product';
            $alias_final = check_alias($table_name, $alias);

            $sql = "INSERT INTO product(product_name, product_description, discription,product_length, product_max_weight, product_price5, product_price25, product_price100, category_id, shipping_id, price, tax_id, status, date_added, sub_category_id, supersubcategory_id, alias, test, product_width, product_height, bundle, product_price10, product_price15, product_price50, special_offer) VALUES('".$product_name."', '".$product_description."', '".$discription."', '".$product_length."', '".$product_max_weight."', '".$product_price5."', '".$product_price25."', '".$product_price100."', '".$category_id."', '".$shipping_id."', '".$product_price."' , '".$tax_id."', '".(int)$product_status."', '".$current_date."', '".$subcatagory_id."', '".$supersubcatagory_id."', '".$alias_final."', '".$product_test."', '".$product_width."', '".$product_height."', '".$bundle."', '".$product_price10."', '".$product_price15."', '".$product_price50."', '".$special_offer."')" ;

            $insertproduct = mysqli_query($db_conx, $sql);
            $inserted_product = mysqli_insert_id($db_conx);

            $check_category_qty = mysqli_query($db_conx, "SELECT category_id FROM category WHERE category_id = '".(int)$category_id."' AND qty_type = ''");
            if(mysqli_num_rows($check_category_qty) == 1){

                $insert_qty_array = array();
                if($product_price5 != 0){
                    array_push($insert_qty_array, 5);
                }
                if($product_price10 != 0){
                    array_push($insert_qty_array, 10);
                }
                if($product_price15 != 0){
                    array_push($insert_qty_array, 15);
                }
                if($product_price25 != 0){
                    array_push($insert_qty_array, 25);
                }
                if($product_price50 != 0){
                    array_push($insert_qty_array, 50);
                }
                if($product_price100 != 0){
                    array_push($insert_qty_array, 100);
                }

                $new_array_qty = implode(',',$insert_qty_array);

                mysqli_query($db_conx, "UPDATE category SET qty_type = '".$new_array_qty."' WHERE category_id = '".(int)$category_id."'");
            }
           
            return $inserted_product;
        }
        else{
            //echo "<script type=\"text/javascript\">alert('No data for submit');</script>";
            return $error = 'No data for submit';
        }
    }

function username_exists($username){
        global $db_conx;

        if($username != ""){
            $sql = mysqli_query($db_conx, "SELECT customer_id FROM customer WHERE username = '".$username."'");
            if(mysqli_num_rows($sql)>=1){   
                return true;
            }else{
                return false;
            }
        }
        else
        {
            return true;
        }
    }

function email_exists($email){
        global $db_conx;

        if($email != ""){
            $sql = mysqli_query($db_conx, "SELECT customer_id FROM customer WHERE email = '".$email."'");
            if(mysqli_num_rows($sql)>=1){   
                return true;
            }else{
                return false;
            }
        }
        else
        {
            return true;
        }
    }
   
    function validateEmail($email){
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            return false;
        }
        else
        {
            return true;
        }
    }

function get_sha512_hash($string){
        return hash("sha512",($string));
    }

function get_alias($alias)
    {
        $alias = trim(strtolower(string_sanitize($alias)));
        $alias = preg_replace("/^\.+|\.+$/", "-", $alias);
        $alias = str_replace(array(':', '\\', '/', '---','--', '-', '__', '_', '  ',  '*', '..', '.',' ', '+', '!', '=', '(', ')',' &amp; ',' &amp;','&amp;', ' & ', '&', '%', '"', ';', '#', '$','!','@','^','?','<','>'), '-', $alias);
        return $alias;
    }
    function check_alias($table_name, $value)
    {
        global $db_conx;
        $select = mysqli_query($db_conx, "SELECT alias FROM ".$table_name." WHERE alias = '".$value."'");
        if(mysqli_num_rows($select) == 0)
        {
            return $value;
        }
        else
        {
            $new_value = $value.'2';
            return check_alias($table_name, $new_value);
        }
    }
    function normal_sanitize($string){
        global $db_conx;
        return mysqli_real_escape_string($db_conx, str_replace("'","",stripslashes(htmlentities(strip_tags($string)))));
    }
    function addadmin($fullname,$emailid,$password,$role,$status)
    {
        global $db_conx; global $current_date;
        if ($fullname != "" && $emailid != "" && $password != "" && $role != "" && $status != ""){
            $sql = "INSERT INTO admin(fullname, emailid, password, role, status, dateadded) VALUES ('".$fullname."', '".$emailid."', '".$password."', '".(int)$role."', '".(int)$status."', '".$current_date."')";
            mysqli_query($db_conx, $sql);
            return mysqli_insert_id($db_conx);
        }
    }

function humanTiming($time)
    {
        $time = time() - $time; // to get the time since that moment
        $time = ($time<1)? 1 : $time;
        $tokens = array (
            31536000 => 'year',
            2592000 => 'month',
            604800 => 'week',
            86400 => 'day',
            3600 => 'hour',
            60 => 'minute',
            1 => 'second'
        );
        foreach ($tokens as $unit => $text) {
            if ($time < $unit) continue;
            $numberOfUnits = floor($time / $unit);
            return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
        }
    }

function sendmail($to, $from, $subject, $message){
       
        require('PHPMailer/class.phpmailer.php');

        if($to != "" && $from != "" && $subject != "" && $message != ""){
            try {
                  $mail = new PHPMailer(true);
                  $mail->IsSMTP();  // Using SMTP.
                  $mail->Host       = "mail.sitename.com"; // SMTP server
                  $mail->CharSet = 'utf-8';
                  $mail->SMTPAuth   = false; // Enables SMTP authentication.
                  $mail->IsSendmail();  // tell the class to use Sendmail

                  $mail->AddAddress($to);
           
                  $mail->From       = $from;
                  $mail->FromName   = 'websitenmfunction date_sanitize($date){
        global $db_conx;
       
        return mysqli_real_escape_string($db_conx, str_replace("'","",$date));
    }
   
    function getAge($then) {
        $then = date('Ymd', strtotime($then));
        $diff = date('Ymd') - $then;
        return substr($diff, 0, -4);
    }


//for gernrate token
    function get_alphanumeric(){return substr(md5(mt_rand().time()),0,20);}
   
    function run_code()
    {
        global $db_conx;
        $temp_token = get_alphanumeric();
        $sql51 = "select token from user WHERE token = '".$temp_token."'";
        $result51 = mysqli_query($db_conx,$sql51);
        if(mysqli_num_rows($result51) >= 1)
        {
             run_code();
        }
        else
        {
            return $temp_token;
        }
    }

    function get_activate_code()
    {
        global $db_conx;
        $temp_token = get_alphanumeric();
        $sql51 = "select token from user WHERE activate = '".$temp_token."'";
        $result51 = mysqli_query($db_conx,$sql51);
        if(mysqli_num_rows($result51) >= 1)
        {
             get_activate_code();
        }
        else
        {
            return $temp_token;
        }
    }
    

function remove_html_tags($string){
        global $db_conx;
        if($string !="" ){
            return strip_tags($string);
        }
    }
    function display_50_character($string){
        global $db_conx;
        if($string !=""){
            return substr($string, 0, 50);
        }
    }
    function remove_r_n($string){
        global $db_conx;
        if($string !=""){
            $text = str_ireplace(array("\r","\n",'\r','\n'),'', $string);
             return str_replace(PHP_EOL, '', $text);
        }
    }

function preg_trim($subject) {
    $regex = "/\s*(\.*)\s*/s";
    if (preg_match ($regex, $subject, $matches)) {
        $subject = $matches[1];
    }
    return $subject;
}

function isValidEmail($email){
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

?>

How to include file :


include("files/connect.php");
include("files/functions.php");
session_start();

No comments:

Post a Comment