Page 1 of 1

Sessions just not working at all

Posted: Thu Mar 29, 2007 1:56 pm
by aarnott
When I try to create sessions in firefox or IE it won't carry to the next page. If I try these files:

Code: Select all

<?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>';
?>
and

Code: Select all

<?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?

Andrew Arnott

Posted: Thu Mar 29, 2007 2:54 pm
by mikeq
Hi andrew,

Start simple

Code: Select all

<?php
session_start();

$_SESSION['test'] = "This is a test";
?>
<a href="next.php">Next Page</a>
then on next.php

Code: Select all

<?php
session_start();

print "Session value: {$_SESSION['test']}";
?>
Breaking it down to this will show if it is definitely sessions that are the issue and not something else in your code.

Also turn on error reporting.

Posted: Thu Mar 29, 2007 3:42 pm
by RobertGonzalez
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

Code: Select all

<?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>';
?>
session-test-page2.php

Code: Select all

<?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>';
?>