> Online tutorial : php session
Showing posts with label php session. Show all posts
Showing posts with label php session. Show all posts

error handling in php

PHP Error Handling
The default error handling in PHP is very simple. An error message with filename, line number and a message describing the error is sent to the browser.
PHP Error Handling
When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks.
We will show different error handling methods:

• Simple "die()" statements
• Custom errors and error triggers
• Error reporting

sending mail in php

PHP Sending E-mails
PHP allows you to send e-mails directly from a script.
The PHP mail() Function .
The PHP mail() function is used to send emails from inside a script.
Syntax
mail(to,subject,message,headers,parameters)

    Parameter Description
  • to Required. Specifies the receiver / receivers of the email
  • subject Required. Specifies the subject of the email. Note: 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

Note:
 
For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.

destroying session in php

Destroying a Session
If you wish to delete some session data, you can use the unset() or the session_destroy() function.

The unset() function is used to free the specified session variable:

<?php

unset($_SESSION['views']);
?> 
function:
You can also completely destroy the session by calling the session_destroy()


<?php
session_destroy();
?>


Note:
 
session_destroy() will reset your session and you will lose all your stored session data.

storing session data php

Storing a Session Variable
The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:

<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>;
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
Output:

page views 1

<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>


output:

In the example , we create a simple page-views counter. The isset() function checks if the
"views" variable has already been set. If "views" has been set, we can increment our counter. If
"views" doesn't exist, we create a "views" variable, and set it to 1

php session

PHP Sessions
A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.

PHP Session Variables

When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.

A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to
store the data in a database.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID.
The UID is either stored in a cookie or is propagated in the URL.

Starting a PHP Session
Before you can store user information in your PHP session, you must first start up the session.
Note: 
The session_start() function must appear BEFORE the <html> tag


<?php session_start(); ?>
<html>

<body>
</body>
</html>   

The code above will register the user's session with the server, allow you to start saving user
information, and assign a UID for that user's session.

Function in session
Destroying session
Storing value in session