Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PHP scripting session

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use PHP scripting sessions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

One long-awaited feature of PHP4.0 is PHP's session support. PHP3.0 users, by contrast, have to use third-party libraries or are unable to do so at all. The lack of session support is one of the biggest shortcomings of PHP, and it is also the most criticized place. However, as session support has been part of the early beta version of PHP4.0, this barrier has disappeared.

To start a session on a page is to tell the PHP engine that you either want to start a session (if not previously) or continue the current session:

Session_start ()

Starting a session sends the user an identity string (such as 940f8b05a40d5119c030c9c7745aead9) via cookie; a matching temporary file is created on the server side, and in the above example, the name looks like this: sess_940f8b05a40d5119c030c9c7745aead9. This file contains the registered session variables and their assignments.

User access counters are the most common examples of using sessions:

Start your PHP module and make sure that the PHP code is the first line of the file: no white space, no HTML output, and so on. This is because when the session function issues a header, if you send a blank or HTML code before the session_start () function, the system will report an error.

/ / ifasessiondoesnotyetexistforthisuser,startone

Session_start ()

Next, register a variable named count.

Session_register ('count')

Registering a variable is tantamount to telling PHP that as long as the session exists, a variable named count also exists. This variable has not been assigned yet. However, if you add 1 to it, the value can be assigned to 1:

$count++

Considering the above lines of code, you have actually started a session (if not previously), assigned a session id to a user, registered a variable named count, and added $count to indicate that the user visited the page for the first time:

To display the number of times a user has visited the page during the current session, you can simply print out the value of $count:

What is the usage of PHP script session

Echo "

You'vebeenhere$counttimes.

"

The entire access counter code is as follows:

Session_start ()

Session_register ('count')

$count++

Echo "

You'vebeenhere$counttimes.

"

? >

If you reload the above script, you can observe that the count value has increased. Isn't that interesting?

You can also register the array in the session. Suppose you have an array called $faves:

$faves=array ('chocolate','coffee','beer','linux')

You can register the array like any other single variable:

Session_register ('faves')

Indexed arrays are no different from indexed other single variables, such as $faves. If your user wants to show his hobby on a page of the Web site, you can register what he likes as a session variable called $faves, and then you can print these values on other pages:

Session_start ()

Echo "Myuserlikes:

"

While (list (, $v) = each ($faves)) {

Echo "

$v ";}

Echo "

"

? >

This is what you want: a beautiful list of users' preferences.

Session variables cannot be overridden by query strings, that is, you cannot type an instruction like http:///www.yourdomain.com/yourscript.php?count=56 to assign a new value to the registered session variable $count. This is important for security: you can only modify or delete (unregistered) session variables on server-side scripts.

If you want to delete a session variable completely, you can unregister the variable from the system:

Session_unregister ('count')

To delete a session completely, such as pressing the Logout button, you can write the following code:

Session_destroy ()

Using sessions to store variable values frees us from the pain of writing database processing code, which doesn't overload the system, reduces the scope of use of proprietary database syntax, and you no longer have to send a lot of cookie to users who visit the site. And now it only takes a cookie and a variable to get it all done, and it really reflects all the brilliance in a drop of water! It couldn't be any easier than that.

Thank you for reading! This is the end of this article on "how to use PHP script conversations". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report