Tag Archives: php

Does we can find the browser capabilities in PHP ?

Yes, we can find the browser capabilities in php using get_browser() function . it is a inbuilt function
in PHP. It returns an object or an array with information about the user’s browser on success, or
FALSE on failure.
The get_browser() function has 2 parameters and are optional parameter, they are given bellow :-
 user_agent : it is utilized to specify the name of an HTTP user agent.
 return_array : it return an array instead of an object if the value is set to true.

Syntax : get_browser(user_agent, return_array)

Example :


<?php
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>

Output :
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/98.0.4758.102 Safari/537.36

What is Exception Handling in PHP ?

Exception handling in php  is used to modify the normal flow of code execution if a specified error occurs. When an exception is given , the following  code will not be executed, and PHP will try to find the appropriate “catch” block. If an exception is not caught, a fatal error will be displayed.

Keywords in Exception Handling:

  • try : This is a block of code where an exception may occur.
  • catch: It represent block of code that will be executed when a particular exception has been thrown.
  • throw: It is used to throw an exception. It is also used to list the exceptions that a function throws, but doesn’t handle itself. Each “throw” must have at least one “catch”.
  • finally: It is used in place of catch block or after catch block basically it is put for cleanup activity in PHP code.

Example :

<?php
function checkMarkStatus($mark) {
  if($mark>=5) {
    throw new Exception("The Student is Pass.");
  }
  return true;
}
try {
  checkMarkStatus(5);
  echo 'The Student is Fail';
}
catch(Exception $e) {
  echo $e->getMessage();
}
?>

Advantages of Exception Handling :

  • Sometimes error handling is very difficult for the developer to understand in normal coding. With try catch block it is more readable and understandable.
  • Exceptions allow you to distinguish between different types of errors, and is also great for routing.
  • The thrown exception can then be handled by a custom error handler.

How To Use Ternary Operator Instead Of if else Condition in php

Instead of writing long lines for if condition we can short it into a single line. Here we are using ternary operator as a short-hand method of if-else condition in PHP. Therefore, the ternary operator has three operands. We write the condition at the beginning, which is the first operand. Then the symbol ‘?’ separates the condition with the expression to be executed if the condition is true. Finally, the ‘ : ‘  symbol separates the true and the false expression. The syntax is written below.

Syntax :

condition ? trueExpression : falseExpression

Example :

$text =”abc”;
$word = ($text == 'bc') ? "true" : "false";
echo $word;

Special functions in PHP

Lets discuss some special functions in PHP. In PHP, we can able TO define some special functions that will be called automatically. Those functions not required function call to execute the code inside these functions.

  1. Die()
  2. Sleep()
  3. Var_dump()
  4. Print_r()
  5. Exit

Die()

  • Inbuilt php function.
  • Prints the message and terminate the php script.
  • It accepts only single parameter and which is not mandatory to be passed .

Syntax : die($message);

Sleep()

  • Inbuilt php function.
  • It accepts only single parameter and which is mandatory to be passed.
  • Used to delay the execution of the program for given number of seconds.
  • It returns zero on success, or FALSE on error. This function returns a non-zero value if the call was interrupted by a signal.

Syntax : int sleep(int $seconds)

Var_dump

  • Inbuilt php function.
  • dumps information about one or more variables.
  • includes the data type and value of the variable.
  • In case of string, it also includes the size of the string passed inside the function.
  • The array and object are explored recursively with values to show their structure.

Syntax: var_dump(var1, var2, …);

  • Inbuilt php function.
  • displays information about a variable in a human-readable way.
  • It shows the information stored in a variable, which is easily understandable to the user.
  • Used to print an array.

Syntax : print_r(variable);

Exit

  • Inbuilt php function.
  • terminates the execution of the script.
  • The shutdown functions and object destructors will always be executed even if exit() function is called.
  • act as an alias of the die() function.

Syntax :  exit(message);

Open Source file manager : elFinder

elFinder is an Open source file manager for the web written in JavaScript using the jQuery user interface.

  • User-friendliness like MacOS Finder or Windows Explorer
  • Mobile view for touch devices
  • All operations with files and folders on a remote server (copy, move, download, create folder / file, rename, etc.)
  • High performance backend server and lightweight client user interface
  • Multi-root support
  • Local file system, MySQL, FTP, Box, Dropbox, GoogleDrive, and OneDrive volume storage drivers
  • Supports AWS S3, Azure, Digital Ocean Spaces and more with League \ Flysystem Flysystem driver
  • Cloud storage drivers (Box, Dropbox, GoogleDrive and OneDrive)
  • Background file / folder upload with HTML5 drag and drop support
  • Fragmented file download for a large file
  • Download directly to the folder
  • Download the form URL (or the list)
  • List View and Icons
  • Keyboard shortcuts
  • Standard methods of selecting files / groups using mouse or keyboard
  • Move / copy files by drag and drop
  • Drag and drop outside starting by dragging with the Alt / Option key
  • Create / extract archives (zip, rar, 7z, tar, gzip, bzip2)
  • Rich context menu and toolbar
  • Quicklook, overview of common file types
  • Edit text files and images
  • “Places” for your favorites
  • Calculate the size of directories
  • Thumbnails for images, video files
  • Easy to integrate with web editors (elRTE, CKEditor, TinyMCE)
  • Flexible configuration of access rights, download file types, user interface and more
  • Extensibility through backend and client side event management
  • Simple client-server API based on JSON
  • Support customized information in the information dialog
  • Configurable columns of the list view
  • Support custom CSS class function for custom folder icon
  • Connector plug-in
  • Auto Rotate: Auto rotate when uploading JPEG files by EXIF ​​orientation.
  • Auto Resize: Auto resize when uploading files.
  • Normalizer: UTF-8 normalizer of filename and path to file, etc.
  • Sanitizer: disinfectant of file name and file path, etc.
  • Watermark: Print the watermark when downloading the file.

Google Recaptcha in CodeIgniter

It is one of the major discussed question, how we can use google recaptcha in codeigniter.First we should need a google account to start. Login the corresponding gmail and access https://www.google.com/recaptcha/admin from your browser. Next you have to register the domain. After that you will get a site key and secret key.

Herecomes the our coding part, insert following part in header

<script src='https://www.google.com/recaptcha/api.js'></script>

For showing the recpatcha section, use the below html code. Complete the code by using your data site key.

<div class="g-recaptcha" data-sitekey="ENTER YOUR SITE KEY"></div>

Add the secret key in your controller with following code.

$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
            $secret='ENTER THE SECRET KEY';
       
            $credential = array(
                    'secret' => $secret,
                    'response' => $this->input->post('g-recaptcha-response')
                );
                $verify = curl_init();
                curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
                curl_setopt($verify, CURLOPT_POST, true);
                curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($credential));
                curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
                $response = curl_exec($verify);
           
                $status= json_decode($response, true);
                if($status['success'])
            {
			 echo “sucesss”;
			}
                 Else{
			  Echo “fail”;
			}