The use of PHP session variable and example code _php skills

Source: Internet
Author: User
Tags php session

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 is not maintained.
The PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchase item, etc.). However, session information is temporary and will be deleted after 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, easy to check the drip, who let us just learn. The session has about 12 functions:

Session_Start: initial session.
Session_destroy: End session.
Session_unset: Frees session memory.
Session_name: Accesses the current session name.
Session_module_name: Accessing 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: Deletes the registered variable.
Session_is_registered: Check whether the variable is registered.
Session_decode:session data decoding.
Session_encode:session data encoding.

There is also a global variable: $_session


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

Copy Code code as follows:

<?php session_start ();?>

<body>

</body>

Store Session Variable

Copy Code code as follows:

<?php
Session_Start ();
Store session Data
$_session[' views ']=1;
?>
<body>

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

</body>
[HTML]

End Session
The unset () function frees the specified session variable:

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

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

Copy Code code as follows:

<?php
Session_destroy ();
?>

Instance:

Copy Code code as follows:

<?php
Session_Start ();
Switch ($_get[' action ']) {
Case "loginif";
Login verification, assuming that the session store secret should be equal to 123 before the correct
if ($_session[' pass ']== "123") {echo "Password is correct you can perform logoff";} Else{echo "Password error, you can login again";}
Break
Case "Logout";
Logout Login
Session_unset ();
Session_destroy ();
echo "Logout successful! Can determine whether the password is correct to see if the successful cancellation ";"
Break
Case "Login";
Writes the session for verification,
$pass = "123";//Password
$_session[' pass ']= $pass;
Echo writes the login password to determine whether the password is successful or not. ";
Break
}
?>
<p> assume this page is named temp.php </p>
<p><a href= "Temp.php?action=login" > User login post, program processing write session</a></p>
<p><a href= "temp.php?action=loginif" > Determine if the user password is correct </a></p>
<p><a href= "Temp.php?action=logout" > Login successful user logout login </a></p>

I've summed up the usage of the session in PHP.

(a) Start session
Before each session, add this sentence: "Session_Start ();". As the name suggests, the function is to start using the session.
(ii) Registration session
The first thing to do is to create a global (note, it must be defined as global, otherwise not on other pages) array, such as $login, where $login[' name ']= "Victor", $login [' pwd ']= "111111", and then call the function " Session_register (login); ", the session was successfully registered.
(iii) Use variables within the session
Like a registered session, you create a global array first and then use a generic array.
(iv) Determine whether the session is registered
Very simply, use "if (session_is_registered (login)") to judge on it.
(v) Uninstall session
Also very simple, "session_unregister (login);" It's OK.
Note: Before (ii) (iii) (iv) (v), it must be preceded by (i).


An example is given below:

Index.htm

Copy Code code as follows:

<title> Testing </title>
<body>
<form method=post action= "login.php" >
User name: <input type= "text" name= "name" ><br/>
Password: <input type= "password" name= "pwd" ><br/>
<input type= "Submit" value= "submitted" >
</FORM>
</body>

login.php

Copy Code code as follows:

<?php
Global $login;
if ($_post[' name ']!= "Victor" | | $_post[' pwd ']!= "111111")
{
echo "Landing failed";
echo "Please <a href=index.htm> return </a>";
Exit
}
$login = Array (' name ' =>$_post[' name '),
' pwd ' =>$_post[' pwd ']);
Session_Start ();
Session_register (login);
echo "<a href=info.php> View information </a><br/>";
echo "<a href=logout.php> exit landing </a><br/>";
?>

info.php

Copy Code code as follows:

<?php
Session_Start ();
if (session_is_registered (login))
{
Global $login;
echo "Hello,". $login [' name ']. " <br/> ";
echo "<a href=logout.php> exit landing </a><br/>";
}
Else
{
echo "Illegal operation <br/>";
Exit
}
?>


logout.php

Copy Code code as follows:

<?php
Session_Start ();
Session_unregister (login);
Header ("location:index.htm");
?>

Related Article

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.