6hz7vif 发表于 2024-10-4 18:58:05

PHP中session存储及删除变量的办法

<img src="https://mmbiz.qpic.cn/mmbiz_jpg/KlYj89pYNRmjgUrwA2gRkJaDMmj4yfomTdibkrDCaSERf3eyAcZDEARMjZ3rIW8umQaVRstia6hxQjib6vPoqoPCA/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php中文网最新课程</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">每日17点准时技术干货分享</span></p><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/LGh7bn8KbYDAupVNZ3Tgm3Rjo95qrBgrd3KmkA4asrDcZdTDejZ1j5FCl7gT1Po9OCW0N1gSYmibK7mEFgiaTvfw/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/GtWwdCwkv7GFibw2nyFhcye46c1b1N9l1ic5q13Gauib5BuSpqib405Jw3C8UESBiaZiakAWv9gmvL4oXwG6S3oJjXmQ/640?wx_fmt=1&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">本篇<span style="color: black;">文案</span><span style="color: black;">重点</span>给<span style="color: black;">大众</span>介绍<strong style="color: blue;">PHP中session</strong><span style="color: black;">怎样</span><strong style="color: blue;">存储变量</strong>以及<span style="color: black;">怎样</span><strong style="color: blue;">删除变量</strong>的。</span><span style="color: black;"><span style="color: black;">首要</span><span style="color: black;">大众</span>需要<span style="color: black;">认识</span>一下,<strong style="color: blue;">PHP中session什么意思</strong>?</span><span style="color: black;"><strong style="color: blue;">有什么用</strong>?</span><span style="color: black;">session中文的意思<span style="color: black;">能够</span><span style="color: black;">暗示</span>为“会话”,其本来的含义<span style="color: black;">指的是</span>有始有终的一系列动作/<span style="color: black;">信息</span>,例如用户提问某个问题,<span style="color: black;">而后</span>被回答,<span style="color: black;">这般</span>一个完整的对话,就相当于一次会话。</span><span style="color: black;">而PHP session 变量<span style="color: black;">便是</span>用于存储<span style="color: black;">相关</span>用户会话的信息,或更改用户会话的设置。</span><span style="color: black;">Session 变量<span style="color: black;">保留</span>的信息是单一用户的,并且可供应用程序中的所有页面<span style="color: black;">运用</span>。</span><span style="color: black;"><span style="color: black;">然则</span>session会话信息是临时的,在用户离开网站后就会被删除。</span><span style="color: black;"><span style="color: black;">倘若</span>需要永久储存信息,就需要把数据存储在数据库中。</span><span style="color: black;"><span style="color: black;">那样</span>下面<span style="color: black;">咱们</span>就结合简单的代码示例,为<span style="color: black;">大众</span>介绍PHP中session的<span style="color: black;">基本</span>用法,存储变量以及删除变量。</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;">1、</span>session存储变量</strong></span><span style="color: black;">当你需要将用户的信息存储到session中时,必须先开启会话。</span><span style="color: black;">代码示例如下:</span><span style="color: black;"><span style="color: black;"><span style="color: black;">&lt;?php</span></span></span><span style="color: black;">session_start();</span><span style="color: black;">$name=<span style="color: black;">"123"</span>;</span><span style="color: black;">$_SESSION[<span style="color: black;">username</span>]=$name;</span><span style="color: black;">$_SESSION[<span style="color: black;">password</span>]=$name;</span><span style="color: black;"><span style="color: black;">echo</span> $_SESSION[<span style="color: black;">password</span>];</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span><span style="color: black;">咱们</span>先开启会话,<span style="color: black;">而后</span>将变量$name存储到session中。</span><span style="color: black;"><span style="color: black;">经过</span>echo输出,测试结果如下:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Lhm5kkN9JXMaRn2ia4nZYuLOReSu9C538Bp2rjVVAcPOm6YFI5RR69Ucvpc0Fia8Hxlk8V7P85HZt9R6zQAQhIAw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">session_start() 会创建新会话<span style="color: black;">或</span>重用现有会话。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">2、</span>session删除变量</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1、<span style="color: black;">运用</span>unset()</strong></span></p><span style="color: black;">&lt;?php</span><span style="color: black;">session_start();</span><span style="color: black;">$name=<span style="color: black;">"123"</span>;</span><span style="color: black;">$_SESSION[<span style="color: black;">username</span>]=$name;</span><span style="color: black;"><span style="color: black;">unset</span>($_SESSION[<span style="color: black;">username</span>]);</span><span style="color: black;"><span style="color: black;">echo</span> $_SESSION[<span style="color: black;">username</span>];</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span>echo测试结果如下:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Lhm5kkN9JXMaRn2ia4nZYuLOReSu9C538ia0ULnM4x18QISZ9MZ9GHAP9ApkDjicDufb1EQqgCosVo4RMr8Ab4KEA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">unset() 销毁指定的变量。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">unset() 在函数中的<span style="color: black;">行径</span>会依赖于想要销毁的变量的类型而有所<span style="color: black;">区别</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2、<span style="color: black;">运用</span>session_destroy()</strong></span></p><span style="color: black;">session_destroy();</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">session_destroy() 销毁当前会话中的<span style="color: black;">所有</span>数据,<span style="color: black;">然则</span>不会重置当前会话所<span style="color: black;">相关</span>的全局变量, <span style="color: black;">亦</span>不会重置会话 cookie。</span><span style="color: black;"><span style="color: black;">倘若</span>需要再次<span style="color: black;">运用</span>会话变量,必须重新调用 session_start() 函数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本篇<span style="color: black;">文案</span>就给关于<strong style="color: blue;">PHP中session存储变量以及删除变量的</strong><span style="color: black;">办法</span>介绍,<span style="color: black;">亦</span>是非常简单的,<span style="color: black;">期盼</span>对需要的<span style="color: black;">伴侣</span>有所<span style="color: black;">帮忙</span>!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><strong style="color: blue;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Lhm5kkN9JXOs2DZjYOCZztperOHPicgNHNWAuGNMQibDLmU5qmNu78EYaAFSO1m0U69AicQ7eROr9f1hzu6CYFH4Q/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></strong></a></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">-END-</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">声明:本文选自「&nbsp;</span><span style="color: black;">php中文网&nbsp;</span><span style="color: black;">」,搜索「 phpcnnew 」<span style="color: black;">就可</span>关注!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">▼</span><span style="color: black;">请点击下方:“阅读原文”,</span><span style="color: black;">在线</span><span style="color: black;">查看<span style="color: black;">所有</span><span style="color: black;">文案</span>内容!</span></span></p>




m5k1umn 发表于 2024-10-11 01:32:10

交流如星光璀璨,点亮思想夜空。

j8typz 发表于 2024-10-15 08:35:12

这篇文章真的让我受益匪浅,外链发布感谢分享!

nqkk58 发表于 2024-11-11 13:28:59

顶楼主,说得太好了!
页: [1]
查看完整版本: PHP中session存储及删除变量的办法