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!

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化