Pages

Monday 25 June 2012

Sending Emails In PHP

Our first step in sending email is locating php.ini file in htdocs folder and then editing.
PHP must be configured correctly in the php.ini file with the details of how your system sends email. Open php.ini file available in /etc/ directory and find the section headed [mail function].We try and find the WORD SMTP and a description shall be made there in place of the description write your "ip" address
in place of  localhost.This will solve the purpose.(press Ctrl+F) and write SMTP and find next and so on.
Windows users should ensure that two directives are supplied. The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address.
SYNTAX:-
Here is the description for each parameters.
Parameter                                                                        Description
to                                                  Required. Specifies the receiver / receivers                                                                                       of the email

subject                                          Required. Specifies the subject of the                                                                        email. This parameter cannot contain any                                                                                      newline characters

message                                         Required. Defines the message to be sent.                                                                Each line should be separated with a LF                                                                    (\n). Lines should not exceed 70 characters

headers                                         Optional. Specifies additional headers, like
                                                      From, Cc, and Bcc. The additional headers                                                               should be separated with a CRLF (\r\n)

parameters                                    Optional. Specifies an additional parameter to                                                                the sendmail program.

Below is a simple program for sending email:-

<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
  $to = "abc@domainName.com";
$subject = "This is subject";
$message = "This is simple text message.";
  $header = "From:pqr@domainName.com \r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
</body>
</html>

PHP (Destroying Session)

Destroying a PHP Session:
A PHP session can be destroyed  by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If we have a single session we can solve purpose by using unset() function.
Let us see now how it works.
/*
below we see counter which is our previously created counter.
but if we don't know the name or we want to destroy multiple sessions we use destroy function
*/
<?php
unset($_SESSION['counter']);
?>
*******************************
<?php
session_destroy();
?>
*******************************
More On Sessions:-
We will now learn turning sessions automatically.we just need to edit one file for this purpose.

Turning on Auto Session:
In this case we have no need to call start_session() function to start a session when a user visits our site if you set session.auto_start variable to 1 in php.ini file and we can find this file in the htdocs folder.

Sessions without cookies:

There may be a case when a user does not allow to store cookies on the machine. So there is another method to send session ID to the browser.

Alternatively, you can use the constant SID which is defined if the session started. If the client did not send an appropriate session cookie, it has the form session_name=session_id. Otherwise, it expands to an empty string. Thus, one can embed it unconditionally into URLs.

The following example demonstrates how to register a variable, and how to link correctly to another page using SID.

PHP Sessions STARTING

Starting of PHP Sessions:-
Our first step is to initialise function  session_start(); This will check if any other session have been already been started or not.If it is not this function will start it .Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session.
Now Let us have a look over a simple program related to starting of seesion.


/*
 The code below starts a session then register a variable called counter that is incremented each time the page is visited during the session.We must make sure
the use of isset() function to check if session variable is already set or not.
*/

<?php
  session_start(); //initialising the function
if( isset( $_SESSION['counter'] ) )
{
$_SESSION['counter'] += 1;
}
else
{
$_SESSION['counter'] = 1;
}
$msg = "You have visited this page ". $_SESSION['counter'];
$msg .= "in this session.";
?>
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
</html>

We must take note tto save this file as .php extension not as a html file although we see some html tags in use here.