duanmei9980 2017-08-30 14:23
浏览 28
已采纳

如何将$ _SESSION变量从另一个页面发送回索引文件?

I have a global variable called $_SESSION['user_name'] which is set with POST value when I click on the Submit button in a form.

In this php file, I check if $_SESSION['user_name'] is empty.

If yes, I have to file a form and if no, means the session is still going on and it sends you to the profile.

Anyway, after I fill the form, then it compares this variable if it matches another variable and then heads to the profile.php if true.

On this page, I have one button which is meant to "logout" the user.

On pressing, it sends you to another php file where I change the variable (I have echo-ed it before and it does change it)

but it doesn't forward it back to main page (index.php) and the variable still has the value from the $_POST although it is different in logout.php as I change it.


Code I have at logout.php:

<?php
  session_start();

  $_SESSION['user_name'] = ""; // makes it empty
  echo $_SESSION['user_name'];
?>

And some html where I use meta tag to redirect back to the index.


Now, my question is, how can I forward the changed variable back to the index or form page? Or is there any other way of doing that?

Thanks :)

  • 写回答

1条回答 默认 最新

  • duanlin1933 2017-08-30 18:25
    关注

    Try setting your logout.php file to something like this:

    <?
    
    require_once('functions.php');
    
    log_out_user();
    header("Location: login.php");
    exit;
    
    ?>
    

    Now in your functions file, for example, try writing this function:

    function log_out_user() {
       unset($_SESSION['username']);
       // You can also use: session_destroy();
       return true;
    }
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改