【问题标题】:Session variable is not working properly how can i improve会话变量无法正常工作我该如何改进
【发布时间】:2017-06-10 11:37:14
【问题描述】:
$_SESSION['profile_updated']="yes";

设置会话变量!

<div class="col-md-12">
<?php if(isset($_SESSION['profile_updated'])){ ?>
<div class="confirmationmsgssg">Profile Updated successfully.</div> 
<?php unset($_SESSION['profile_updated']); } ?>
<h3>Your personal info</h3>
</div>

我正在尝试使用会话显示警报消息..

实际上这是一个增强项目,我无法更改整个代码,因为它存在于很多页面中

代码实际发生的事情首先是在视图页面中检查,例如$_SESSION['profile_updated'] 是否显示消息,它会在 3 秒后自动消失......当我刷新页面时,该消息不应该出现,因为我没有更新任何东西,我必须使用 unset..

如果我删除unset($_SESSION['profile_updated']);,它工作正常 这个未设置。但是当我添加这个时,我不知道为什么它不起作用

我到处检查session_start(); 在我的第一行 php 可能是什么错误..? 我试过ob_start();没用..!

【问题讨论】:

  • &lt;div class="col-md-12"&gt; &lt;?php if(!empty($_SESSION['profile_updated'])){ ?&gt; &lt;div class="confirmationmsgssg"&gt;Profile Updated successfully.&lt;/div&gt; &lt;?php $_SESSION['profile_updated'] = ''; } ?&gt; &lt;h3&gt;Your personal info&lt;/h3&gt; &lt;/div&gt;

标签: php session variables unset


【解决方案1】:

您的代码对我来说看起来非常好。我不知道为什么这不起作用。

使用!empty$_SESSION['profile_updated'] = '' 尝试一次

<div class="col-md-12"> 
    <?php if(!empty($_SESSION['profile_updated'])){ ?> 
        <div class="confirmationmsgssg">Profile Updated successfully.</div> 
        <?php $_SESSION['profile_updated'] = '';?>
    <?php  } ?> 
    <h3>Your personal info</h3> 

注意:-

确保session_start(); 在您的文件顶部

尝试清除缓存和 cookie 并检查您的原始代码。

【讨论】:

    【解决方案2】:

    清除cookies,一切正常。

    <div class="col-md-12">
    <?php if(isset($_SESSION['profile_updated'])){ ?>
    <div class="confirmationmsgssg">Profile Updated successfully.</div>
    <?php unset($_SESSION['profile_updated']); } ?>
    <h3>Your personal info</h3>
    </div>
    

    【讨论】:

    • 你的答案代码和OP的问题代码有什么区别?
    • Abdul Waheed 我没有看到接受的答案代码有任何变化
    • 我已经清除了 cookie 并尝试了一个新的浏览器,@Anant
    • @AbdulWaheed I have cleared the cookies and tried a new browser it wokred:- 你说的新浏览器是什么意思?
    • @AbdulWaheed 那么在 fiifox 中会发生什么?不工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    相关资源
    最近更新 更多