PHP session Use method session application instance

Source: Internet
Author: User
Tags mysql tutorial php session

/*
Session Brief description
Session is a server global variable, commonly used in the user login, shopping cart instances, used in some security requirements of the application of high.
Session also has many advantages, such as easy to control, can be customized according to user storage (stored in the database tutorial), php.ini permissions, the default session of the storage path is the server's system temporary folder

Session Use Method

How do I create a session? It's very simple, really. Start session sessions and create a $useradmin variable

The code is as follows Copy Code

?
Session session_start ();
$_session["admin"] = ' www.111cn.net ';
Session_unset ();
Session_destroy ();

?>

A simple example of the application of the session instance
CREATE TABLE Session_test (
Admin NOT null default ',
PWD () NOT NULL default '
) type = Mysima;

Insert a piece of data
INSERT into session_test values (' admin ', ' 123456 ');
*/

User Login

The code is as follows Copy Code

$DB = ' abc ';
$c = MySQL tutorial _connect (' localhost ', ' root ', ' 123456 ');

$sql = "SELECT * from Session_test where admin= ' $admin ' and pwd= ' $pwd '";
$result = Mysql_db_query ($db, $sql, $c);

if (mysql_num_rows ($result))
{
$rs = Mysql_fetch_array ($result);
$_session[' admin ' = $admin;
}
Else
{
Exit (' username password incorrect ');
}

OK above the landing instance is complete, now we are to determine whether the user is logged in state, if it is to enter the relevant page

The code is as follows Copy Code
if (isset ($_session[' admin ')) && isset ($_session[' admin ']!= '))
{
Header (' location:http://www.111cn.net '); To determine the session of the admin is a value, so jump
}
Else
{
Exit (' not landed ');
}

Let's get out of here again.

The code is as follows Copy Code
$_session["admin"] = ';
Or
Session_unset ();
Or
Session_destroy ();

This site original article reproduced annotated from Www.111cn.net
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.