Error message “Forbidden You don’t have permission to access / on this server”

By default, global directory settings is (httpd.conf line ~188):

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
</Directory>

set the options to : Options Indexes FollowSymLinks Includes ExecCGI

Finally, it should look like:

<Directory />
    #Options FollowSymLinks
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order deny,allow
    Allow from all
</Directory>
Advertisements

General – Type safe coding – 1

The issue: In general PHP is loosely typed.
<?php
$foo = "1";
$bar = $foo + 1;
var_dump($foo, $bar);
?>
Output
string(1) “1” int(2)
The normal comparison operators are loosely typed, too.
<?php
$int = 1;
$string = "1";
$bool = true;
var_dump($int == $string);
var_dump($string == $bool);
var_dump($int == $bool);
?>
Output
bool(true) bool(true) bool(true)
This can cause WTF situations
<?php
var_dump( '1' == '1.' );
?>
Output
bool(true)
This may lead to very common mistakes:
<?php
function foo($answer) {
if ($answer > 10) {
return true;
} else {
return $answer;
}
}
if (foo(11)) {
echo "11 is greater 10<br />";
}
if (foo(9)) {
echo "9 is greater than 10<br />";
}
?>
Output

11 is greater 10
9 is greater than 10

Referenced By http://talks.php.net/show/php-best-practices/

Command Line Application Using php

To find the longitude and latitude of city/state/address from user input string

fwrite(STDOUT, "Please enter your city state or address\n");
$address = fgets(STDIN);
//fwrite(STDOUT, "Welcome $name");
//$address = 'Kolhapur tarbai park'; // Google HQ
$prepAddr = str_replace(' ','+',$address);

$geocode=file_get_contents(‘http://maps.google.com/maps/api/geocode/json?address=&#8217;.$prepAddr.’&sensor=false’);

$output= json_decode($geocode);

$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;

fwrite(STDOUT, $address.’
Lat: ‘.$lat.’
Long: ‘.$long);

 

Output

output

PDF Report Generation Using fpdf,Mysql and php


define('path','fpdf/font/');
require('fpdf/fpdf.php');
if(isset($_REQUEST['submit']))
{
//Connect to your database
//include("conectmysql.php");
$today=date('Y-m-d');
$hostname = "localhost";
$database = "todo";
$username = "";
$password = "";
$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($database, $conn);
//Create new pdf file
$pdf=new FPDF();

//Open file
$pdf->Open();

//Disable automatic page break
$pdf->SetAutoPageBreak(false);

//Add first page
$pdf->AddPage();

//set initial y axis position per page
$y_axis_initial = 25;

//print column titles for the actual page
$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(25);
$pdf->Cell(30, 6, 'Complaint', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Date', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Text', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Polar words', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Source', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Company id', 1, 0, 'R', 1);

$y_axis = $y_axis + $row_height;

//Select the Products you want to show in your PDF file
$fromdate=$_REQUEST['fromdate'];
$todate=$_REQUEST['todate'];
$result=mysql_query("SELECT * FROM sd WHERE date BETWEEN '$fromdate' AND '$todate'") or die(mysql_error());

//initialize counter
$i = 0;

//Set maximum rows per page
$max = 25;

//Set Row Height
$row_height = 6;

while($row = mysql_fetch_array($result))
{
//If the current row is the last one, create new page and print column title
if ($i == $max)
{
$pdf->AddPage();

//print column titles for the current page
$pdf->SetY($y_axis_initial);
$pdf->SetX(25);

$pdf->Cell(30, 6, 'Complaint', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Date', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Text', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Polar words', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Source', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Company id', 1, 0, 'R', 1);

//Go to next row
$y_axis = $y_axis + $row_height;

//Set $i variable to 0 (first row)
$i = 0;
}

$complainant = $row['complainant'];
$date = $row['date'];
$complainttext = $row['complainttext'];

$complainttitle = $row['complainttitle'];
$polarwords = $row['polarwords'];
$source = $row['source'];
$companyid = $row['companyid'];

$pdf->SetY($y_axis);
$pdf->SetX(25);
$pdf->Cell(30, 6, $complainant, 1, 0, 'L', 1);
$pdf->Cell(30, 6, $date, 1, 0, 'L', 1);
$pdf->Cell(30, 6, $complainttext, 1, 0, 'L', 1);
$pdf->Cell(30, 6, $polarwords, 1, 0, 'L', 1);
$pdf->Cell(30, 6, $source, 1, 0, 'L', 1);
$pdf->Cell(30, 6, $companyid, 1, 0, 'R', 1);
// $pdf->Cell(30, 6, $companyid, 1, 0, 'R', 1);

//Go to next row
$y_axis = $y_axis + $row_height;
$i = $i + 1;
}

//Create file
$pdf->Output('report.pdf','F');
/*
if($pdf->Output('report.pdf','F'))
{
echo "Report Created Successfyully CLick to view";
}
else
{
echo "failed to create Report";
}*/
}
?>

   

Read .docx File Using PHP


<?php
function read_file_docx($filename){

$striped_content = '';
$content = '';

if(!$filename || !file_exists($filename)) return false;

$zip = zip_open($filename);

if (!$zip || is_numeric($zip)) return false;

while ($zip_entry = zip_read($zip)) {

if (zip_entry_open($zip, $zip_entry) == FALSE) continue;

if (zip_entry_name($zip_entry) != "word/document.xml") continue;

$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

zip_entry_close($zip_entry);
}// end while

zip_close($zip);

//echo $content;
//echo "


";
//file_put_contents('1.xml', $content);

$content = str_replace('', " ", $content);
$content = str_replace('', "\r\n", $content);
$striped_content = strip_tags($content);

return $striped_content;
}

$filename = "SAMPLEFILE.docx"; //Add file with folder

$content = read_file_docx($filename);
if($content !== false) {

echo nl2br($content);
}
else {
echo 'Couldn\'t the file. Please check that file.';
}

?>

PDF TO TEXT conversion using php

Create a pdf.php file and copy below code

 


<?php
define( 'PDF_PATTERN_SEPARATOR', ' \n\r\/\()\[\]' );
define( 'PDF_PATTERN_WHITESPACE', ' \n\r' );

class pdf_readstream
{
var $data;
var $offset;
var $size;
var $allow_references;

function pdf_readstream( &$data, $offset=0 )
{
$this->data = trim($data);
$this->offset = $offset;
$this->size = strlen($this->data);
}

function read_object()
{
$this->skip_whitespace();

#echo $this->offset."\n";

switch( $this->get_next() )
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
case '-':
case '+':
// number, object, reference

$number = $this->read_while("0123456789+-.");

if( $this->allow_references )
{
if( $this->expect(' 0 R') )
{
return $this->mark( new pdf_reference( $number.' 0' ) );
}
else if( $this->expect(' 0 obj') )
{
$value = $this->read_object();

$this->skip_whitespace();

$this->offset += 6;

#$endobj = $this->read(6);

#if( $endobj != 'endobj' )
#{
# echo 'Unknown object data:'.$this->get_next(20)." at offset ".$this->offset."\n";
# exit;
#}

#$value = new pdf_indirect_object( $value );

if( is_object( $value ) )
return $this->mark( $value );
else
return $value;
}
}

return (float)$number;

case '(':
// string;

$this->offset++;

$value='';

$level=1;
while(true)
{
$next = $this->read();

if($next=='(')
$level++;
else if($next==')')
{
$level--;
if($level==0)
break;
}
else if($next=='\\')
{
$next = $this->read();

switch($next)
{
case'n': $value.="\n"; break;
case'r': $value.="\r"; break;
case't': $value.="\t"; break;
case'b': $value.="\b"; break;
case'f': $value.="\f"; break;
case'(': $value.="("; break;
case')': $value.=")"; break;
case'\\': $value.="\\"; break;
default:
$next .= $this->read(2);
$value.=chr(octdec($next));
break;
}
}
else
$value .= $next;
}

return $value;

case '/':
// name;
$this->offset++;
return $this->read_until(PDF_PATTERN_SEPARATOR);

case '[':
// array;
$this->offset++;

$value = array();

while(true)
{
$this->skip_whitespace();

if( $this->get_next() == ']' ) break;

$value[] = $this->read_object();
}

$this->offset++;
return $value;

case '%':
// comment;
$this->offset++;

return $this->read_until(PDF_PATTERN_WHITESPACE);

case 'read_dictionary() ) !== false )
{
$this->skip_whitespace();

if( $this->expect('stream') )
{
$data_length = $value['Length'];

if( is_object( $data_length ) )
$data_length = $data_length->resolve();

$this->skip_whitespace();
$data = $this->read($data_length);

$this->skip_whitespace();

$this->offset += 9;

#$endstream = $this->read(9);

#if( $endstream != 'endstream' )
#{
# echo 'Unknown object data:'.$this->get_next(20)." at offset ".$this->offset."\n";
# exit;
#}

return $this->mark( new pdf_stream( $value, $data ) );
}
else
return $value;
}
else
{
// hex string
$this->offset++;

$hex = $this->read_until('>');

$value=pack("H*", $hex);

$this->offset++;

return $value;
}
break;

case 'f':

if( $this->expect('false') )
return false;

break;

case 'n':

if( $this->expect('null') )
return null;

break;

case 's':

if( $this->expect('startxref') )
{
$this->skip_whitespace();
$value = $this->read_while("0123456789");

return $this->mark( new pdf_startxref($value) );
}
break;

case 't':

if( $this->expect('true') )
return true;
else if( $this->expect('trailer') )
{
$value = $this->read_object();
$value = $this->mark( new pdf_trailer($value) );
}

break;
}

return new pdf_operator( $this->read_until(PDF_PATTERN_SEPARATOR) );
}

function read_dictionary()
{
if( $this->expect('<skip_whitespace();

if( $this->expect('>>') ) break;

$value[ $this->read_object() ] = $this->read_object();
}

return $value;
}
else
return false;
}

function mark( $child )
{
$child->parent = $this;

return $child;
}

function skip($count=1)
{
$this->offset += $count;
}

function read($count=1)
{
$v = substr( $this->data, $this->offset, $count );
$this->offset += $count;
return $v;
}

function expect($str)
{
$l = strlen($str);

if( substr($this->data,$this->offset,$l) == $str )
{
$this->offset += $l;
return true;
}
else
return false;
}

function get_next($count=1)
{
return substr( $this->data, $this->offset, $count );
}

function skip_whitespace()
{
preg_match('/['.PDF_PATTERN_WHITESPACE.']*/', $this->data, $matches, 0, $this->offset );

$this->offset += strlen($matches[0]);
}

function skip_until($chars)
{
preg_match('/[^'.$chars.']*/', $this->data, $matches, 0, $this->offset );

$this->offset += strlen($matches[0]);
}

function skip_while($chars)
{
preg_match('/['.$chars.']*/', $this->data, $matches, 0, $this->offset );

$this->offset += strlen($matches[0]);
}

function read_until($chars)
{
preg_match('/[^'.$chars.']*/', $this->data, $matches, 0, $this->offset );

$this->offset += strlen($matches[0]);

return $matches[0];
}

function read_while($chars)
{
preg_match('/['.$chars.']*/', $this->data, $matches, 0, $this->offset );

$this->offset += strlen($matches[0]);

return $matches[0];
}

function jump($offset)
{
$this->offset = $offset;
}

function eof()
{
return $this->offset >= strlen($this->data);
}
}

class pdf extends pdf_readstream
{
var $catalog;
var $xref_table;

function pdf($filename)
{
parent::pdf_readstream( file_get_contents($filename) );

$this->xref_table = array();
$this->objects_at_offsets = array();

$this->allow_references = true;

$this->jump( strrpos( $this->data, 'startxref' ) );

$this->expect('startxref');

$offset = $this->read_object();

$this->parse_xref( $offset );

if(isset($this->catalog))
$this->catalog = $this->catalog->resolve();
}

function parse_xref($offset)
{
$this->jump( $offset );

$this->expect( 'xref' );

while(true)
{
$this->skip_whitespace();

if( $this->expect('trailer') ) break;

$start = $this->read_while('0123456789');

$this->skip_whitespace();
$count = $this->read_while('0123456789');

for($n=0;$nskip_whitespace();
$line = $this->read_while('0123456789 fn');

list($offset,$generation,$type)=explode(' ',$line);

$generation = (int)$generation;

$this->xref_table[ $number.' '.$generation ] = (int)$offset;
}
}

$this->skip_whitespace();
$trailer = $this->read_dictionary();

if(isset($trailer['Root']))
$this->catalog = $trailer['Root'];

if(isset($trailer['Prev']))
$this->parse_xref( $trailer['Prev'] );
}

function get_pages()
{
$pages = array();

$this->add_pages( $this->catalog['Pages']->resolve(), $pages );

return $pages;
}

function add_pages( $array, &$pages )
{
$type = $array['Type'];

if($type=='Pages')
{
$kids = $array['Kids'];

foreach($kids as $kid)
$this->add_pages( $kid->resolve(), $pages );
}
else if($type=='Page')
{
$pages[] = new pdf_page( $array );
}
}

function get_dimensions( $array = false )
{
$pages = $this->get_pages();

return $pages[0]->get_dimensions();
}

function debug()
{
return pdf_debug( $this->catalog );
}

function resolve( $reference )
{
$old_offset = $this->offset;

$this->offset = $this->xref_table[ $reference->value ];

$value = $this->read_object();

$this->offset = $old_offset;

return $value;
}
}

class pdf_page
{
var $props;

function pdf_page( $props )
{
$this->props = $props;
}

function get_dimensions()
{
$mediabox = false;
$rotate = false;

$array = $this->props;

while(true)
{
if($mediabox === false)
if( isset($array['MediaBox']) )
$mediabox = $array['MediaBox'];

if($rotate === false)
if( isset($array['Rotate']) )
$rotate = $array['Rotate'];

if( $mediabox !== false and $rotate !== false )
break;
else if( isset($array['Parent']) )
$array = $array['Parent']->resolve();
else
break;
}

if($rotate===false)
$rotate=0;

list( $x1, $y1, $x2, $y2 ) = $mediabox;

$width = abs( $x1-$x2 );
$height = abs( $y1-$y2 );

if( ( $rotate % 180 ) == 0 )
return array( $width, $height );
else
return array( $height, $width );
}

function get_content_stream()
{
$contents = $this->props['Contents'];

if( is_array( $contents ) )
{
$content_data = '';
foreach( $contents as $part )
$content_data .= $part->resolve()->get_data();
}
else
{
$content_data = $contents->resolve()->get_data();
}

return new pdf_content_stream( $content_data );
}

function get_text()
{
return $this->get_content_stream()->get_text();
}

function debug()
{
return pdf_debug( $this->props );
}
}

class pdf_content_stream extends pdf_readstream
{
var $operators;

function pdf_content_stream( &$data )
{
parent::pdf_readstream( $data );

$this->allow_references = false;

$this->operators = array();

$operands = array();

$textarea=false;

while(!$this->eof())
{
$object = $this->read_object();

if( is_object($object) )
{
#if( is_a($object,'operator') )
{
if($object->value == 'BT')
$textarea = true;
else if($object->value == 'ET')
$textarea = false;

if($textarea)
{
$object->operands = $operands;
$this->operators[] = $object;
}
}

$operands = array();
}
else
$operands[] = $object;
}
}

function get_text()
{
$text='';

reset($this->operators);
foreach($this->operators as $operator)
$text .= $operator->get_text();

return $text;
}

function debug($level=0)
{
$inset=str_repeat("\t",$level);

echo $inset."content_stream\n";
echo $inset."(\n";

reset($this->operators);
foreach($this->operators as $operator)
{
echo $operator->debug($level+1);
}

echo $inset.")\n";
}
}

function pdf_debug( $value, $level=0 )
{
$inset = str_repeat("\t",$level);

if( is_object( $value ) )
{
return $value->debug($level);
}
else if( is_array( $value ) )
{
$str='';
$str.=$inset."Array\n";
$str.=$inset."(\n";

reset($value);
while(list($key,$v)=each($value))
{
if(is_object($v) or is_array($v))
{
$str.=$inset."\t".$key." =>\n";
$str.=pdf_debug($v,$level+2);
}
else
{
$str.=$inset."\t".$key." => ".pdf_debug($v);
}
}

$str.=$inset.")\n";

return $str;
}
else if( is_bool( $value ) )
{
if($value)
return $inset."true\n";
else
return $inset."false\n";
}
else if( is_null( $value ) )
{
return $inset."NULL\n";
}
else if( is_string( $value ) )
{
return $inset."\"$value\"\n";
}
else
{
return $inset.$value."\n";
}
}

class pdf_object
{
var $parent;
var $value;

function pdf_object($value)
{
$this->value = $value;
}

function resolve()
{
return $this;
}

function get_value()
{
return $this->value;
}

function debug($level=0)
{
$inset = str_repeat("\t",$level);

$str = $inset.get_class($this).' : '."\n";

$str .= $inset."(\n";
$str .= pdf_debug($this->value,$level+1);
$str .= $inset.")\n";

return $str;
}
}

class pdf_reference extends pdf_object
{
function resolve()
{
return $this->parent->resolve($this);
}
}

class pdf_stream extends pdf_object
{
var $data;
function pdf_stream($value,&$data)
{
$this->value = $value;
$this->data = $data;
}

function get_data()
{
$object = $this->resolve();

$filter = $object->value['Filter'];

switch($filter)
{
case false:
return $object->data;

case 'FlateDecode':

$data = @gzuncompress($object->data);

if(!$data)
{
#file_put_contents('data.bin',$object->data);
return false;
}

return $data;

default:
return false;
}
}

function get_text()
{
return $this->get_content_stream()->get_text();
}
}

class pdf_operator extends pdf_object
{
var $operands;

function debug($level=0)
{
$inset=str_repeat("\t",$level);

echo $inset."operator( ".$this->value." )\n";

if(count($this->operands))
{
echo $inset."(\n";

reset($this->operands);
foreach($this->operands as $operand)
{
echo pdf_debug($operand,$level+1);
}

echo $inset.")\n";
}
}

function get_text()
{
switch( $this->value )
{
case 'Tj':
return $this->operands[0];

case '\'':
return "\n".$this->operands[0];

case '"':
return "\n".$this->operands[2];

case 'TJ':

$string='';

$parts = $this->operands[0];

foreach($parts as $part)
if(is_string($part))
$string .= $part;
else
if($part operands[1];

if($delta_y!=0)
return "\n";
else
return '';

case 'Tm':

$delta_y = $this->operands[5];

if($delta_y!=0)
return "\n";
else
return '';

case 'T*':
return "\n";

default:
return '';
}
}
}
?>

2.Second create a index.php file and copy below code

get_pages();

while( list($nr,$page) = each($pages) )
{
list($width,$height) = $page->get_dimensions();
$text = $page->get_text();

echo "Page $nr is $width x $height and the text is:\n$text\n\n";
}
?>

enjoy you have done with small job … 🙂

# 1146 – Table ‘phpmyadmin.pma_table_uiprefs’ doesn’t exist

phpmyadmin_error

 

phpmyadmin reported the following error

# 1146 – Table ‘phpmyadmin.pma_table_uiprefs’ doesn’t exist

Modify the configuration file config.inc.php phpmyadmin

$ Cfg [‘Servers’] [$ i] [‘table_uiprefs’] = ‘pma_table_uiprefs’;

Changed to: $ cfg [‘Servers’] [$ i] [‘pma__table_uiprefs’] = ‘pma__table_uiprefs’;

Restart apache, web page log to re-enter, problem solving