Cookies in php

Cookies in php, infact in all lnguages,are used to identify the user.

A cookie is a file that server saves on the user computer. Whenever the same computer request the server for any webpage it sends cookies too with the request. Cookies are not secure method to identify the user because it saves on the user's computer and there can be security issues.

Creating Cookies:-

The setcookie() function in php is used to create the cookies. This function must come at the top of your webpage before the opening tag of html.

Example of Creating Cookies:-

setcookie("programmer","Adeel Fakhar",time+3600);

The above example is creating a cookie with name programmer, setting its value Adeel Fakhar and expiration time is after one hour.

You can set expiration time through variable too, like below

$expiration_time=time()+60*60*24*20;

setcookie("programmer","Adeel Fakhar", $expiration_time);

In the above example, the expiration time is set to after 20 days

How to retrieve value from Cookies?

$_COOKIE is used to retrieve the value of the cookie.

for above example, to retrieve value from programmer cookie, just use this

$_COOKIE["programmer"];

to view all cookies just type

print_r($_COOKIE);

Deleting Cookies

To delete the cookie just set its expiration time to the past. Like in the below example we are setting expiration time of cookie one hour ago

cookie("programmer","Adeel Fakhar",time()-3600);

What to do, if browser is not supporting Cookies?

If your application is dealing with browsers that are not supporting cookies then your website is useless for the users because in the middle all functionality of your website will collapse, so try to use sessions or sent data through forms using get or post method. This is professional way.



Read more...

Sending emails in php

Sending email functionality is now a must feature in almost every website. Now its up to you in which module you are allowing visitor to send emails.

The php mail() is used to send emails. It normally takes five parameters, among these three parameters are required. The five parameters are as follows

  1. to (Required. Specify the email address of reciever)
  2. subject (Required. Specify the subject of the email)
  3. message(Required. Specify the message of the email)
  4. headers(Optional. Specify the other additional information like from.cc, bcc etc
  5. parameters(Specify additional parameter)

PHP Simple Email Program

The simplest way for email to be send is a text based email

$to="abc@yahoo.com";

$subject="Testing email in php";

$message="I just want to test the email functionality in php";

$from="xyz@gmail.com";

$headers="from:$from";

mail($to,$subject,$message,$headers);

echo "Email sent successfully";

Read more...

How to include a php file in a php program?

Sometimes we may want to include another php file in our php program. This technique is normally followed when let us suppose we want to interact with the database, know for example there are 20 php files in which database interaction code is written, as we all know before interacting with database, we first have to create connection with it, so in the above senario we have to write connection code in all 20 pages one by one, it is really frustrated thing but the really frustrated stuff is that when u wish to change anything in your connection, u will have to also change it in all 20 pages.

So to overcome this problem, file inclusion is used.

Methods to include the file

There are normally four methods to include file in php. These methods are used to include the content of file in your program before server execute it.
  1. include();
  2. include_once();
  3. require();
  4. require_once();

Example:-

include('myfile.php');

include_once('myfile.php');

require('myfile.php');

require_once('myfile.php');

Normally people use include() or require() as these functions working is almost same, the only differ between these is error handling process. Incase of any error in your code the include() generates the warning but script execution never stopped but when we use require() then incase of any error in your code, it generates a fatal error and execution of the program is stopped.

Read more...

Session Handling in php

Session is a very powerful feature that is offered by php. Now question is what is session?

Session

Session is a logical object that offers a complete functional transaction to the user. For example like in gmail, when u signin through ur google account then session is created and when u logout session is destroyed. Its due to session when u go from one page to another page it never ask you to submit ur gmail id again, so session offer users complete functional transaction.

Session store all the information of the user in its session variables and then make this avilaible to all pages in the whole application.

Where sessions are used in the Web Site?

Sessions are normally used when user successfully login through its account, as i give u gmail example earlier. Session is a very powerful thing without session you cannot imagine to built a website that allows user to register first and then allowing signin process and then want to know what user will do after signing in.

For example there are lot of websites who offers online shopping, shopping cart cannot be made without sessions.

How to create sessions in php?


  1. First of all create the session object like this session_start();

  2. Create the session variable and store value in it like $_SESSION['username']="Adeel Fakhar";
  3. Now just call this username in all pages but one thing make sure always declare session_start(); at top, there should be no thing before it, else u may get errors.

How to unset and destroy session?

Normally we unset session and destroy session at logout page. unset() will clear all session variables and session_destroy() just destroy all the session.

Example:-

In logout page just write this code

session_start();

unset($_SESSION['username']);

session_destroy();

View the session demonstration in the images below








Read more...

Form Handling in php

Forms are use to get user information and then process these information. Without forms no one can imagine to built a dynamic website that offers different functionalities to the user.

Where forms are used in the website?

Forms can be used anywhere in the website it depends up on you. For example if you want from user to register then you have to use form, if u want from user to signin then u have to use form, etc.

How to access form values after form submit the form?

There are two main attributes of form elements, one is method and other one is action.

method is the attribute that specify how form data must be transfered from this page to the page define in action attribute of the form.

There are two instances(values) of method attribute, one is get and other one is post

When to use get and when to use post?

When u want to show the form elements in the addressbar of the browser or when you want to submit query string then method=get is used , suppose we have a form containing two fields one is username and other is password, if we use method=get then all information will be displayed to the addressbar of the browser like below

for example:-http://localhost/dest-pae.php?name=Adeel&&password=987654

so hence one thing is also proved that u cannou use get method whenever you want to transfer secure data like password so then u have to use method=post because no information is display to the addressbar if we use method=post

How to get form data?

To get form data in next page or in same page just make sure what is the value of attribute method, if method=get then use $_GET to get the form elements and if method=post then use the $_POST


Example:-

$name=$_POST['name'];

$password=$_POST['password'];

Also view the images to see full demonstration of form handling in php








Read more...