session can not be cleared - php

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 />page 2';
?>
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 />page 1';
?>
Although I call session_destroy(), session_unset(), I still get the data coming from page1. why? and how to really clear the session? Thanks!

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();

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

Related

PHP Session Variables are not passed to next page

I have 2 PHP pages in the same folder. I am trying to set a session variable on one page (test.php), then read its value on another (test2.php). However, the session variable gets lost on the second page.
test.php
<?php
session_start();
$_SESSION["test"] = "123hello";
$hello = $_SESSION["test"];
echo "out: $hello";
$sid = session_id();
echo "<br> sid: $sid";
?>
test2.php
<?php
session_start();
$hello = "test";
if(isset($_SESSION["test"]))
{
$hello = $_SESSION["test"];
}
echo "out: $hello";
$sid = session_id();
echo "<br> sid: $sid";
?>
First I visit test.php, which outputs:
out: 123hello
Then I visit test2.php, which outputs:
out: test
Which means in test.php, the session variable does keep its value. However, for some reason it gets lost in test2.php.

Webmatrix session_destroy()

I have written a simple code for $_SESSION variable in the first php file:
<?php
session_start();
$_SESSION["name"] = "John";
?>
and in another php file to render this:
<?php
session_start();
echo $_SESSION["name"];
?>
But after that i used session_unset(); and session_destroy(); and after that i can't render any new $_SESSION variable nor the existing one. I am using Microsoft WebMatrix program and Chrome as main browser. Any suggestions? Thank you in advance.
That is because session_destroy(); destroys the current session and also sends a header to the browser to delete the session variable. In the same time the session is deleted on the server (in PHP) and the $_SESSION variables can no longer be used. You can always try to save the $_SESSION in another variable;
session_start();
$_SESSION['test'] = 'foo';
Next page:
session_start();
$saveSession = $_SESSION;
session_destroy();
var_dump($_SESSION); //Gives an empty array
var_dump($saveSession); //Still has ['test' => 'foo']
More information: http://php.net/manual/en/function.session-destroy.php and http://php.net/manual/en/book.session.php
Also, sidenote, you do not need to open and close PHP tags if they are combined;
<?php
session_start();
echo $_SESSION["name"];
?>
works just as well as
<?php
session_start();
?>
<?php
echo $_SESSION["name"];
?>

php session can not pass variable to other page

I have a formal code, but the session variable can't be passed to another page.
So here is my sample code. However, the result of this code is 0.:
page1 code:
<?php
session_start();
$_SESSION['try'] = 5;
header('Location: page2.php');
?>
page2 code:
<?php
session_start();
$test = $_SESSION['try'];
echo $test;
?>
From the manual:
session_start() creates a session or resumes the current
one based on a session identifier passed via a GET or POST request,
or passed via a cookie.
pay attention to the last two lines of code:
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br />page 2';
// Or maybe pass along the session id, if needed
echo '<br />page 2';
?>

session_regenerate_id doesnt work together with sesion_destroy

I found session_regenerate_id will not work it together with session_destroy(), please see
this one works
<?php
session_start();
$old='old is: '.session_id();
session_regenerate_id(true);
echo $old;
echo '<br>';
echo 'new is: '.session_id();
?>
this one failed, return 2 same session id numbers
<?php
session_start();
$old='old is: '.session_id();
session_regenerate_id(true);
session_destroy();
session_unset();
session_start();
echo $old;
echo '<br>';
echo 'new is: '.session_id();
?>
I use php 5.3.3, the second one is a recommended method to produce a new session, but why it does not work on my side.
thanks everyone,after tests, I found session_destroy must be invoked before session_regenerate_id(), or session_regenerate_id never works.
<?php
session_start();
$_SESSION['abc']=12323;
$old='old is: '.session_id();
session_unset();
session_destroy();
session_start();
session_regenerate_id(true);
echo $old;
echo '<br>';
echo 'new is: '.session_id();
?>
session_start();
$old='old is: '.session_id();
session_destroy();
session_start();
session_regenerate_id(true);
echo $old;
echo '<br>';
echo 'new is: '.session_id();
Place
session_unset();
above session_destroy(); or remove session_unset(); completely. As you are destroying the session.
Try this code:
<?php
session_start();
$oldSessionId ='Old session ID is: ' . session_id() . '<br />';
echo $oldSessionId;
session_destroy();
session_start();
session_regenerate_id(true);
echo 'New session ID is: ' . session_id();
?>
You can also add in the following code after session_destroy() if you wish
setcookie(session_name(),'',0,'/');
Hope this helps
You should destroy session first. Try this:
session_destroy();
session_unset();
setcookie(session_name(), null, 0, "/");

Help me with Php session vs Header redirect?

I have the following pages:
page1.php
<?php
if (isset($_GET['link'])) {
session_start();
$_session['myvariable'] = 'Hello World';
header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php');
exit;
}
?>
Click Here
page2.php
<?php
print 'Here is page two, and my session variable: ';
session_start();
print $_session['myvariable']; //This line could not output.
exit;
?>
When I try output $_session['myvariable'] I did not get the result hello world message.
I could not find out the solution to fix it?
$_SESSION not $_session. Uppercase.
error_reporting(E_ALL); at the top of the script always helps in such case
session_start() has to be called before you send any output as it relies upon cookies to store the ID.
Also $_SESSION is uppercase
<?php
session_start();
echo $_SESSION['myvariable'];
echo 'Here is page two, and my session variable: ';
exit;
?>
HTTP headers must be the very first output, so session_start() must be at the top of your code.
Other notes:
* $_SESSION should be uppercase.
* Echo > print

Categories