470,870 Members | 1,435 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

SESSION variable gets cleared

I have two files login.php and loggedin.php. If I understand $_SESSION
correctly, its values should stay in place when I go from login.php to
loggedin.php, right? They won't, $_SESSION gets cleared in
loggedin.php. Have I done anything wrong?

Here are my test files:
login.php

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
PHP: Log In
</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1" />
</head>
<body>
<div id="pageheader">
<h1>
Pot Head Pixie: PHP
</h1>
</div>
<div id="main">
<!-- short_open_tag = Off -->
<?php
echo '<p>Hello World</p>';
echo $_SERVER['HTTP_USER_AGENT'];
echo str_replace('\\','/',getcwd());

@session_destroy();
@session_start();
unset($adminlogin);
unset($adminpass);
unset($adminip);
$_SESSION['adminlogin']=md5('adm');
$_SESSION['adminpass']=md5('pass');
unset($adminlogin);
unset($adminpass);
unset($adminip);

echo '<br />';
echo '$_SESSION[\'adminlogin\']: ';
echo $_SESSION['adminlogin'];
echo '<br />';
if (isset($_SESSION['adminlogin'])) {
echo "adminlogin is set";
} else {
echo "adminlogin is not set";
}
?>
<form action="loggedin.php" method="post">
<p>
<input type="text" value="user" />
</p>
<p>
<input type="password" value="password" />
</p>
<p>
<input type="submit" value="OK" />
</p>
</form>
</div>
</body>
</html>


loggedin.php
<h1>
PHP: Logged In
</h1>
</div>
<div id="main">
<!-- short_open_tag = Off -->
<?php
echo '<p>Hello World</p>';
echo $_SERVER['HTTP_USER_AGENT'];
echo str_replace('\\','/',getcwd());
echo '<br />';
echo '$_SESSION[\'adminlogin\']: ';
echo $_SESSION['adminlogin'];
echo '<br />';

if (isset($_SESSION['adminlogin'])) {
echo "adminlogin is set";
} else {
echo "adminlogin is not set";
}

?>
<form action="loggedout.php" method="post">
<p>
<input type="submit" value="Logout" />
</p>
</form>
</div>
</body>
</html>

--
Per Johansson
Systems developer
http://per.johansson.name/

Jul 17 '05 #1
2 2370
Per Johansson wrote:
session_destroy();
session_start();


You need these to appear every time a page (but not including all of its
includes) is returned from a trip to your server.
login.php
session_destroy();
session_start();
loggedin.php
session_destroy();
session_start();
include_once('javascript_includes.js');
include_once('banner.htm');
print "you are logged in";
You can see in the second example, that you only need to start your session
once, you do not also need to start it on the pages that are included from
loggedin.php.

--
TJ Talluto
torpedo51 at yahoo dot com
Jul 17 '05 #2
On Mon, 1 Nov 2004 20:55:50 UTC, TJ Talluto <tj@getlostspammers.com>
wrote:
Per Johansson wrote:
session_destroy();
session_start();


You need these to appear every time a page (but not including all of its
includes) is returned from a trip to your server.
login.php
session_destroy();
session_start();
loggedin.php
session_destroy();
session_start();
include_once('javascript_includes.js');
include_once('banner.htm');
print "you are logged in";
You can see in the second example, that you only need to start your session
once, you do not also need to start it on the pages that are included from
loggedin.php.


OK, I added to loggedin.php these lines first in the script (I haven't
included anything yet):
session_destroy();
session_start();

So now $_SESSION retains its values.
This is the first day ever I have worked with PHP, so there are some
novelties.

--
Per Johansson
Systems developer
http://per.johansson.name/

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Paul | last post: by
6 posts views Thread by Gonenc Ercan | last post: by
1 post views Thread by Wiktor Zychla | last post: by
3 posts views Thread by Phillip N Rounds | last post: by
6 posts views Thread by mosscliffe | last post: by
1 post views Thread by Chad | last post: by
14 posts views Thread by Rick | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.