Prevent Multiple Inserts When Submitting a Form in PHP

AmitDiwan
Updated on 09-Apr-2020 13:40:58

2K+ Views

PHP session can be used to prevent multiple inserts when submitting a form. PHP session sets a session variable (say $_SESSION['posttimer']) that sets the current timestamp on POST. Before processing the form in PHP, the $_SESSION['posttimer'] variable is checked for its existence and checked for a specific timestamp difference (say 2 or 3 seconds). This way, those insertions that are actually duplicates can be identified and removed.Simple form −// form.html         The reference to ‘my_session_file.php’ in the above will have the below lines of code −Exampleif (isset($_POST) && !empty($_POST)) {    if (isset($_SESSION['posttimer'])) {     ... Read More

When to Use the ServiceLoader Class in Java 9

raja
Updated on 09-Apr-2020 12:22:21

283 Views

Java has a ServiceLoader class from java.util package that can help to locate service providers at the runtime by searching in the classpath. For service providers defined in modules, we can look at the sample application to declare modules with service and how it works.For instance, we have a "test.app" module that we need to use Logger that can be retrieved from System.getLogger() factory method with the help of LoggerFinder service.module com.tutorialspoint.test.app {    requires java.logging;    exports com.tutorialspoint.platformlogging.app;    uses java.lang.System.LoggerFinder; }Below is the test.app.MainApp class:package com.tutorialspoint.platformlogging.app; public class MainApp {    private static Logger LOGGER = System.getLogger();    public static void ... Read More

Casting Variable as Object Type in Foreach Loop in PHP

AmitDiwan
Updated on 09-Apr-2020 11:44:45

977 Views

This depends on the IDE that is being used. For example, Netbeans and IntelliJ can enable the usage of @var in a comment −/* @var $variable ClassName */ $variable->This way, the IDE would know that the ‘$variable’ is a class of the ClassName after the hint ‘->’ is encountered.In addition, an @return annotation can be created with a method that specifies that the return type will be an array of ClassName objects. This data can be accessed using a foreach loop that fetches the values of the objects −function get_object_type() {    return $this->values; } foreach( $data_object-> values as $object_attribute ... Read More

Sort PHP Multidimensional Array by Sub Value in PHP

AmitDiwan
Updated on 09-Apr-2020 11:43:24

365 Views

The ‘usort’ function can be used to sort multidimensional arrays in PHP. It sorts an array based on a user-defined criteria −Example Live DemoOutputThis will produce the following output −2 4 63 81An array with 4 elements is declared and this array is passed to the usort function, as well as calling the user-defined ‘my_sort’ function on the elements to make sure that the sorting takes place is ascending order.

Add Object Element to Array in PHP

AmitDiwan
Updated on 09-Apr-2020 11:41:50

3K+ Views

The code is as follows −Example$object = new stdClass(); $object->name = "My name"; $myArray[] = $object;OutputThis will produce the following output −Suppose myArray already contains ‘a’ and ‘c’, the value of “My name” will be added to it. It becomes Array {    a:0, c:1, “My name”:2 }The object is created and then it is pushed to the end of the array (that was previously present).Alternative$myArray[] = (object) ['name' => 'My name'];

Detect Language from String in PHP

AmitDiwan
Updated on 09-Apr-2020 11:40:36

1K+ Views

The language can’t be detected from the character type. There are other ways, but they don’t guarantee complete accuracy. The ‘TextLanguageDetect Pear Package’ can be used with decent accuracy. Below is a sample code for the same −Examplerequire_once 'Text/LanguageDetect.php'; $l = new Text_LanguageDetect(); $result = $l->detect($text, 4); if (PEAR::isError($result)) {    echo $result->getMessage(); } else {    print_r($result); }OutputThis will produce the following output −Array (    [german] => 0.407037037037    [dutch] => 0.288065843621    [english] => 0.283333333333    [danish] => 0.234526748971 )It is easy to use and has 52 language databases. But the downside is that the Eastern Asian languages can’t be detected using this package.

Read Last 5 Lines of a Text File in PHP

AmitDiwan
Updated on 09-Apr-2020 11:39:18

625 Views

To read only 5 last lines of the text file, the code is as follows −Example$file = file("filename.txt"); for ($i = max(0, count($file)-6); $i < count($file); $i++) {    echo $file[$i] . ""; }OutputThis will produce the following output −Given that the file has more than 5 lines of text, the last 5 lines of the text file will be displayed.The file is opened and the number of lines in the file are counted, and beginning from the last line, 5 lines are read.

Download Large Files Through PHP Script

AmitDiwan
Updated on 09-Apr-2020 11:37:51

3K+ Views

To download large files through PHP script, the code is as follows −ExampleOutputThis will produce the following output −The large file will be downloaded.The function ‘readfile_chunked’ (user defined) takes in two parameters- the name of the file and the default value of ‘true’ for the number of bytes returned meaning that large files have been successfully downloaded. The variable ‘chunksize’ has been declared with the number of bytes per chunk that needs to be read. The ‘buffer’ variable is assigned to null and the ‘cnt’ is set to 0. The file is opened in binary read mode and assigned the ... Read More

Make PHP pathinfo Return the Correct Filename for UTF-8

AmitDiwan
Updated on 09-Apr-2020 11:35:56

305 Views

Most of the core PHP functions don’t deal with character sets apart from Latin-1. But before the ‘pathinfo’, placing the ‘setlocale’ can be used to return the correct filename even if it is UTF-8 encoded.By default, it runs with ‘C’ locale, and CLI scripts run with a default utf-8 locale. The locale on the server should be changed from ‘C’ to ‘C.UTF-8’ or to ‘en_US.UTF-8’ before calling the other functions.setlocale(LC_ALL,'en_US.UTF-8'); pathinfo($OriginalName, PATHINFO_FILENAME); pathinfo($OriginalName, PATHINFO_BASENAME);

Iterate Through DOM Elements in PHP

AmitDiwan
Updated on 09-Apr-2020 11:34:16

1K+ Views

Following is the XML data (input) −                       Iterating through the elements in the DOM object.Example$elements = $dom->getElementsByTagName('foo'); $data = array(); foreach($elements as $node){    foreach($node->childNodes as $child) {       $data[] = array($child->nodeName => $child->nodeValue);    } }OutputThis will produce the following output −Every ‘foo’ tag will be iterated over and specific ‘bar’ and ‘pub’ value will be obtained, i.e specific child nodes can be accessed by their names itself.The elements in the XML file are obtained by ... Read More

Advertisements