PHP Session Variable

Source: Internet
Author: User
Tags php session php tutorial unique id

PHP Session Variable

<?php Tutorial
Session_Start ();
$music = "A";
Session_register (' music ');

Echo $music;
?>

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

The session works by creating a unique ID (UID) for each visitor and storing the variables based on this UID. The UID is stored in a cookie or transmitted through a URL.
Start PHP 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

<?php session_start ();?>

<body>

</body>
The code above Store Session Variable
The correct way to store and retrieve session variables is to use PHP $_session variables:

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

<body>

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

</body>


Pageviews=1 in the following example, we created a simple page-view counter. The Isset () function detects whether the "views" variable has been set. If you have set the "views" variable, we accumulate the counter. If "views" does not exist, we create the "views" variable and set it to 1:

<?php
Session_Start ();

if (isset ($_session[' views '))
$_session[' views ']=$_session[' views ']+1;

Else
$_session[' views ']=1;
echo "views=". $_session[' views '];


PHP session_start () about cannot send session cache Limiter-headers
Solution:
Modify Session.auto_start = 0 for session.auto_start = 1 in php.ini
output_buffering = change to On or any number.
And don't forget to reboot ...


Error tips
Warning:cannot send session Cookie-headers already sent
Warning:cannot Send session cache Limiter-headers already sent
Analysis and Solving methods
The reason for this kind of problem is that when you use PHP session_start () in your program, you already have the actual HTML content output. Perhaps you say, I did not ah, I just echo or print a message. I'm sorry, the output from your Echo or print statement is the actual output of the HTML content. The solution to this type of problem is to transfer your session_start () to the first line of the program.


Warning:trying to destroy uninitialized sessions in
Analysis and Solving methods
A class such as a hint, the general situation is that you directly adjust the Session_destroy () function caused. Many friends think that the Session_destroy () function can run independently, but it is not. The solution is to use PHP session_start () to open the session function before you tune the Session_destroy () function.

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.