PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
//Session start
session_start();
//Set session vars
if (!isset($_SESSION['username']) && !isset($_SESSION['password']))
{
$_SESSION['username'] = 'testusername';
$_SESSION['password'] = 'testpassword';
}
else
{
echo '<p>You user name is already set to ' . $_SESSION['username'] . '</p>';
echo '<p>And your password is already set to ' . $_SESSION['password'] . '</p>';
}
if (isset($_GET['clear']))
{
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// Finally, destroy the session.
session_destroy();
echo '<p>Even though you see the information above, the session has already been terminated.
<a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '">Refresh the page</a> and you will see nothing as the page sets the session vars again.</p>';
}
echo '<p><a href="session-test-page2.php">Click here for page 2</a></p>';
echo '<p><a href="?clear=true">Click here to clear the session data</a></p>';
?>
<?php
//Session start
session_start();
//Set session vars
echo '<p>You user name is ' . $_SESSION['username'] . '</p>';
echo '<p>And your password is ' . $_SESSION['password'] . '</p>';
echo '<a href="session-test-page1.php">Click here to go back to page 1</a>';
?>
which I found in another thread, it does not carry any session info. Cookies ARE enabled. Any ideas?
Hey mike. That snippet is from another thread I had posted for someone whose session were not working either. It is essentially the same as what you are suggesting, with a little more information to the user.
To aarnott.
Make sure you save the long file as session-test-page1.php and the short file as session-test-page2.php. Also, make sure they are run from your webserver. Post back what the screen shows when you call the first page (it should actually take you to the second page and show the session data there).
Edit | Just noticed that code is not exactly what I posted. Try these: session-test-page1.php
<?php
//Session start
session_start();
//Set session vars
if (!isset($_SESSION['username']) && !isset($_SESSION['password']))
{
$_SESSION['username'] = 'testusername';
$_SESSION['password'] = 'testpassword';
header('Location: session-test-page2.php');
exit;
}
else
{
// Quick error checking
echo '<pre>'; var_dump($_SESSION); echo '</pre>';
echo '<p>You user name is already set to ' . $_SESSION['username'] . '</p>';
echo '<p>And your password is already set to ' . $_SESSION['password'] . '</p>';
}
if (isset($_GET['clear']))
{
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// Finally, destroy the session.
session_destroy();
echo '<p>Even though you see the information above, the session has already been terminated.
<a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '">Refresh the page</a> and you will see nothing as the page sets the session vars again.</p>';
}
echo '<p><a href="session-test-page2.php">Click here for page 2</a></p>';
echo '<p><a href="?clear=true">Click here to clear the session data</a></p>';
?>
<?php
//Session start
session_start();
// Quick error checking
echo '<pre>'; var_dump($_SESSION); echo '</pre>';
// Echo session vars
echo '<p>You user name is ' . $_SESSION['username'] . '</p>';
echo '<p>And your password is ' . $_SESSION['password'] . '</p>';
echo '<a href="session-test-page1.php">Click here to go back to page 1</a>';
?>