Sanjoy Roy

[MCM, MCP, SCJP] – Senior PHP Programmer

Tag Archives: PHP

PHP Mobile Detection Library


Download this ZIP: click MobileESP_Samples.zip

include("mp/php_redirect_sample/code/mdetect.php");
      $iphoneTierHomePage = 'http://m.xyz.com.au/index.php?page=m';
      $genericMobileDeviceHomePage = 'http://m.xyz.com.au/index.php?page=m';
      $desktopHomePage = 'index.php';
      $uagent_obj = new uagent_info();         
      function AutoRedirectToProperHomePage(){
	      global $uagent_obj, $iphoneTierHomePage, $genericMobileDeviceHomePage, $desktopHomePage; 
	      if ($uagent_obj->isTierIphone == $uagent_obj->true) 
          header ('Location: '.$iphoneTierHomePage);          
	      else if ($uagent_obj->DetectMobileQuick() == $uagent_obj->true) 
          header ('Location: '.$genericMobileDeviceHomePage);        
	      else 
          header ('Location: '.$desktopHomePage);
      }  
      AutoRedirectToProperHomePage();
Advertisements

Use PHP functions in JavaScript


php.js Logo php.js is an open source project that brings high-level PHP functions to low-level JavaScript platforms such as webbrowsers, AIR, V8 and rhino.

If you want to perform high-level operations on these platforms, you probably need to write JS that combines it’s lower-level functions and build it up until you have something useful like: md5(), strip_tags(), strtotime(), number_format(), wordwrap().

That’s what we are doing for you.

No server component required. To use php.js you can either:

WampServer


WampServer is a Windows web development environment. It allows you to create web applications with Apache, PHP and the MySQL database. It also comes with PHPMyAdmin to easily manage your databases.

https://i1.wp.com/www.wampserver.com/en/data/image_menu_wamp.gif
WampServer installs automatically (installer), and its usage is very intuitive. You will be able to tune your server without even touching the setting files.
WampServer is the only packaged solution that will allow you to reproduce your production server. Once WampServer is installed, you have the possibility to add as many Apache, MySQL and PHP releases as you want.
WampServer also has a trayicon to manage your server and its settings.

WampServer’s functionalities are very complete and easy to use so we won’t explain here how to use them.
With a left click on WampServer’s icon, you will be able to:

  • manage your Apache and MySQL services
  • switch online/offline (give access to everyone or only localhost)
  • install and switch Apache, MySQL and PHP releases
  • manage your servers settings
  • access your logs
  • access your settings files
  • create alias

With a right click:

  • change WampServer’s menu language
  • access this page

Download Link

Parse .xls (Excel) using PHP


XLSParser.php:

if ($fileType !='' && $fileType == 'application/vnd.ms-excel'){
		//parse xsl file
		 $ext = 'xls';
		 $filename 	= $user.'.'.$section.'.upload.'.date('Ymd').'.'.$ext;
		 copy($_FILES['file']['tmp_name'], $UPLOAD_CSV_PATH.$filename);
		 require_once '../includes/reader.php';

		$data = new Spreadsheet_Excel_Reader();
		 //set output encoding
		$data->setOutputEncoding('CP1251');
		//read the excel file through api
		$data->read($UPLOAD_CSV_PATH.$filename);
		//prepare to retun result
		$actual_data = $data->sheets[0]['cells'];

		$rowsLength	= array();
		for($i=1; $i<$max_nos; $j++){
				$allColumns[$j] = $actual_data[$i][$j];
			}
			$newRowData[] =  $allColumns;
		}
		$data = $newRowData;
  }

Parse .CSV using PHP


CSVParser.php:

if ($fileType !='' && $fileType == 'text/comma-separated-values'){
		//parse csv file
		 $ext = 'csv';
		 $filename 	= $user.'.'.$section.'.upload.'.date('Ymd').'.'.$ext;
		 copy($_FILES['file']['tmp_name'], $UPLOAD_CSV_PATH.$filename);

		 $row 				= 1;
		$fhandle 			= @fopen($UPLOAD_CSV_PATH.$filename, "r");
		$columnHeaders		= '';
		$columnNames		= '';
		$rawData			= array();
		$count = 0;
		while (($fdata = fgetcsv($fhandle, 1000, $terminated_by, $enclosed_by)) !== FALSE) {
			$len = count($fdata)-1;
			if (strlen($fdata[$len])==0) array_pop($fdata);
			$rawData[$count] = $fdata;
			$count++;
		}
		fclose($fhandle);

		$newRowData = array();

		for($i=0; $iencode($rawData);

  }

Parse XML Spreadsheet using PHP DOMDocument


xmlparser.html:

File Name:  
Options: Column Heading Included
Column Heading & Name Included
Just Data
 
Hidden Parameters:

 

xmlparser.php:

$fileType 	= isset($_FILES['file']['type']) ? $_FILES['file']['type']: '';
if ($fileType !='' && $fileType == 'text/xml'){
	  $ext = 'xml';
	  if ($_FILES['file']['tmp_name']){
	  	  //Copy to the backup directory
	  	  $filename 	= $user.'.'.$section.'.upload.'.date('Ymd').'.'.$ext;
		  copy($_FILES['file']['tmp_name'], $UPLOAD_CSV_PATH.$filename);
		  //parse xml spredsheet
		  $dom 		= DOMDocument::load($UPLOAD_CSV_PATH.$filename);
		  $rows 	= $dom->getElementsByTagName('Row');
		  $data		= array();

		  foreach ($rows as $row){
			  $index 	= 1;
			  $cells 	= $row->getElementsByTagName('Cell');
			  $tempdata = array();
			  foreach( $cells as $cell ){
				  $ind = $cell->getAttribute('Index');
				  if ( $ind != null ) $index = $ind;
				  //echo $cell->nodeValue."
"; $tempdata[] = $cell->nodeValue; $index += 1; }//inner for loop $data[] = implode(",", $tempdata); }//outer for loop }//outer if (file temp_name compare) }

PHP – Undefined Index


This error appears because of your PHP error reporting settings. Usually, it appears when your variable is not properly set. There are two ways to handle this issue:

1. Check if $_POST[‘action’] is set before using it. For example:

if (!isset($_POST['action']))
{
//If not isset -> set with dumy value
$_POST['action'] = "undefine";
}

2. Suppress Notice warnings

Notice warnings could be suppressed by changing the error_reporting variable in your PHP.ini. error_reporting could be set to show all errors except those for notices and coding standards warnings: error_reporting = E_ALL & ~E_NOTICE

The same is accomplished by adding the following line in your php page:

 error_reporting (E_ALL ^ E_NOTICE);
%d bloggers like this: