Archive for the ‘PHP’ Category

How to force download in php

Create a new php file and copy and paste following script, then change the filename of the file to be downloaded.

  1. <?PHP
  2.  
  3. // HTTP Header
  4. header('Content-disposition: attachment; filename=applicationform.pdf');
  5.  
  6. // MIME0type
  7. header('Content-type: application/pdf');
  8.  
  9. // read the file
  10.  
  11. readfile('applicationform.pdf');

References : How To Use PHP to Force a File Download

,

No Comments


The following text is not allowed in this context:

This exception occurs when there is an extra string / character outside the element of the schema,

example:

schema_error

,

No Comments


Windows7 error access forbidden with xampp

goto windows\system32\drivers\etc\hosts, then, move the line with # ::1 localhost to the bottom

or

  1. Navigate to your xampp\apache\conf\extra folder
  2. Using a text editor, open httpd-xampp.conf
  3. Find the following line:

    Allow from localhost

  4. Change the line to:

    Allow from 127.0.0.1

  5. Save the file and restart your XAMPP server
  6. Try to access http://localhost/security/lang.php?en

You should now have no problems accessing the XAMPP Security Page via localhost or 127.0.0.1

, ,

No Comments


Save, resize image using php
image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }
}
?>

source

, , , , , ,

No Comments


How to access variables or methods of the parent class in php

use the double colon(::) and parent keyword to access the variables or methods of the parent class in php

  1. class myparent {
  2. function myfunction() {
  3. echo "parent method";
  4. }
  5. }
  6.  
  7. class mychild extends myparent {
  8. function myfunc()[
  9. parent::myfunction();
  10. }
  11. }

, , , ,

No Comments


How to access Static variables in php

To access Static variables;

  1. class mystatic {
  2. private static $staticvar;
  3.  
  4. public static function staticmethod{
  5. return <strong>self::$staticvar</strong>;
  6. }
  7. }

, ,

No Comments


How to generate random password in php
  1. function get_randomString($count=8, $rm_similar = false){
  2.  //create list of characters
  3.  
  4.  $chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));
  5.  
  6.  // remove similar looking characters that might cause confusion
  7.  if ($rm_similar) {
  8.   unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8], $chars['B'],
  9.     $chars['I'], $chars['O'], $chars['Q'], $chars['S'], $chars['U'], $chars['V'], $chars['Z']);  
  10.  }
  11.  
  12.  // generate the string of random text
  13.  
  14.  for( $i = 0, $text = '';  $i < $count; $i++ ) {
  15.   $text .= array_rand($chars);
  16.  }
  17.  return $text;
  18. }

,

No Comments


What @ symbol in PHP

@ symbol – is used to suppress warning message.

,

No Comments


Difference between $this vs self in class using php

$this – use this if you are accessing a variable or method without static keyword within a class
self - use this if you are accessing a static variable or constant within a class

No Comments


Accessing Static Variable within Static method in a Class

Use self to access static variable or const within a class.
example:

  1. class static_class {
  2.  
  3.             private static static_variable = 'I am static ';
  4.             public static function getStaticVariable() {
  5.                  self::$static_variable;
  6.             }
  7. }

, , ,

No Comments



SetPageWidth