1

Look at the example code directly:

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();
echo '<br /><a href="page2.php">page 2</a>';

?>

And another page:

<?php
// page2.php

session_destroy();
session_unset();
session_start();

echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);

echo '<br /><a href="page1.php">page 1</a>';
?>

Although I call session_destroy(), session_unset(), I still get the data coming from page1. why? and how to really clear the session? Thanks!

Saty
  • 22,443
  • 7
  • 33
  • 51
itenyh
  • 1,921
  • 2
  • 23
  • 38

2 Answers2

0

This should do the trick

session_start();
$_SESSION = array();
session_unset();

But just for clarity, this is happening to you because you have to call session_start() first

session_start();
session_destroy();
session_unset();
ksealey
  • 1,698
  • 1
  • 17
  • 16
0

You have to start the session first on the second page page2.php.Put session_start(); at the top on second page.

sonam gupta
  • 775
  • 6
  • 17