-3

How to destroy session across all domains in php. I simply use this code but this is not work

session_destroy();

across all domains means i create a session in example.com that is also create in www.example.com this works perfectly but let suppose i destroy session from example.com it is destroy only from example.com not from www.example.com

2 Answers2

0

If I have understood correctly, you want to destroy the sessions set in every other pages already. I will demonstration a little behavior of how it should actually be, and hopefully you will find the place where you were messing up.

Let's say we have a page1.php we have just started using sessions.

page1.php

<?php

 // starts session
 session_start();

 // setting variable values during session
 $_SESSION['ID']='13CS54';

 $_SESSION['name']='Muneer';

 $_SESSION['account']='Stack Overflow';

echo "You have set the sessions"; 
?>

Now on another page let's say page2.php, you are using those sessions.

page2.php

<?php

//don't forget to put session_start(); in the start
session_start();


echo "ID: " .$_SESSION['ID']."<br>";

echo "Name: " .$_SESSION['name']. "<br>";

echo "Account: " .$_SESSION['account'];

?>

On page3.php or you wish to use/add more sessions.

page3.php

<?php

//yes, add the session_start(); (always)
session_start();

$_SESSION['country'] = 'Pakistan';

echo "ID: " .$_SESSION['ID']."<br>";

echo "Name: " .$_SESSION['name']. "<br>";

echo "Location: " .$_SESSION['country'];

?>

Now, on page4.php you wish to destroy all the sessions made on every page.

page4.php

<?php

session_start();
session_destroy();

?>
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

I think what you wanted to ask was how to destroy sessions on a domain plus its subdomains
e.g example.com, account.example.com, app.example.com
You have to add these lines before you call session_start():

$sess_name = session_name('app');
session_set_cookie_params(0, '/', '.example.com');

Check this link to know more on session_set_cookie_params() I have been experiencing this problem, but those lines fixed it for me

jontee
  • 1
  • 2