现在的位置: 首页 > Web设计> 正文
PHP中Session的使用
2013年08月15日 Web设计 暂无评论 ⁄ 被围观 3,732+

php中使用session的场合主要是用户登录信息的记录和页面之间数据的传递,session的有效期为浏览器的生存期,直到用户关闭浏览器,服务器中的session数据才清除。本文介绍了怎么样利用session来存储变量。

In this article I’m going to show you some simple things to work with sessions in php, these are mostly used to store information about your users, like usernames, choices selected by users and similar, these however are stored on the server and deleted once the user closes the browser. To interact with session variables you need to carefully create them and then check them and accordingly do something based on their value. This is actually the main topic of this article, how to check and create php session variables.

Starting sessions …【启用session】

It is imperative that you first and foremost start the session on your website, otherwise the session variables will be lost as soon as the page is refreshed or goes to a different page. This can be easily done using the session_start() php function.

1
2
3
4
5
6
7
8
9
<?php session_start(); ?>
 
<html>
    <head>
    </head>
    <body>
        <p>Some content here</p>
    </body>
</html>
<?php session_start(); ?>

<html>
    <head>
    </head>
    <body>
        <p>Some content here</p>
    </body>
</html>

It is important that you use the session_start() function before any other code on your website.

Creating php session variables【创建session变量】

As you can see bellow, working wish session is similar to associative php arrays, the variable however needs to be $_SESSION. Then you create a session variable (more like an array element) by assigning it a value:

1
2
3
4
5
<?php
    session_start();
    $_SESSION['username']='Teddy';
    $_SESSION['logged']='yes';
?>
<?php
    session_start();
    $_SESSION['username']='Teddy';
    $_SESSION['logged']='yes';
?>

The above code will store the session variable ‘logged’ with the value ‘yes’ and based on this, you can probably show some specific links for a section that’s meant for logged users. Will show you in a second bellow.

How to check session variable【访问session变量】

Checking the session variable is important, else you’re just storing them for nothing, you need to make use of them in certain ways. Bellow its an example of using the above ‘logged’ session variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
    session_start();
    $username = $_SESSION['username'];
    if(isset($_SESSION['logged']) && $_SESSION['logged']=='yes') {
        echo "<p><a href='account-settings.php?user=$username' title='Account settings'>Account settings</a></br>";
        echo "<p><a href='view-profile.php' title='View profile>View profile</a></br>";
        echo "<p><a href='other-page.php' title='Other page'>Some other page</a></br>";
    }
// session is set, this will show similar as bellow:
// Account settings
// Edit account
// Some other page
?>
<?php
    session_start();
    $username = $_SESSION['username'];
    if(isset($_SESSION['logged']) && $_SESSION['logged']=='yes') {
        echo "<p><a href='account-settings.php?user=$username' title='Account settings'>Account settings</a></br>";
        echo "<p><a href='view-profile.php' title='View profile>View profile</a></br>";
        echo "<p><a href='other-page.php' title='Other page'>Some other page</a></br>";
    }
// session is set, this will show similar as bellow:
// Account settings
// Edit account
// Some other page
?>

Printing session variable【打印输出session变量】

You can also print the session value wherever you need its value, for example:

1
2
3
4
5
6
7
8
9
10
11
<?php
    session_start();
 
    $username = $_SESSION['username'];
    if(isset($_SESSION['logged']) && $_SESSION['logged']=='yes') {
        echo "Is $username logged in?<br/>";
        echo "Answer is: " . (isset($_SESSION['logged']) && $_SESSION['logged']=='yes' ? $_SESSION['logged'] : "no");
    }
// Is Teddy logged in?
// Answer is: yes
?>
<?php
    session_start();

    $username = $_SESSION['username'];
    if(isset($_SESSION['logged']) && $_SESSION['logged']=='yes') {
        echo "Is $username logged in?<br/>";
        echo "Answer is: " . (isset($_SESSION['logged']) && $_SESSION['logged']=='yes' ? $_SESSION['logged'] : "no");
    }
// Is Teddy logged in?
// Answer is: yes
?>

Heads up! If you are testing this, do not forget that sessions are being stored until you close your browser or until you clear your browser cookies and cache!

As you can see, this is not exactly hard, but needs a bit of attention. Sessions are normally used for logged section, where you rely on session to show account level section for your users.

Do not forget that this is only an example to see how session work, you will need to secure these if you really wish to use them in production … but that’s another article that may come soon.

That’s it for now, don’t forget to share and follow our website for new articles!

文章节选:how-to-check-and-create-php-session-variables

给我留言

留言无头像?


×
腾讯微博