How to use PHP Session variables and example code _php tutorial

Source: Internet
Author: User
When you run an application, you open it, make some changes, and then close it. It's like a conversation. The computer knows who you are. It knows when you start the application and when it terminates. But on the internet, there is a problem: the server does not know who you are and what you do, because the HTTP address does not maintain state.
By storing user information on the server for later use, the PHP session solves the problem (such as user name, purchase of goods, etc.). However, the session information is temporary and will be deleted when the user leaves the site. If you need to store information permanently, you can store the data in a database.

Copy the manual, and then each try and then write it, convenient to check the drop, who let us just learn it. The session has about 12 functions, namely:

Session_Start: initial session.
Session_destroy: End session.
Session_unset: Release session memory.
Session_name: Access the current session name.
Session_module_name: Access the current session module.
Session_save_path: Accesses the current session path.
SESSION_ID: Access the current session code.
Session_register: Registers a new variable.
Session_unregister: Delete the registered variable.
Session_is_registered: Checks if the variable is registered.
Session_decode:session data decoding.
Session_encode:session data encoding.

There is also a global variable: $_session


Before you store user information in a PHP session, you must first start the session.
Note: the Session_Start () function must precede the label:

Copy the Code code as follows:







Store Session variables

Copy the Code code as follows:
Session_Start ();
Store session Data
$_session[' views ']=1;
?>



Retrieve session data
echo "pageviews=". $_session[' views ';
?>



[HTML]

End Session
The unset () function is used to release the specified session variable:

[Code]
unset ($_session[' views ');
?>

You can also completely end the session with the Session_destroy () function:

Copy the Code code as follows:
Session_destroy ();
?>

Instance:

Copy the Code code as follows:
Session_Start ();
Switch ($_get[' action ']) {
Case "loginif";
Login verification, assuming the secret of session storage should be equal to 123 for correct
if ($_session[' pass ']== "123") {echo "Password is correct you can perform logout";} Else{echo "Password error, you can re-login";}
Break
Case "Logout";
Logout Login
Session_unset ();
Session_destroy ();
echo "Logout successful! You can determine whether the password is correct to see if it is successful logout ";
Break
Case "Login";
The session is written for verification,
$pass = "123";//Password
$_session[' pass ']= $pass;
echo "Write the login password to determine whether the password is successful or not." ";
Break
}
?>

False This page is named temp.php


User login post, program processing write session


Determine if the user's password is correct


Login successful user log-off login


I summed up the usage of the session in PHP.

(a) Start session
Before each use of the session, add this sentence: "Session_Start ();". As the name implies, the function is to start using the session.
(b) Registration session
The first thing to do is to create a global (note, be sure to define it as global, otherwise not in other pages) array, such as $login, where $login[' name ']= "Victor", $login [' pwd ']= ' 111111 ', then call function " Session_register (login); "The session was successfully registered.
(iii) Use the variables in the session
Similar to registering a session, you must first create a global array, and then just the same as using a generic array.
(d) Determining whether the session is registered
Very simply, use "if (login)" to Judge Session_is_registered.
(v) Uninstall session
Also very simple, "session_unregister (login);" You can do it.
Note: It is important to proceed before (ii) (iii) (iv) (v).


An example is given below:

Index.htm

Copy the Code code as follows:


Test





login.php

Copy the Code code as follows:
Global $login;
if ($_post[' name ']!= "Victor" | | $_post[' pwd ']!= "111111")
{
echo "Landing failed";
echo "Please return";
Exit
}
$login = Array (' name ' =>$_post[' name '),
' pwd ' =>$_post[' pwd ');
Session_Start ();
Session_register (login);
echo "Viewing information
";
echo "Exit Login
";
?>

info.php

Copy the Code code as follows:
Session_Start ();
if (session_is_registered (login))
{
Global $login;
echo "Hello,". $login [' name ']. "
";
echo "Exit Login
";
}
Else
{
echo "Illegal operation
";
Exit
}
?>


logout.php

Copy the Code code as follows:
Session_Start ();
Session_unregister (login);
Header ("location:index.htm");
?>

http://www.bkjia.com/PHPjc/313634.html www.bkjia.com true http://www.bkjia.com/PHPjc/313634.html techarticle when you run an application, you open it, make some changes, and then close it. It's like a conversation. The computer knows who you are. It knows when you start the application and in ...

  • 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.