0

I'm new to stackoverflow but I'm not new to PHP. Now I've a funny problem:

I'm developing with XAMPP 7.1.11 with PHP 7.1.11 locally, doing logging out from sessions with

$_SESSION = array();
session_destroy();  

in a logout.php file. On top of the page there's a

session_start();

and in past everything went without any problem. Now I did a php version change at the provider from PHP 7.0 to 7.1 and it's not possible to log out any more, the session informations seem not be deleted. I searched the internet but I didn't find a helpful hint and the provider told me to ask some PHP forums.

I tried all hints in the topics

why session_destroy() not working

The session_destroy(); doesn't work correctly on server with php 5.3.21

and many more but nothing is working. A Change from PHP 7.0 to PHP 7.2 at the provider doesn't help as well.

What I'm doing wrong? as I said: locally everything works normally.

Thanks for your help!

Here's the complete logout.php file:

<?php
session_start();
?>
<!-- Import Wordpress -->
<?php 
define('WP_USE_THEMES', false);
require('../wp-load.php'); ?>
<?php get_header(); ?>
<link href="cpplattform.css" type="text/css" rel="stylesheet">
<div class="spacer"></div>
<div class="container">
    <div class="row">
        <div class="<?php if ( is_active_sidebar( 'rightbar' ) ) : ?>col-md-8<?php else : ?>col-md-12<?php endif; ?>">
            <div class="content">                   
                <h2 class="entry-title">Logout</h2>
                <!----------------------------------------------------->                
                <section class = "conf">    
                    <i class='fa fa-power-off fa-5x' style ='color:#00ADED'></i>
                    <br>
                    <br>
                    Your logout was successful! Good Bye!
                    <br>
                    <br>
                    <a class="btn btn-md btn-inverse" href="cplogin.php">Login again</a></p>
                    <?php

                        $_SESSION = array();
                        $_SESSION['username'] = "";                     
                        session_destroy();                      
                    ?>                  
                </section>              
                <!----------------------------------------------------->
            </div><!--content-->
        </div>
    </div>
</div>

<!-- Change all links from Wordpress -->
<script src="cplinkmodify.js"></script>
<?php get_footer(); ?>

I added some static pages and imported a Wordpress theme.

Jan-Hendrik
  • 3
  • 1
  • 7
  • **Note: You do not have to call session_destroy() from usual code. Cleanup $_SESSION array rather than destroying session data.** - From [the manual](http://php.net/manual/en/function.session-destroy.php) – Martin Jan 13 '18 at 15:14
  • 3
    Show us your code page where you set `$_SESSION = array();` This should be all you need to do. Check you have set `session_start();` on *this* page. – Martin Jan 13 '18 at 15:16
  • Possible duplicate of [why session\_destroy() not working](https://stackoverflow.com/questions/6472123/why-session-destroy-not-working) – Chirag Jain Jul 12 '18 at 13:42
  • @Chirag: Thank you. As I mentioned in my question: I already tried those hints in https://stackoverflow.com/questions/6472123/why-session-destroy-not-working and it didn't help. – Jan-Hendrik Jul 13 '18 at 18:41

1 Answers1

1

Note: You do not have to call session_destroy() from usual code. Cleanup $_SESSION array rather than destroying session data.

Show us your code page where you set $_SESSION = array(); This should be all you need to do. Check you have set session_start(); on this page:

session_start();
$_SESSION = []; //empty the array. 

--End of file.

If you want to make absolutely sure that it works ok you can try using something like this:

session_start();
$_SESSION = []; //empty array. 
session_write_close(); 

But note any further edits to any session data on this script will not be saved once the script completes.


You may also have an issue if your scripts are in different folders and the local php.ini session name is different in these different folders... Different names, different sessions.

Central PHP.ini:

session.name=somethingSessiony

local folder specific PHP.ini

session.name=somethingsessiony

If you feel this may be a factor try something like this:

error_log(__FILE__." : " .print_r(session_name(),true)); 

In both the file that clears the session data and the file that should be reading the "empty" session data.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • Hi Martin! Thanks for your answer in advance! I added the complete file and will check your hints with local php.ini files. – Jan-Hendrik Jan 13 '18 at 19:05
  • I did'nt change anything - and now it works with PHP 7.1 at the provider. Can't say, what it was. I now added `session_write_close();` - hopefully it helps to avoid problems in future. – Jan-Hendrik Jan 16 '18 at 07:31