How to prevent direct file access from a directory on server?

Friends, Here is a simple approach to prevent the direct file access from a directory on a server.

To do this we need to create a file .htaccess and just write a single line of code:

To prevent direct file access put the .htaccess file with the above line of code in the directory containing the file.

Sample Directory Without .htaccess File:
server1

Output when accessing the pages/page1.html:
sout1

Sample Directory With .htaccess File:
server3

Output when accessing the pages/page1.html:
sout2

To prevent direct file access from a directory, just place the .htaccess file with following lines of text i.e. “Deny From All” in that directory.

PHP implode and explode functions

PHP implode() function is used to join array elements with a string.

This function can be used whenever you want to represent an array in the form of string separated by some or any delimiter.

Example:

OUTPUT:
orange, grapes, banana, apple

 
PHP explode() function is used to fetch or get an array of elements from a string separated by some delimiter.

Example:

OUTPUT:
orange
grapes

Magento Basic Requirements Check

Magento Basic Requirements Check

The following code can help us to check whether our system meets the basic requirements for magento installation or not. To use this code copy and paste it into a file with .php extension and put the file in the root directory of your server and run it from there. The same file can be downloaded from magento website.

Read And Write From a File Line by Line in PHP

Hello Friends,
This is a simple example which demonstrates how one can write into a file line by line data in json format and read from that file again line by line.

1. Create a file index.php and write the following code there.
require_once(‘db.php’);

$db=new db();

$db->insert(“insert into tablename values(‘sudhanshu’, ’22’, ‘male’)”);
$result=$db->readandexecute();
if($result){
    foreach($result AS $k => $re){
        if($k!=” && $re!=”){
        echo “

”;
        }
    }
}else{
    echo ‘na’;
}

2. Create a file db.php

class db{
 public function insert($query){
              $this->filestore($query);
 }
 private function filestore($q, $qstatus=true){
              global $profiler;
              $arr=explode(‘ ‘, trim($q));
              $getfirst=strtolower($arr[0]);
              $gethird=strtolower($arr[2]);
             
              if($getfirst==’show’ || $getfirst==’select’ || $gethird==’errorlog’){
                  return false;
              }
              if($qstatus==true){
              $filedir=’.’.’\\’.’dblogs’;
              $filename=’log.txt’;
              $fileaddress=$filedir.’\\’.$filename;
              $query[‘id’]=uniqid();
              $query[‘query’]=$q;
              $query[‘status’]=’unsynchronized’;
             
              if(!file_exists($filedir)) //part A
              {
                    mkdir($filedir);//directory created
              }
              if(!file_exists($fileaddress)){
                  $fhandle= fopen($fileaddress,’a’);
                  fclose($fhandle);
              }
                  // Let’s make sure the file exists and is writable first.
              if (is_writable($fileaddress)) {
               $profiler->add(‘Now Storing the queries in File[‘.$fileaddress.’]’);
                      // The file pointer is at the bottom of the file hence
                      // that’s where $filecontent will go when we fwrite() it.
                        if (!$handle = fopen($fileaddress, ‘a’)) {
                        echo “Cannot open file (“.$fileaddress.”)”;
                        return FALSE;
                        }
                 
                         // Write $filecontent which is of array type and stored in $vals to our opened file.
                         if (fwrite($handle, json_encode($query).”\r\n”) === FALSE) {
                        // echo “Cannot write to file ($fileaddress)”;
                         }
                         fclose($handle);
                         $profiler->add(‘Error Stored in File[‘.$fileaddress.’]’);
                         return TRUE;
                  }
                   $profiler->add(‘File [‘.$fileaddress.’] not found.’);
        return FALSE;
              }
    }
 public function readandexecute(){
              return $this->getqueriesfromfile();
      }
      public function getqueriesfromfile(){
              $filedir=’.’.’\\’.’dblogs’;
              $filename=’log.txt’;
              $fileaddress=$filedir.’\\’.$filename;
              $matches=array();
             
                $file = fopen($fileaddress, “r”);
                //Output a line of the file until the end is reached
                while(!feof($file))
                {
                    $lines=fgets($file);
                    $jsonarray=json_decode($lines, true);
                    if($jsonarray[‘status’]!=’synchronized’){
                        $matches[$jsonarray[‘id’]]=$jsonarray[‘query’];
                       // fputs($file, json_encode(array(‘id’=>$jsonarray[‘id’])))
                    }
   
                }
                fclose($file);
                return $matches;
      }
}

How to concatenate two or more arrays in PHP?

This is one of the example which are simple enough to implement but sometimes we make so many efforts to achieve this but we didn’t succeed at the end. I have suffered with this kind of problem thats why am going to post the entire code to implement array concatenation. You can use implode and explode functions at numerous places but not at all places to make array concatenation look at the following example.

What We want to achieve?

S.No.            Student’s Name                Roll No.
1                   Sudhanshu                        1
2                   Rohin                                2

To show or get output like this we can code in the following way:


$name=array();
$name[]="S.No.";
$name[]="Student's Name";
$name[]="Roll No.";

$name1 = array(); 
$name1[] = 1; 
$name1[] = 'Sudhanshu'; 
$name1[] = 1; 

$name2 = array(); 
$name2[] = 2; 
$name2[] = 'Rohin'; 
$name2[] = 2; 
 

S.No. Student’s Name Roll No.
1 Sudhanshu 1
2 Rohin 2