Thursday, August 24, 2023

Sorting numbers without using inbuilt function

 $result = ['9','2','5','4','3','7'];

for ($i = 0; $i < count($result) - 1; $i++) {

  for ($j = $i + 1; $j < count($result); $j++) {

    if ($result[$i] > $result[$j]) {

      $temp = $result[$i];

      $result[$i] = $result[$j];

      $result[$j] = $temp;

    }

  }

}


echo "<pre>";print_r($result);echo "</pre>";

OUTPUT:

Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 7
    [5] => 9
)

Encrypt and decrypt password method with key

 Hello Today I saw you how to encrypt and decrypt password.

core PHP:

define ("SECRETKEY", "MyApplicationResult");
function encryptedPassword($password){
return openssl_encrypt($password, "AES-128-ECB", SECRETKEY);
}
function decryptedPassword($password){
return openssl_decrypt($password, "AES-128-ECB", SECRETKEY);
}


CodeIgniter 4:

 public function encrypt_pass($simple_string){
       
        // Store the cipher method
        $ciphering = "AES-128-CTR";
        // Use OpenSSl Encryption method
        $iv_length = openssl_cipher_iv_length($ciphering);
        $options = 0;
        // Non-NULL Initialization Vector for encryption
        $encryption_iv = '1234567891011121';
        // Store the encryption key
        $encryption_key = "MySecretKey";
        return $encryption = openssl_encrypt($simple_string, $ciphering, $encryption_key, $options, $encryption_iv);
     }

    public function decrypt_pass($encryption){
        // Store the cipher method
        $ciphering = "AES-128-CTR";
        // Use OpenSSl Encryption method
        $iv_length = openssl_cipher_iv_length($ciphering);
        $options = 0;
        // Non-NULL Initialization Vector for encryption
        $decryption_iv = '1234567891011121';
        // Store the encryption key
        //$decryption_key = $this->websitekey;
        $decryption_key = "MySecretKey";
        return $decryption=openssl_decrypt ($encryption, $ciphering, $decryption_key, $options, $decryption_iv);
    }

Tuesday, August 22, 2023

Get Browser name, version and operating system name in php

Hello.

Recently I want to know about user's details. SO I use these code for geting browser name, version and operating system name.

Output: 
Browser Name: Google Chrome
Browser Version: 116.0.0.0
Operating System: Windows 10

Code:

$userAgent = $_SERVER['HTTP_USER_AGENT'];

function getBrowserVersion($userAgent, $browserName) {
    $pattern = "";
    switch ($browserName) {
        case "Internet Explorer":
            $pattern = '/MSIE|Trident\/.*rv:([\d\.]+)/';
            break;
        case "Mozilla Firefox":
            $pattern = '/Firefox\/([\d\.]+)/';
            break;
        case "Microsoft Edge":
            $pattern = '/Edge\/([\d\.]+)/';
            break;
        case "Google Chrome":
            $pattern = '/Chrome\/([\d\.]+)/';
            break;
        case "Apple Safari":
            $pattern = '/Version\/([\d\.]+)/';
            break;
        case "Opera":
            $pattern = '/Opera\/([\d\.]+)/';
            break;
    }

    if (preg_match($pattern, $userAgent, $matches)) {
        return $matches[1];
    }

    return "Unknown";
}

function getBrowserInfo($userAgent) {
    if (preg_match('/MSIE|Trident/i', $userAgent)) {
        return "Internet Explorer";
    } elseif (preg_match('/Firefox/i', $userAgent)) {
        return "Mozilla Firefox";
    } elseif (preg_match('/Edge/i', $userAgent)) {
        return "Microsoft Edge";
    } elseif (preg_match('/Chrome/i', $userAgent)) {
        return "Google Chrome";
    } elseif (preg_match('/Safari/i', $userAgent)) {
        return "Apple Safari";
    } elseif (preg_match('/Opera/i', $userAgent)) {
        return "Opera";
    }

    return "Unknown";
}

function getOperatingSystem($userAgent) {
    $os = "Unknown";

    if (preg_match('/Windows NT 10/i', $userAgent)) {
        $os = "Windows 10";
    } elseif (preg_match('/Windows NT 6.3/i', $userAgent)) {
        $os = "Windows 8.1";
    } elseif (preg_match('/Windows NT 6.2/i', $userAgent)) {
        $os = "Windows 8";
    } elseif (preg_match('/Windows NT 6.1/i', $userAgent)) {
        $os = "Windows 7";
    } elseif (preg_match('/Windows NT 6.0/i', $userAgent)) {
        $os = "Windows Vista";
    } elseif (preg_match('/Windows NT 5.1/i', $userAgent) || preg_match('/Windows XP/i', $userAgent)) {
        $os = "Windows XP";
    } elseif (preg_match('/Windows NT 5.0/i', $userAgent)) {
        $os = "Windows 2000";
    } elseif (preg_match('/Windows NT 4.0/i', $userAgent)) {
        $os = "Windows NT 4.0";
    } elseif (preg_match('/Macintosh|Mac OS X/i', $userAgent)) {
        $os = "Mac OS X";
    } elseif (preg_match('/Linux/i', $userAgent)) {
        $os = "Linux";
    } elseif (preg_match('/Unix/i', $userAgent)) {
        $os = "Unix";
    } elseif (preg_match('/Ubuntu/i', $userAgent)) {
        $os = "Ubuntu";
    }

    return $os;
}
$operatingSystem = getOperatingSystem($userAgent);

$browserName = getBrowserInfo($userAgent);
$browserVersion = getBrowserVersion($userAgent, $browserName);

echo "Browser Name: " . $browserName . "<br>";
echo "Browser Version: " . $browserVersion;

echo "<br>Operating System: " . $operatingSystem;