Cookies in php
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...
Labels: php
Sending emails in php
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
- to (Required. Specify the email address of reciever)
- subject (Required. Specify the subject of the email)
- message(Required. Specify the message of the email)
- headers(Optional. Specify the other additional information like from.cc, bcc etc
- 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...
Labels: php
How to include a php file in a php program?
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.
- include();
- include_once();
- require();
- 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...
Labels: php
Session Handling in php
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?
- First of all create the session object like this session_start();
- Create the session variable and store value in it like $_SESSION['username']="Adeel Fakhar";
- 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
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...
Labels: php


