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!

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

报告相同问题?