Powered By Blogger

Monday, 29 August 2011

What is Hypertext Preprocessor....?

When you are Using a language such as php and database like MySQL , those in combination enables you to create dynamic websites that can interact with the end user as well  provides  the ability to contain the real world information at any instance of time that may actually vary with the time , in the right order on your application. 
                                                         Php is a server side scripting language that you can embed  on your HTML page which gets interpreted at the sever end and then  translated into a HTML or some other output that a user needs  to see……php is specifically designed for the web applications. 
                                                                                  
Initially php was developed by”Rasmus lerdorf” in the year 1994 which can be said to be PHP v 1.0 it was not a public release.  for me he was a great fan of “C” …..as v 1.0 was nothing more than Cgi binaries written in C language. he termed Php as “Personal home page “ as  he wished to replace the Perl scripts that he had been using to maintain his home page…..with his own written Cgi binaries.In the year 1998 v 3 of php was introduced with major changes made to the earlier one due to efforts of  jeems jeroskee and Andrew gutmun .They also renamed it to “Php: hypertext preprocessor”.Later both of them formed a new company named “ZEND Technologies” which still guides the php group busy with the development of the php.
                                                   Php is an open source project which means you have access to source code and can use, alter, and redistribute it all without charges.Along with this php is cross platform, robust, scalable and object oriented as well. 
                      Php is getting popular everyday…at any instance of time if you wish to see the number of usage of php all around the world you simply need to type http://www.php.net/usage.php on your browsers window.
What I like in PHP ?
1. The way php is growing.
2. Huge Support Online (forums, Blogs and etc).
3. Open Source.
4. Large number of Third Party Tools.

                                                     California-based developer, Tyler Renelle blogged about his experiences in the web development job market. After applying to over 200 jobs and having nearly 30 interviews, he found that the easiest jobs to get for developers with minimal experience were PHP jobs using systems like Drupal, Joomla, and Word-press. He adds that the jobs using the LAMP stack were the easiest to land right out of college with 0-3 years of experience required for most of the positions. PHP is considered by some to be an easy language to learn. However, Renelle says there are more career growth opportunities and better pay in Java.


Median Hourly Rate by Job - Skill: PHP (United States)
Median Hourly Rate by Job

Monday, 8 August 2011

autoload function

an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

<?php
function __autoload($class_name) {
    include $class_name . '.php';
}

$obj  = new MyClass1();
$obj2 = new MyClass2(); 
?>

Preventing Inheritance and Overriding

need to prevent some methods or classes from being inherited might arise in my object hierarchy. For example, I might want a way to prevent people from overriding the get_ProductID method in my Product class.
In its long-running tradition of solving my problems, PHP also provides a solution for this. By prefixing a method with the final keyword, you can prevent inheriting classes from overriding it and implementing their own version. I can modify my Product class as follows:


<?php
  abstract class Product
  {
    // etc.
    //
    // nobody can override this method.
    //
    final public method get_ProductID()
    {
      return $this->id;
    }
    // etc.
  }
?>

You may be scratching your head and wondering how a class can be abstract but have final methods that nobody can override. The answer is that an abstract class can still contain some implementation, but somebody has to extend it and implement the unimplemented portion defined by the abstract methods. The implemented portion of the class can include methods that cannot be overridden, as indicated by the final keyword.

Inheriting classes can still access and call these methods; they just cannot include them in the set of things they extend, modify, or re-implement.

Classes can also be declared as final, which means that nobody is allowed to inherit from them. You might see this if you have a User object and an inheriting AdministratorUser object with special privilegesyou might choose to declare the AdministratorUser object as final to prevent others from inheriting it and any of its special privileges.

Parent Keyword

This article will guide you through the PARENT keyword used in OOPS implementation in PHP5. Parent Keyword allows to forcefully call the parent method and not the child method. This is useful when you are implementing Polymorphism in PHP5. This keyword is also very useful if you want the derived class to call the parent class before performing certain operation.

Example – Implementing Parent Keyword
< ?
class Person
{
      public function showData()
      {
            echo "This is Person's showData()n";
      }
}
 
class Customer extends Person
{
      public function showData()
      {
            parent::showData();  //Calling Person Class showData Method
            echo "This is Customer's showData()";
      }
}
 
$c = new Customer();
$c->showData();
?>



Output:
This is Person’s showData()
This is Customer’s showData()

Explanation:
  • Here i am extending the Person Class in Customer Class
  • In Customer Class showData() method is calling the parent class showData() method


auto_prepend_file and auto_append_file

SSI is great, no doubt. But, if you just need to implement a process for including a header and / or a footer in your pages, there are other options – one such option being PHP. When most people think of modularising their pages with PHP they think of the include() construct – use of this feature has, in my opinion, the same draw backs of using Server Side Includes: you have to manually add an include() statement to every page. However, the PHP language does offer another alternative through its initialisation file (the php.ini file) by way of the auto_prepend_file and auto_append_file values.
Each of these values define an absolute path to a PHP file and when set the auto_prepend_file value will include the indicated file at the beginning of every PHP file that is executed on the server. Likewise, the file indicated in the auto_append_file value is added to the end of each executed PHP file.
Keep in mind that this process isn’t without its drawbacks – mainly in Lighttpd and Nginx where PHP configuration is server-wide. In these environments the two files specified in your php.ini file will become part of every PHP file that is executed on the server. In an Apache environment, however, where PHP has been installed as an Apache Module (or, indeed, in the drop-in replacement server, Litespeed), PHP initialisation values can be defined on a per-directory level and this opens up a world of possibilities.

Setting the Values Per-Directory

Please note: If PHP isn’t installed as an Apache Module (e.g. PHP installed through FastCGI) then this process won’t work and any PHP configuration will have to be done server-wide or per PHP file at runtime.
How you define these values will depend on the type of Apache environment in which you’re operating. In most circumstances the values are defined in a .htaccess file – the standard Apache directory configuration file. However, if your server has been patched with suPHP or PhpSuExec the you will need to include the values in a per-directory php.ini file. If you define the values in a .htaccess file and are greeted with a server error then you need to use the latter method.

Setting the Values in a .htaccess File

The syntax for defining a PHP initialisation value in a .htaccess file is: #A Comment
php_value setting_name setting_value

So, to set the auto_prepend_file and auto_append_file values you would add the following lines to a directory’s .htaccess file (changing the file path to your own, of course): #Prepend to each PHP File
php_value auto_prepend_file /home/path/to/header.php

#Append to each PHP File
php_value auto_append_file /home/path/to/footer.php

Setting the Values in a php.ini File

IMPORTANT: To use a per-directory php.ini file you do NOT need to include a full php.ini file. Simply create a php.ini file in the desired directory and include the value you wish to define only. The PHP installation’s default php.ini file will take care of all other values.
The syntax for defining an initialisation value in a per-directory php.ini file where the value is a string is: setting_name = "Setting Value"
So, to set the auto_prepend_file and auto_append_file values you would add the following lines to a directory’s php.ini file (changing the file path to your own, of course): auto_prepend_file = "/home/path/to/header.php"
auto_append_file = "/home/path/to/footer.php"

Send email using GMail SMTP server from PHP page

1 Download PHPMailer from http://phpmailer.sourceforge.net
2 Extract to folder phpmailer
3 Create a file email.php
4 Paste this code and change the values in blue as you need (I modified the sample code given on the PHPMailer homepage)
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username@doamin.com"; //Reply to this email ID
$email="username@domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "
; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
5 Open the file class.smtp.php in phpmailer directory
6 Paste this code
$host = "ssl://smtp.gmail.com";
$port = 465;
before the line 104 #connect to the smtp server
Hint: Search for #connect
7 Open this page in browser and it will send the email using GMail.

Hint: When you want to email the details from a form, set the variables using the form variables.
eg. $mail->Username=$_POST['email']


System Configuration for the mail() Function

Before you can use the mail() function to send mail, a few directives must be set up in the php.ini file so that the function works properly. Open php.ini with a text editor and look for these lines:

[mail function] 
; For Win32 only. 
SMTP = localhost
; For Win32 only.
sendmail_from = me@localhost.com 

; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;
sendmail_path 




If you're using Windows as your Web server platform, the first two directives apply to you. For the mail() function to send mail, it must be able to access a valid outgoing mail server. If you plan to use the outgoing mail server of your ISP (in the following example, we use EarthLink), the entry in php.ini should look like this:

SMTP = mail.earthlink.net

The second configuration directive is sendmail_from, which is the email address used in the From header of the outgoing email. It can be overwritten in the mail script itself, but normally operates as the default value. For example:
 
sendmail_from = youraddress@yourdomain.com

A good rule of thumb for Windows users is that whatever outgoing mail server you've set up in your email client on that machine, you should also use as the value of SMTP in php.ini.

If your Web server is running on a Linux/Unix platform, you use the sendmail functionality of that particular machine. In this case, only the last directive applies to you: sendmail_path. The default is sendmail -t -i, but if sendmail is in an odd place or if you need to specify different arguments, feel free to do so, as in the following example:

sendmail_path = /opt/sendmail -odd -arguments

After making any changes to php.ini on any platform, you must restart the Web server process for the changes to take effect.

Sunday, 7 August 2011

What’s the special meaning of __sleep and __wakeup?

__sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.

What Is a Persistent Cookie?

A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. By default, cookies are created as temporary cookies which stored only in the browser's memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
• Temporary cookies can not be used for tracking long-term information.
• Persistent cookies can be used for tracking long-term information.
• Temporary cookies are safer because no programs other than the browser can access them.
• Persistent cookies are less secure because users can open cookie files see the cookie values.

What is the difference between session_register and $_session?

Following are differences between session_register and $_SESSION

1. session_register function returns boolean value and $_SESSION returns string value

2. session_register function does'nt work if register_global is disabled. $_SESSION works in both case whether register_global is disabled or enabled. So using $_SESSION for session variable manipulation is more appropriate.

What is the difference between Notify URL and Return URL?

Notify URL and Return URL is used in Paypal Payment Gateway integration. Notify URL is used by PayPal to post information about the transaction. Return URL is sued by the browser; A url where the user needs to be redirected on completion of the payment process.   

How we can get the value of current session id?

We can get the value of our current session id by using session_id().It returns the value of our current session id.

ow we can increase the execution time of a PHP script?

I have given you some function using them you can increase the execution time of a PHP script.Default time for execution of a PHP script is 30 seconds.
These are,
1.set_time_limit()//change execution time temporarily.
2.ini_set()//change execution time temporarily. 
3. By modifying `max_execution_time' value in PHP
configuration(php.ini) file.//change execution time permanent.

How we can upload videos using PHP?

When want to upload videos using PHP we should follow these steps:
1.first you have to encrypt the the file which you want to upload by using "multipart-form-data" to get the $_FILES in the form submition.
2.After getting the $_FILES['tmp_name'] in the submition 
3.By using move_uploaded_file (tmp_location,destination) function in PHP we can move the file from original location.

Monday, 1 August 2011

Uploading Files with PHP

This script will allow you to upload files from your browser to your hosting, using PHP. The first thing we need to do is create an HTML form that allows people to choose the file they want to upload.

 <form enctype="multipart/form-data" action="upload.php" method="POST">
 Please choose a file: <input name="uploaded" type="file" /><br />
 <input type="submit" value="Upload" />
 </form> 
 
This form sends data to the file "upload.php", which is what we will be creating next to actually upload the file.

 The actual file upload is very simple:

 <?php 
 $target = "upload/"; 
 $target = $target . basename( $_FILES['uploaded']['name']) ; 
 $ok=1; 
 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 {
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
 } 
 else {
 echo "Sorry, there was a problem uploading your file.";
 }
 ?> 
 
 
This very small piece of code will upload files sent to it by your HTML form.
  1. The first line $target = "upload/"; is where we assign the folder that files will be uploaded to. As you can see in the second line, this folder is relative to the upload.php file. So for example, if your file was at www.yours.com/files/upload.php then it would upload files to www.yours.com/files/upload/yourfile.gif. Be sure you remember to create this folder!
  2. We are not using $ok=1; at the moment but we will later in the tutorial.
  3. We then move the uploaded file to where it belongs using move_uploaded_file (). This places it in the directory we specified at the beginning of our script. If this fails the user is given an error message, otherwise they are told that the file has been uploaded.
 These few lines of code we have given you will allow anyone to upload data to your server. Because of this, we recommend that you do not have such a simple file uploader available to the general public. Otherwise, you might find that your server is filled with junk or that your server's security has been compromised.