Google

Saturday, July 25, 2009

Read CSV file through PHP

Read CSV file through PHP

/*

$file_handles = fopen("my.csv", "r");

while (!feof($file_handles) ) {

$line_of_texts = fgetcsv($file_handles, 1024);

print $line_of_texts[0] . $line_of_texts[1]. $line_of_texts[2] . "
";

}

fclose($file_handle);

*/

Saturday, July 11, 2009

Difference between Cookies and Session (Cookies vs sessions) in PHP

Cookies and Session (Cookies vs sessions) in PHP

Both are use in terms of data storage in programming language...but how ? what is the preferable?, when use session?, when use cookie?, there are some question which arise in the mind at the time of of store data temp. as well as permanent.

There are differences between the two that will make each favorable in their own circumstance.

Cookies can be set to a long lifespan, which means that data stored in a cookie can be stored for months if not years. Cookies, having their data stored on the client, work smoothly when you have a cluster of web servers, whereas sessions are stored on the server, meaning if one of your web servers handles the first request, the other web servers in your cluster will not have the stored information.

Sessions are stored on the server, which means clients do not have access to the information you store about them – this is particularly important if you store shopping baskets or other information you do not want your visitors to be able to edit by hand by hacking their cookies. Session data, being stored on your server, does not need to be transmitted with each page; clients just need to send an ID and the data is loaded from the local file. Finally, sessions can be any size you want because they are held on your server, whereas many web browsers have a limit on how big cookies can be to stop rogue web sites chewing up gigabytes of data with meaningless cookie information.

So, as you can see, each have their own advantages, but at the end of the day it usually comes down to one choice: do you want your data to work when you visitor comes back the next day? If so, then your only choice is cookies – if you have any particularly sensitive information, your best bet is to store it in a database, then use the cookie to store an ID number to reference the data. If you do not need semi-permanent data, then sessions are generally preferred, as they are a little easier to use, do not require their data to be sent in entirety with each page, and are also cleaned up as soon as your visitor closes their web browser.

Friday, June 26, 2009

PHP Issue Trackers software list

Mantis


* Web: http://www.mantisbt.org/
* Requirements: PHP 4.3
* Supported databases are MySQL with stated support for Postgress, MSSQL, DB2 and Oracle.
* License: GPL


Eventum

* Web: http://dev.mysql.com/downloads/other/eventum/
* Requirements: PHP 5.1
* Supported databases is MySQL.
* License: GPL


Bugs the Bug genie

* Web: http://www.thebuggenie.com/
* Requirements: PHP 5.1.
* Supported databases is MySQL.
* License: MPL 1.1


WHUPS

* Web: http://www.horde.org/whups/
* Requirements: Horder framework 3.2+. PHP 4.3+.
* Most databases are supported.
* License: BSD license


phpBugTracker

* Web: http://phpbt.sourceforge.net/
* Requirements: PHP 4.0.6+.
* Supported databases are MySQL, PostreSQL and Oracle.
* License: BSD license


Other PHP Systems

* Flyspray - http://flyspray.org/
* FireAnt - http://chaozz.nl/software/fireant/
* zenTrack - http://www.zentrack.net

PHP performance tips by google

1) Upgrade your version of PHP

2) Use caching

3) Use output buffering

4) Don't copy variables for no reason.

5) Avoid doing SQL queries within a loop

6) Use single-quotes for long strings.

7) Use switch/case instead of if/else.

Saturday, May 23, 2009

How to read integer value from XML in PHP and after read add those value

When you read XML in php through simple xml class then at the time of integer value you need to type cast those value in "string", because
when you read through simple xml class object then he return the object(show through the var_dump).

Ex:

your XML look like -










now you want to read both ints tag value and add both value.


/*


$xml->load('abc.xml');

foreach($xml->a[0] as $abTagObj)
{

$ints[] = (String)$abTagObj->ints;
}

array_sum($ints); // show the total of all ints tag.

*/


but if you not type cast ints tag value as "string" then when you go for add then show the total as 0.


If still any question then pleases put your comment i will answer you. or any question regarding PHP, javascript, ajax, css, html, mysql put your comment feel free.

Saturday, April 25, 2009

Memcache for Code and Performance

"Memcache for Code and Performance "



When website has high traffic then continuos every time fetch record from db and show on the client side is tedious job and we need high performance then we use Memcache.



What is Memcache?



"memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load."



What is MemcacheDB?



It's a distributed key-value storage system designed for persistent. It is NOT a cache solution, but a persistent storage engine for fast and reliable key-value based object storage and retrieval. It conforms to memcache protocol(not completed, see below), so any memcached client can have connectivity with it. MemcacheDB uses Berkeley DB as a storing backend, so lots of features including transaction and replication are supported.



Before you get too excited keep in mind this is a key-value store. You read and write records by a single key. There aren't multiple indexes and there's no SQL. That's why it can be so fast.



The original client for memcached was written in Perl by the folks at Danga Interactive. The Python client is now maintained by a 3rd party group. The PHP client is available as a PECL package.PHP client is written in C as a PHP extension. The other two are written in Perl and Python. So, all the complex code has to be interpretted. For PHP however, most of the work happens in the engine, not in PHP code. This makes the PHP client far superior in performance to the other two. By default, memcached uses the port 11211.



The system is used by several very large, well-known sites including YouTube, LiveJournal, Slashdot, Wikipedia, SourceForge, ShowClix, GameFAQs, Facebook, Digg, Twitter, Fotolog, BoardGameGeek, NYTimes.com, deviantART, Jamendo, Kayak and in our Mobshare too.




PHP provides support for the Memcache functions through a PECL extension. To enable the PHP memcache extensions, you must build PHP using the --enable-memcache option to configure when building from source.



A few important things about memcache:



Memcache is a daemon, meaning it runs as a separate service on your machine. Just like MySQL runs as a separate service. In fact, to use memcache in PHP you have to connect to it, just like MySQL.



Think of memcache as the $_SESSION variable for PHP, but instead of it working on a per-user basis, it runs over the entire application — like MySQL. In fact, you can use memcache as you session handler for PHP.




Memcache to work on windows machine:



Download memcache [grab the 'win32 binary' version]



Install memcache as a service:



  • Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, memcached.exe] - thanks to Stephen for the heads up on the new version

  • If you’re running Vista, right click on memcached.exe and click Properties. Click the Compatibility tab. Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”.

  • Install the service using the command: c:\memcached\memcached.exe -d install from the command line

  • Start the server from the Microsoft Management Console or by running one of the following commands: c:\memcached\memcached.exe -d start, or net start "memcached Server"



Now that you have memcache installed, you’ll have to tie it in with PHP in order to use it.



1. Check your php extensions directory [should be something like: C:\php\ext] for php_memcache.dll
If you don’t have any luck finding it, try looking here: pecl4win.php.net/ext.php/php_memcache.dll [or look here for PHP 5.2.*]



2. Now find your php.ini file [default location for XP Pro is C:\WINDOWS\php.ini] and add this line to the extensions list:



extension=php_memcache.dll



3. Restart apache



4. Run this code to test the installation:



/*

$memcache = new Memcache;
$memcache->connect("localhost",11211);

"Server's version: " . $memcache->getVersion();

$tmp_object = new stdClass;
$tmp_object->str_attr = "test";
$tmp_object->int_attr = 123;

$memcache->set("key",$tmp_object,false,10);
echo "Store data in the cache (data will expire in 10 seconds)";

echo "Data from the cache:";
var_dump($memcache->get("key"));
*/

If you see anything but errors, you are now using memcache!



EDIT



Memcached, by default, loads with 64mb of memory for it’s use which is low for most applications. To change this to something else, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to look something like this:


“C:\memcached\memcached.exe” -d runservice -m 512



Now when you start the service via net start “memcached Server”, it will run with 512mb of memory at it’s disposal.



Memcache Functions:

The memcache object has several methods. With the Memcache::set method we can add an entry to the cache. The method had four arguments:

  • a key

  • any serializable variable we'd like to cache, in this case it's a string

  • a boolean whether we want to use on-the-fly zip compression using libz

  • the cache expiry measured in seconds

  • Ex: - $memcache->set("key",$tmp_object,false,10);
  • Memcache::add — Add an item to the server

  • Memcache::addServer — Add a memcached server to connection pool

  • Memcache::close — Close memcached server connection

  • Memcache::connect — Open memcached server connection

  • memcache_debug — Turn debug output on/off

  • Memcache::decrement — Decrement item's value

  • Memcache::delete — Delete item from the server

  • Memcache::flush — Flush all existing items at the server

  • Memcache::get — Retrieve item from the server

  • Memcache::getExtendedStats — Get statistics from all servers in pool

  • Memcache::getServerStatus — Returns server status

  • Memcache::getStats — Get statistics of the server

  • Memcache::getVersion — Return version of the server

  • Memcache::increment — Increment item's value

  • Memcache::pconnect — Open memcached server persistent connection

  • Memcache::replace — Replace value of the existing item

  • Memcache::setCompressThreshold — Enable automatic compression of large values

  • Memcache::setServerParams — Changes server parameters and status at runtime





The data is converted into a string with the serialize() function, so numbers, strings, arrays, and most objects will be stored easily… only resources (file descriptors, database connections, MemCache connections) can not be stored effectively.




It is important to keep in mind that if the MemCached server crashes or the process dies, all your cache data will be lost. Always store the actual data in a database server or another persistent storage system.




Conclusion



While the example we created is fairly trivial, it’s easy to see how this technique can be expanded throughout your site. Other queries and other data can be cached with similar benefits. Obviously, MemCached isn’t the only solution to web scaling and data caching, but it can be a pretty useful. Often, as seen in our example, a MemCached solution can be implemented and put in place with minimal code changes, minimal time, and, hopefully, minimum downtime for your users!

Future of Web Development with Hypertext Preprocessor V6

"Future of Web Development with Hypertext Preprocessor V6"



Are you ready for PHP V6? If you were upgrading tomorrow, would your scripts execute just fine or would you have work to do?



With the "PHP 6 Developers Meeting Notes" out I finally took a while to go and read through them and figure out what is the best way to move forward and start writing code that will be compatible not only for 4x and 5x but continue on though 6x.



New PHP V6 features




  • Unicode support:
    The primary alteration in PHP6 is support for Unicode(

    "Unicode is a computing industry standard allowing computers to consistently represent and manipulate text expressed in most of the world's writing systems.An encoding scheme that allows for virtually all major world languages to be encoded into a single character set. " )

    Much improved for PHP V6 is support for Unicode strings in many of the core functions. This new feature has a big impact because it will allow PHP to support a broader set of characters for international support. So, if you're a developer or architect using a different language, such as the Java™ programming language, because it has better internationalization (i18n) support than PHP, it'll be time to take another look at PHP when the support improves.


  • Namespaces:
    Namespaces are a way of avoiding name collisions between functions and classes without using prefixes in naming conventions that make the names of your methods and classes unreadable. So by using namespaces, you can have class names that someone else might use, but now you don't have to worry about running into any problems.


  • Move XML extension :
    The XMLReader and XMLWriter extensions will move into the core distribution and will be on by default.


  • Move Fileinfo exntesion :
    The extremely useful Fileinfo exntesion will move into the core distribution and enabled by default.


  • Static Binding :
    A new keyword will be created to allow for late static binding - static::static2(), this will perform runtime evaluation of statics.


  • FastCGI always on :
    The FastCGI code will be cleaned up and always enabled for the CGI SAPI, it will not be able to be disabled.


  • 64 bit integers added :
    64 bit integers added(int64), no int32(it is assumed unless you specify int64).


  • "break" keyword extended :
    break keyword will be extended with a static label - so you could do 'break first' and it'll jump to the label first: in your code.


  • foreach multi-dim arrays :
    foreach multi-dim arrays - you'll be able to foreach through array lists, i.e. "foreach( $a as $k => list($a, $b))".


  • {} vs [] :
    You can currently use both {} and [] to access string indexes. But the {} notation will raise an E_STRICT in PHP5.1 and will be gone totally in PHP6. Also the [] version will gain substr and array_slice functionality directly - so you could do "[2,]" to access characters 2 to the end, etc.


  • E_STRICT merges into E_ALL :
    This is quite a serious one! E_STRICT level messages will be added to E_ALL by default. This shows a marked move by the PHP team to educate developers on 'best practises' and displaying language-level warnings.


  • dl() only enable it when a SAPI(Server API) layer registers it explicitly



  • Make "var" an alias for "public" and remove the warning for it




Things removed from PHP V6



In addition to having new features, PHP V6 will not have some other functions and features that have been in previous versions. Most of these things, such as register_globals and safe_mode, are widely considered "broken" in current PHP, as they may expose security risks.



Features that will be removed from the PHP version include:




  • Magic Quotes to go :
    The magic quotes feature of PHP will be going, and as with register globals it's going to raise an E_CORE_ERROR if the setting is found anywhere. This will affect magic_quotes, magic_quotes_sybase and magic_quotes_gpc.


  • Register Globals to go :
    It will no longer be an ini file setting, and if found it will raise an E_CORE_ERROR,finally break all PHP3 era scripts with no recourse at all but to re-code it.


  • Register Long Arrays to go :
    Remember the HTTP_*_VARS globals from yesteryear? Well if you're not already using $_GET, $_POST, etc - start doing so now, because the option to enable long arrays is going (and will throw an E_CORE_ERROR).


  • Safe Mode to go :
    This may please developers who have web hosts that insist upon safe mode! But it will now go totally, again raising an E_CORE_ERROR if found. The reason is that apparently they felt it gave the 'wrong signal', implying that it made PHP secure, when infact it didn't at all. open_basedir will (thankfully) be kept.


  • Short php tags :
    Microsoft Active Server Pages (ASP)-style tags — the shorter version of the PHP tags — are no longer supported


  • ereg extension :
    The ereg extension, which supports Portable Operating System Interface (POSIX) regular expressions, is being removed from core PHP support.



  • Return by Reference will error :
    Don't initiate objects with the reference operator(E_STRICT error)



  • Stop Freetype 1 and GD 1 support
    Stop Freetype 1(TrueType rendering engine library) and GD 1(dynamic creation of images library) support




Some of the features mentioned here have also been ported to PHP V5.3, so that when you move to V6 of PHP, it'll be less of a jump.
The following list of features have been back-ported to V5.3, such as - " Namespaces"




Conclusions



That is about all If you are still using php4 to php5 compatibility, you might want to think about removing PHP5 in the coming months since there is already 6.0 dev versions out. Yes I know you guys do not like to give up your old ways(adoption of PHP6 even slower than that of PHP5 becuase of no backward compatibility) but it is time to move on.



Now PHP is becoming closer to enterprise apps. PHP6 is not a software revolution, but rather a standards evolution, which is sometimes more important in enterprise applications.

Saturday, March 21, 2009

PHP classes question

1) class myclass{

}

if you run this code then what you get ?

Guesss ... answer in last


2) class parent{

$a = 2;
}

$obj_parent = parent();

if you run this code then what you get ?

Guesss ... answer in last




Answer :

1) Fatel error
2) Parent word is reserve so again Fatel error.

Sunday, January 11, 2009

Famous javascript Library list and javascript framework

Famous javascript Library list -

* Ample SDK
* Backbase
* Clean AJAX
* Dojo Toolkit
* Echo
* Ext
* Himle
* JQuery
* Joose (framework)
* Microsoft AJAX Library
* midori
* Mochikit
* MooTools
* My Library
* Morfik
* OpenLink AJAX Toolkit
* Prototype JavaScript Framework
* qooxdoo
* Rialto Toolkit
* Rico
* Script.aculo.us
* SmartClient
* Spry framework
* Yahoo! UI Library

PHP MVC Framework list

10- Ambivalence: A Java-Maverick Port
9- WACT: Web Application Component Toolkit
8- Achievo: A good RAD framework
7- Phrame: A Java-Struts port
6- Studs: A Java-Struts port to PHP
5- Prado: The winner of Zend coding contest
4- PHPOnTrax: a Rails port - PHP5 Only
3- CakePHP: Inspired by Rails PHP4/5
2- Mojavi: The first MVC framework I fell in love with

and the winner is:

1- Symfony: Based on Mojavi and inspired by Rails

This list is based on my personal tests and use. I have tested and played with many others, but I think these are the best frameworks out there.
- The first framework I fell in love with was Mojavi because of its elegant way to implement the MVC model.
- Symfony corrected some problems in Mojavi and improved it by taking the good sides of RubyOnRails and Propel.
- CakePHP is very promising, the only problem - really, I don't know if it is a problem - is: the development process is very slow.